亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2390 | 回復(fù): 4
打印 上一主題 下一主題

[C] url格式檢查(不用正則表達式) [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2013-11-08 20:28 |只看該作者 |倒序瀏覽
請好心人給個代碼參考。用標(biāo)準(zhǔn)c寫的url格式的正確性校驗程序。不是用正則表達式的哦:)

論壇徽章:
0
2 [報告]
發(fā)表于 2013-11-08 21:29 |只看該作者
一定要用C的話有兩個辦法,第一步寫出需要的regexp,然后要么用flex寫一個lexer,要么handcraft一個自動機(別忘了轉(zhuǎn)換為DFA)。最后發(fā)現(xiàn)干嘛不直接用正則呢?推薦pcre或者re2

論壇徽章:
0
3 [報告]
發(fā)表于 2013-11-08 21:29 |只看該作者
本帖最后由 zhousiyv 于 2013-11-08 22:03 編輯

這有個可以從自動機生成C的東東,http://www.complang.org/ragel,有興趣可以模仿一下它生成的代碼。

論壇徽章:
154
2022北京冬奧會紀(jì)念版徽章
日期:2015-08-07 17:10:5720周年集字徽章-年
日期:2022-10-26 16:44:2015-16賽季CBA聯(lián)賽之深圳
日期:2022-11-02 14:02:4515-16賽季CBA聯(lián)賽之八一
日期:2022-11-28 12:07:4820周年集字徽章-20	
日期:2023-07-19 08:49:4515-16賽季CBA聯(lián)賽之八一
日期:2023-11-04 19:23:5115-16賽季CBA聯(lián)賽之廣夏
日期:2023-12-13 18:09:34
4 [報告]
發(fā)表于 2013-11-09 14:13 |只看該作者
樓上玩的可是高科技啊

論壇徽章:
2
2015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
5 [報告]
發(fā)表于 2013-11-10 14:34 |只看該作者
  1. #include <string.h>

  2. int charset_in(int c, const char *set)
  3. {
  4.         const unsigned char *p;
  5.         unsigned char uc;
  6.         int retval;

  7.         p = (const unsigned char *)set;
  8.         uc = (unsigned char)c;
  9.         for (; *p != '\0'; ++p) {
  10.                 if (*p == '-') {
  11.                         ++p;
  12.                         retval = 0;
  13.                 }else {
  14.                         retval = 1;
  15.                 }
  16.                 if (*p == 'L') {
  17.                         for (++p; *p != '\0'; ++p) {
  18.                                 if (*p == uc) {
  19.                                         return retval;
  20.                                 }
  21.                         }
  22.                 }else if (*p == 'S') {
  23.                         for (++p; *p != '\0' && *(p + 1) != '\0'; p += 2) {
  24.                                 if (uc >= *p && uc <= *(p + 1)) {
  25.                                         return retval;
  26.                                 }
  27.                         }
  28.                 }
  29.         }
  30.         return 0;
  31. }

  32. int skip_text(const char *s, int *io_off, int lmt, const char *text)
  33. {
  34.         int off;
  35.         int len;

  36.         off = *io_off;
  37.         len = strlen(text);
  38.         if (off + len <= lmt) {
  39.                 if (strncmp(s + off, text, len) == 0) {
  40.                         *io_off = off + len;
  41.                         return 0;
  42.                 }
  43.         }
  44.         return -1;
  45. }

  46. int skip_token(const char *s, int *io_off, int lmt, const char *charset)
  47. {
  48.         int off;

  49.         off = *io_off;
  50.         if (off < lmt && charset_in(s[off], charset)) {
  51.                 do {
  52.                         ++off;
  53.                 }while (off < lmt && charset_in(s[off], charset));
  54.                 *io_off = off;
  55.                 return 0;
  56.         }
  57.         return -1;
  58. }

  59. int check_url_id(const char *s, int *io_off, int lmt)
  60. {
  61.         int off;

  62.         off = *io_off;
  63.         if (off < lmt) {
  64.                 if (charset_in(s[off], "L_\0SazAZ\0")) {
  65.                         if (skip_token(s, &off, lmt, "L_-\0SazAZ09\0") == 0) {
  66.                                 *io_off = off;
  67.                                 return 0;
  68.                         }
  69.                 }
  70.         }
  71.         return -1;
  72. }

  73. int check_urlname(const char *s, int *io_off, int lmt)
  74. {
  75.         int off;

  76.         off = *io_off;
  77.         if (skip_token(s, &off, lmt, "L-%\0SazAZ09\0") == 0) {
  78.                 *io_off = off;
  79.                 return 0;
  80.         }
  81.         return -1;
  82. }

  83. int check_portnum(const char *s, int *io_off, int lmt)
  84. {
  85.         int off;

  86.         off = *io_off;
  87.         if (skip_token(s, &off, lmt, "S09\0") == 0) {
  88.                 *io_off = off;
  89.                 return 0;
  90.         }
  91.         return -1;
  92. }

  93. int check_urlpath(const char *s, int *io_off, int lmt)
  94. {
  95.         int off;

  96.         off = *io_off;
  97.         while (skip_text(s, &off, lmt, "/") == 0
  98.                 && check_urlname(s, &off, lmt) == 0) {
  99.         }
  100.         *io_off = off;
  101.         return 0;
  102. }

  103. int check_urlparam_item(const char *s, int *io_off, int lmt)
  104. {
  105.         int off;

  106.         off = *io_off;
  107.         if (check_urlname(s, &off, lmt) == 0) {
  108.                 if (skip_text(s, &off, lmt, "=") == 0) {
  109.                         if (check_urlname(s, &off, lmt) == 0) {
  110.                                 *io_off = off;
  111.                                 return 0;
  112.                         }
  113.                 }else {
  114.                         *io_off = off;
  115.                         return 0;
  116.                 }
  117.         }
  118.         return -1;
  119. }

  120. int check_urlparam(const char *s, int *io_off, int lmt)
  121. {
  122.         int off;

  123.         off = *io_off;
  124.         if (skip_text(s, &off, lmt, "?") == 0) {
  125.                 if (check_urlparam_item(s, &off, lmt) == 0) {
  126.                         while (skip_text(s, &off, lmt, "&") == 0
  127.                                 && check_urlparam_item(s, &off, lmt) == 0) {
  128.                         }
  129.                 }
  130.         }
  131.         *io_off = off;
  132.         return 0;
  133. }

  134. int check_urltag(const char *s, int *io_off, int lmt)
  135. {
  136.         int off;

  137.         off = *io_off;
  138.         if (skip_text(s, &off, lmt, "#") == 0) {
  139.                 check_url_id(s, &off, lmt);
  140.         }
  141.         *io_off = off;
  142.         return 0;
  143. }

  144. int check_url(const char *url, int url_len)
  145. {
  146.         int off;

  147.         off = 0;
  148.         if (check_url_id(url, &off, url_len) == 0) {
  149.                 if (skip_text(url, &off, url_len, "://") == 0) {
  150.                         if (check_urlname(url, &off, url_len) == 0) {
  151.                                 if (skip_text(url, &off, url_len, ":") == 0) {
  152.                                         if (check_urlname(url, &off, url_len) == 0) {
  153.                                                 if (skip_text(url, &off, url_len, "@") == 0) {
  154.                                                         if (check_urlname(url, &off, url_len) == 0) {
  155.                                                                 while (skip_text(url, &off, url_len, ".") == 0
  156.                                                                         && check_urlname(url, &off, url_len) == 0) {
  157.                                                                 }
  158.                                                                 if (skip_text(url, &off, url_len, ":") == 0) {
  159.                                                                         check_portnum(url, &off, url_len);
  160.                                                                 }
  161.                                                                 check_urlpath(url, &off, url_len);
  162.                                                                 check_urlparam(url, &off, url_len);
  163.                                                                 check_urltag(url, &off, url_len);
  164.                                                                 if (off == url_len) {
  165.                                                                         return 0;
  166.                                                                 }
  167.                                                         }
  168.                                                 }
  169.                                         }
  170.                                 }else if (skip_text(url, &off, url_len, "@") == 0) {
  171.                                         if (check_urlname(url, &off, url_len) == 0) {
  172.                                                 while (skip_text(url, &off, url_len, ".") == 0
  173.                                                         && check_urlname(url, &off, url_len) == 0) {
  174.                                                 }
  175.                                                 if (skip_text(url, &off, url_len, ":") == 0) {
  176.                                                         check_portnum(url, &off, url_len);
  177.                                                 }
  178.                                                 check_urlpath(url, &off, url_len);
  179.                                                 check_urlparam(url, &off, url_len);
  180.                                                 check_urltag(url, &off, url_len);
  181.                                                 if (off == url_len) {
  182.                                                         return 0;
  183.                                                 }
  184.                                         }
  185.                                 }else {
  186.                                         while (skip_text(url, &off, url_len, ".") == 0
  187.                                                 && check_urlname(url, &off, url_len) == 0) {
  188.                                         }
  189.                                         if (skip_text(url, &off, url_len, ":") == 0) {
  190.                                                 check_portnum(url, &off, url_len);
  191.                                         }
  192.                                         check_urlpath(url, &off, url_len);
  193.                                         check_urlparam(url, &off, url_len);
  194.                                         check_urltag(url, &off, url_len);
  195.                                         if (off == url_len) {
  196.                                                 return 0;
  197.                                         }
  198.                                 }
  199.                         }
  200.                 }
  201.         }
  202.         return -1;
  203. }

  204. /* TESTING */

  205. #define LENGTH(a) (sizeof(a)/sizeof((a)[0]))

  206. #include <stdio.h>

  207. int main(void)
  208. {
  209.         struct url_example {
  210.                 const char *url_text;
  211.                 int result;
  212.         }url_examples[] = {
  213.                 "http://abc@aa1.com:368?a=123&b=456#kkk", 0,
  214.                 "http://abc.aa1.com:368?a=123&b=456#kkk", 0,
  215.                 "http://abc@abc.aa1.com?a=123&b=456#kkk", 0,
  216.                 "http://abc.aa1.com", 0,
  217.                 "ftp://user:password@abc.aa1.com:22", 0,
  218.                 "http://123@abc.aa1.com#kk", 0,
  219.                 "http://abc@aa-bb.net:387?a=23343&b=%2f#kslkj", 0,
  220.                 "http://abc@aa-bb.net:3a87?a=23343&b=%2f#kslkj", -1,
  221.                 "http-ftp@://www.sina.com", -1,
  222.         };
  223.         int i;
  224.         int retval;

  225.         for (i = 0; i < LENGTH(url_examples); ++i) {
  226.                 fputs(url_examples[i].url_text, stdout);
  227.                 retval = check_url(url_examples[i].url_text, strlen(url_examples[i].url_text));
  228.                 if (url_examples[i].result == 0) {
  229.                         fputs(" => CORRECT TESTING... ", stdout);
  230.                 }else {
  231.                         fputs(" => WRONG TESTING... ", stdout);
  232.                 }
  233.                 if (retval == url_examples[i].result) {
  234.                         puts("PASSED");
  235.                 }else {
  236.                         puts("FAILED");
  237.                 }
  238.         }
  239.         return 0;
  240. }
復(fù)制代碼
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP