- 论坛徽章:
- 0
|
LZ,在你看的那个buffer.c文件中还有这个函数:
- /* simple-assumption:
- *
- * most parts are equal and doing a case conversion needs time
- *
- */
- int buffer_caseless_compare(const char *a, size_t a_len, const char *b, size_t b_len) {
- size_t ndx = 0, max_ndx;
- size_t *al, *bl;
- size_t mask = sizeof(*al) - 1;
- // 首先将需要进行比较的字符数组的char转换成size_t类型
- // 因为size_t类型比char类型有更多的字节数, 所以每次进行
- // 比较都可以比较更多的字节
- al = (size_t *)a;
- bl = (size_t *)b;
- /* is the alignment correct ? */
- if ( ((size_t)al & mask) == 0 &&
- ((size_t)bl & mask) == 0 ) {
- max_ndx = ((a_len < b_len) ? a_len : b_len) & ~mask;
- for (; ndx < max_ndx; ndx += sizeof(*al)) {
- // 一旦不相同就退出循环
- if (*al != *bl)
- break;
- al++; bl++;
- }
- }
- // 从不相同的地方开始比较
- a = (char *)al;
- b = (char *)bl;
- max_ndx = ((a_len < b_len) ? a_len : b_len);
- for (; ndx < max_ndx; ndx++) {
- char a1 = *a++, b1 = *b++;
- if (a1 != b1) {
- if ((a1 >= 'A' && a1 <= 'Z') && (b1 >= 'a' && b1 <= 'z'))
- a1 |= 32;
- else if ((a1 >= 'a' && a1 <= 'z') && (b1 >= 'A' && b1 <= 'Z'))
- b1 |= 32;
- // 不相等可以返回了
- if ((a1 - b1) != 0) return (a1 - b1);
- }
- }
- /* all chars are the same, and the length match too
- *
- * they are the same */
- // 如果到了这里两个仍然是相等的
- // 而且两者长度相等, 那么返回0
- if (a_len == b_len)
- return 0;
- /* if a is shorter then b, then b is larger */
- // 否则两者长度不等
- return (a_len - b_len);
- }
复制代码 |
|