Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1879291
  • 博文数量: 184
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2388
  • 用 户 组: 普通用户
  • 注册时间: 2016-12-21 22:26
个人简介

90后空巢老码农

文章分类

全部博文(184)

文章存档

2021年(26)

2020年(56)

2019年(54)

2018年(47)

2017年(1)

我的朋友

分类: NOSQL

2018-12-25 09:50:35

这两个文件是用来提供一些公用函数的,首当其冲的就是类正则表达式的stringmatchlen函数,看了之后感觉是豁然开朗,感觉对正则表达式的实现多了点理解,又可以去LeetCode装x了~~自己惦着脸加了点中文注释:-)

点击(此处)折叠或打开

  1. int stringmatchlen(const char *pattern, int patternLen, const char *string, int stringLen, int nocase)
  2. {
  3.     while(patternLen) {
  4.         switch(pattern[0]) {
  5.         case '*':
  6.             while (pattern[1] == '*') {
  7.                 pattern++;
  8.                 patternLen--;
  9.             }
  10.             if (patternLen == 1)
  11.                 return 1; /* match */
  12.             while(stringLen) {
  13.             /*关于正则当中的'*',只管匹配其后的模式即可,只要有一次匹配,即可匹配,所以下面用了递归*/
  14.                 if (stringmatchlen(pattern+1, patternLen-1,
  15.                             string, stringLen, nocase))
  16.                     return 1; /* match */
  17.                 string++;
  18.                 stringLen--;
  19.             }
  20.             return 0; /* no match */
  21.             break;
  22.         case '?':/* ?表示0次或者1次,直接跳过 */
  23.             if (stringLen == 0)
  24.                 return 0; /* no match */
  25.             string++;
  26.             stringLen--;
  27.             break;
  28.         case '[':
  29.         {
  30.             int not, match;

  31.             pattern++;
  32.             patternLen--;
  33.             not = pattern[0] == '^';
  34.             if (not) {
  35.                 pattern++;
  36.                 patternLen--;
  37.             }
  38.             match = 0;
  39.             while(1) {/*方括号的话,最好在自己的内部完成是否匹配,所以这边用了死循环*/
  40.                 if (pattern[0] == '\\' && patternLen >= 2) {
  41.                     pattern++;
  42.                     patternLen--;
  43.                     if (pattern[0] == string[0])
  44.                         match = 1;
  45.                 }
  46.                 else if (pattern[0] == ']') {
  47.                     break;
  48.                 }
  49.                 else if (patternLen == 0) {
  50.                     pattern--;
  51.                     patternLen++;
  52.                     break;
  53.                 }
  54.                 else if (pattern[1] == '-' && patternLen >= 3) {
  55.                     int start = pattern[0];
  56.                     int end = pattern[2];
  57.                     int c = string[0];
  58.                     if (start > end) {
  59.                         int t = start;
  60.                         start = end;
  61.                         end = t;
  62.                     }
  63.                     if (nocase) {
  64.                         start = tolower(start);
  65.                         end = tolower(end);
  66.                         c = tolower(c);
  67.                     }
  68.                     pattern += 2;
  69.                     patternLen -= 2;
  70.                     if (c >= start && c <= end)
  71.                         match = 1;
  72.                 }
  73.                 else {
  74.                     if (!nocase) {
  75.                         if (pattern[0] == string[0])
  76.                             match = 1;
  77.                     } else {
  78.                         if (tolower((int)pattern[0]) == tolower((int)string[0]))
  79.                             match = 1;
  80.                     }
  81.                 }
  82.                 pattern++;
  83.                 patternLen--;
  84.             }
  85.             if (not)
  86.                 match = !match;
  87.             if (!match)
  88.                 return 0; /* no match */
  89.             string++;
  90.             stringLen--;
  91.             break;
  92.         }
  93.         case '\\':
  94.             if (patternLen >= 2) {
  95.                 pattern++;
  96.                 patternLen--;
  97.             }
  98.             /* fall through */
  99.         default:/*默认情况直接匹配*/
  100.             if (!nocase) {
  101.                 if (pattern[0] != string[0])
  102.                     return 0; /* no match */
  103.             } else {
  104.                 if (tolower((int)pattern[0]) != tolower((int)string[0]))
  105.                     return 0; /* no match */
  106.             }
  107.             string++;
  108.             stringLen--;
  109.             break;
  110.         }
  111.         pattern++;
  112.         patternLen--;
  113.         if (stringLen == 0) {
  114.             while(*pattern == '*') {
  115.                 pattern++;
  116.                 patternLen--;
  117.             }
  118.             break;
  119.         }
  120.     }
  121.     if (patternLen == 0 && stringLen == 0)
  122.         return 1;
  123.     return 0;
  124. }
接下来比较有意思的就是ll2string函数了,它是两位两位的除以100,而不是像想象的那样一位一位的除以10,并且它还将对应关系实现存在了一个数组里面,值得借鉴~~

点击(此处)折叠或打开

  1. int ll2string(char *dst, size_t dstlen, long long svalue) {
  2.     static const char digits[201] =
  3.         "0001020304050607080910111213141516171819"
  4.         "2021222324252627282930313233343536373839"
  5.         "4041424344454647484950515253545556575859"
  6.         "6061626364656667686970717273747576777879"
  7.         "8081828384858687888990919293949596979899";
  8.     int negative;
  9.     unsigned long long value;

  10.     /* The main loop works with 64bit unsigned integers for simplicity, so
  11.      * we convert the number here and remember if it is negative. */
  12.     if (svalue < 0) {
  13.         if (svalue != LLONG_MIN) {
  14.             value = -svalue;
  15.         } else {
  16.             value = ((unsigned long long) LLONG_MAX)+1;
  17.         }
  18.         negative = 1;
  19.     } else {
  20.         value = svalue;
  21.         negative = 0;
  22.     }

  23.     /* Check length. */
  24.     uint32_t const length = digits10(value)+negative;
  25.     if (length >= dstlen) return 0;

  26.     /* Null term. */
  27.     uint32_t next = length;
  28.     dst[next] = '\0';
  29.     next--;
  30.     while (value >= 100) {
  31.         int const i = (value % 100) * 2;
  32.         value /= 100;
  33.         dst[next] = digits[i + 1];
  34.         dst[next - 1] = digits[i];
  35.         next -= 2;
  36.     }

  37.     /* Handle last 1-2 digits. */
  38.     if (value < 10) {
  39.         dst[next] = '0' + (uint32_t) value;
  40.     } else {
  41.         int i = (uint32_t) value * 2;
  42.         dst[next] = digits[i + 1];
  43.         dst[next - 1] = digits[i];
  44.     }

  45.     /* Add sign. */
  46.     if (negative) dst[0] = '-';
  47.     return length;
  48. }
之后其他的string和double类型的互转都用到了atolsnprintf等库函数,这里暂且不表

阅读(4285) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~