- UID
- 418
- 精华
- 积分
- 3994
- 威望
- 点
- 宅币
- 个
- 贡献
- 次
- 宅之契约
- 份
- 最后登录
- 1970-1-1
- 在线时间
- 小时
|
最近用C自己写词法分析器
词法分析器的标准写法是构建自动机进行正则匹配
然而我不走寻常路,自己使用字符串函数拆解字符串
因为我的第一次已经给了vb(请大家不要:{:soso_e127:} )
所以vb的字符串函数已经扔不掉了,这次用C重写了vb大多数字符串函数。
文件名叫做:cvbstrfun.h,大家只需包含该头文件即可
- /////////////////////////////////////////////////
- // Description: //
- // C Style VB Str Func //
- // Name:cvbstrfun.h //
- // Auther:Cyycoish //
- // ID:201506282102A //
- // (C) 2015 [url]www.0xaa55.com[/url] //
- // All Rights Reserved. //
- /////////////////////////////////////////////////
- #include <locale.h>
- #include <wchar.h>
- #include <tchar.h>
- #include <malloc.h>
- #define WCHR wchar_t
- //________________________
- //| <Function Table> |
- //|______________________|
- //|C Style |VB Style |
- //|-----------|----------|
- //|wslen |Len |
- //|wsucase |UCase |
- //|wsleft |Left |
- //|wsinverse |StrReverse|
- //|wsright |Right |
- //|wsmid |Mid |
- //|wsinstr |InStr |
- //|wsltrim |LTrim |
- //|wsrtrim |RTrim |
- //|wsinstrrev |InStrRev |
- //|___________|__________|
- int wslen(WCHR* s)
- {
- return wcslen(s);
- }
- void wsucase(WCHR* s)
- {
- int i;
- for (i = 0; i <= wcslen(s) - 1; i++)
- s[i] = towupper(s[i]);
- return;
- }
- void wslcase(WCHR* s)
- {
- int i;
- for (i = 0; i <= wcslen(s) - 1; i++)
- s[i] = towlower(s[i]);
- return;
- }
- WCHR* wsleft(WCHR* szTar, int n)
- {
- int i;
- WCHR* rtn;
- WCHR tmpWChr[2] = {'\0', '\0'};
- if (n > wcslen(szTar))
- return L"";
- rtn = (WCHR*)malloc(sizeof(szTar));
- wcscpy(rtn, L"");
- for (i = 0; i <= n - 1; i++)
- {
- tmpWChr[0] = szTar[i];
- wcscat(rtn, tmpWChr);
- }
- return rtn;
- }
- void wsinverse(WCHR* szTar)
- {
- int i, j, l;
- WCHR* rtn;
- WCHR tmpWChr[2] = {'\0', '\0'};
- l = wcslen(szTar);
- rtn = (WCHR*)malloc(sizeof(szTar));
- wcscpy(rtn, L"");
- for (i = 0, j = l - 1; i < wcslen(szTar); i++, j--)
- {
- rtn[j] = szTar[i];
- }
- rtn[i] = '\0';
- wcscpy(szTar, rtn);
- }
- WCHR* wsright(WCHR* szTar, int n)
- {
- int i, j, l;
- WCHR* rtn;
- WCHR tmpWChr[2] = {'\0', '\0'};
- l = wcslen(szTar);
- if (n > l)
- return L"";
- rtn = (WCHR*)malloc(sizeof(szTar));
- wcscpy(rtn, L"");
- for (i = l - 1, j = 1; j <= n; i--, j++)
- {
- tmpWChr[0] = szTar[i];
- wcscat(rtn, tmpWChr);
- }
- wsinverse(rtn);
- return rtn;
- }
- WCHR* wsmid(WCHR* szTar, int st, int n)
- {
- int i;
- WCHR* rtn;
- WCHR tmpWChr[2] = {'\0', '\0'};
- if ((st - 1 + n) > wcslen(szTar))
- return L"";
- rtn = (WCHR*)malloc(sizeof(szTar));
- wcscpy(rtn, L"");
- for (i = st - 1; i <= st + n - 2; i++)
- {
- tmpWChr[0] = szTar[i];
- wcscat(rtn, tmpWChr);
- }
- return rtn;
- }
- int wsinstr(WCHR* szTar, WCHR* szCmp, int st)
- {
- int i;
- if (st >= wcslen(szTar) - wcslen(szCmp))
- return -1;
- for (i = st; i <= wcslen(szTar); i++)
- {
- if (!wcscmp(szCmp, wsmid(szTar, i, wcslen(szCmp))))
- return i;
- }
- return -1;
- }
- void wsltrim(WCHR* szTar)
- {
- int i;
- int b = 0;
- WCHR* rtn;
- WCHR tmpWChr[2] = {'\0', '\0'};
- rtn = (WCHR*)malloc(sizeof(szTar));
- wcscpy(rtn, L"");
- for (i = 0; i < wcslen(szTar); i++)
- {
- tmpWChr[0] = szTar[i];
- if (tmpWChr[0] != ' ' || b != 0)
- {
- wcscat(rtn, tmpWChr);
- if (tmpWChr[0] != ' ' && b == 0)
- b = !(b);
- }
- }
- wcscpy(szTar, rtn);
- }
- void wsrtrim(WCHR* szTar)
- {
- int i, j, l;
- int b = 0;
- WCHR* rtn;
- WCHR tmpWChr[2] = {'\0', '\0'};
- l = wcslen(szTar);
- rtn = (WCHR*)malloc(sizeof(szTar));
- wcscpy(rtn, L"");
- for (i = l - 1, j = 1; j < l; i--, j++)
- {
- tmpWChr[0] = szTar[i];
- if (tmpWChr[0] != ' ' || b != 0)
- {
- wcscat(rtn, tmpWChr);
- if (tmpWChr[0] != ' ' && b == 0)
- b = !(b);
- }
- }
- wsinverse(rtn);
- wcscpy(szTar, rtn);
- }
- int wsinstrrev(WCHR* szTar, WCHR* szCmp, int st)
- {
- int i;
- WCHR* s1, * s2;
- s1 = (WCHR*)malloc(sizeof(szTar));
- s2 = (WCHR*)malloc(sizeof(szCmp));
- wcscpy(s1, szTar); wcscpy(s2, szCmp);
- wsinverse(s1); wsinverse(s2);
- if (st >= wcslen(szTar) - wcslen(szCmp))
- return -1;
- for (i = st; i < wcslen(szTar); i++)
- {
- if (!wcscmp(s2, wsmid(s1, i, wcslen(s2))))
- {
- return wcslen(szTar) - i + 1;
- }
- }
- return -1;
- }
复制代码
还有一点 大家需要注意的是:
因为是宽字符,所以main函数调用该头文件,使用函数时要locate一下,具体做法如下:
- #include <stdio.h>
- #include "cvbstrfun.h"
- int _tmain(int argc, wchar_t* argv[])
- {
-
- WCHR str[] = {L"abcdefg"};
- //int a;
- setlocale(LC_CTYPE, ""); //就是这个关键的一句,不加显示不了中文
- //wcscpy(str, wsleft(str, 2));
- //wsinverse(str);
- //wcscpy(str, wsright(str, 3));
- //wcscpy(str, wsmid(str, 3, 2));
- //wsucase(str);
- //wsrtrim(str);
- //a = wsinstrrev(L"aabccccc", L"b", 1);
- //wprintf(L""%s"\n", str);
- //printf("\n%d\n", a);
-
- return 0;
- }
复制代码 |
|