php natsort内核函数浅析第2/2页

 更新时间:2009年08月10日 10:32:08   作者:  
今天发现了php有个自然排序的函数----natsort,第一次听说了原来还有一种叫做“自然排序”的算法,很好奇

复制代码 代码如下:

/* {{{ compare_right
*/
static int
compare_right(char const **a, char const *aend, char const **b, char const *bend)
{
    int bias = 0;
    /* The longest run of digits wins. That aside, the greatest
     value wins, but we can't know that it will until we've scanned
     both numbers to know that they have the same magnitude, so we
     remember it in BIAS. */
    for(;; (*a)++, (*b)++) {
        if ((*a == aend || !isdigit((int)(unsigned char)**a)) &&
            (*b == bend || !isdigit((int)(unsigned char)**b)))
            return bias;
        else if (*a == aend || !isdigit((int)(unsigned char)**a))
            return -1;
        else if (*b == bend || !isdigit((int)(unsigned char)**b))
            return +1;
        else if (**a < **b) {
            if (!bias)
                bias = -1;
        } else if (**a > **b) {
            if (!bias)
                bias = +1;
        }
}
return 0;
}
/* }}} */
/* {{{ compare_left
*/
static int
compare_left(char const **a, char const *aend, char const **b, char const *bend)
{
/* Compare two left-aligned numbers: the first to have a
different value wins. */
    for(;; (*a)++, (*b)++) {
        if ((*a == aend || !isdigit((int)(unsigned char)**a)) &&
            (*b == bend || !isdigit((int)(unsigned char)**b)))
            return 0;
        else if (*a == aend || !isdigit((int)(unsigned char)**a))
            return -1;
        else if (*b == bend || !isdigit((int)(unsigned char)**b))
            return +1;
         else if (**a < **b)
             return -1;
         else if (**a > **b)
             return +1;
}

return 0;
}
/* }}} */
/* {{{ strnatcmp_ex
* call in array.c: strnatcmp_ex(Z_STRVAL(first), Z_STRLEN(first), Z_STRVAL(second), Z_STRLEN(second), fold_case);
*/
PHPAPI int strnatcmp_ex(char const *a, size_t a_len, char const *b, size_t b_len, int fold_case)
{
    char ca, cb;
    char const *ap, *bp;
    char const *aend = a + a_len,
             *bend = b + b_len;
    int fractional, result;
    if (a_len == 0 || b_len == 0)
        return a_len - b_len;
    ap = a;
    bp = b;
    while (1) {
        ca = *ap; cb = *bp;
        /* skip over leading spaces or zeros */
        while (isspace((int)(unsigned char)ca) || (ca == '0' && (ap+1 < aend) && (*(ap+1)!='.')))
            ca = *++ap;
        while (isspace((int)(unsigned char)cb) || (cb == '0' && (bp+1 < bend) && (*(bp+1)!='.')))
            cb = *++bp;
        /* process run of digits */
        if (isdigit((int)(unsigned char)ca) && isdigit((int)(unsigned char)cb)) {
            fractional = (ca == '0' || cb == '0');
            if (fractional)
                result = compare_left(&ap, aend, &bp, bend);
            else
                result = compare_right(&ap, aend, &bp, bend);
            if (result != 0)
                return result;
            else if (ap == aend && bp == bend)
                /* End of the strings. Let caller sort them out. */
                return 0;
            else {
                /* Keep on comparing from the current point. */
                ca = *ap; cb = *bp;
            }
        }
        if (fold_case) {
            ca = toupper((int)(unsigned char)ca);
            cb = toupper((int)(unsigned char)cb);
        }
        if (ca < cb)
            return -1;
        else if (ca > cb)
            return +1;
        ++ap; ++bp;
        if (ap >= aend && bp >= bend)
            /* The strings compare the same. Perhaps the caller
             will want to call strcmp to break the tie. */
            return 0;
        else if (ap >= aend)
            return -1;
        else if (bp >= bend)
            return 1;
    }
}
/* }}} */

从strnatcmp_ex函数中的:
复制代码 代码如下:

while (isspace((int)(unsigned char)ca) || (ca == '0' && (ap+1 < aend) && (*(ap+1)!='.')))
    ca = *++ap;
while (isspace((int)(unsigned char)cb) || (cb == '0' && (bp+1 < bend) && (*(bp+1)!='.')))
    cb = *++bp;

所以,我觉得应该字符串(当前位置开始)中前面的空字符和数字前面的‘0'不会参与比较,比较的结果应该和

http://us.php.net/manual/en/function.natsort.php

http://sourcefrog.net/projects/natsort/example-out.txt

所说的一样,但是在我的php5.2.9中对于“0”的处理结果却不一样(例如“img002.png”与“img1.png”,我的理解应该是前者大于后者,不过在我的5.2.9中却是前者小于后者),原因还没想清楚,可能是5.2.9的一个bug,也可能是自己还没有理解清楚源码的意思。下次配置好环境再好好测试,好好消化~~

在array.c中有两个重要的数据结构很值得我们关注

相关文章

  • PHP学习之数组的定义和填充

    PHP学习之数组的定义和填充

    先了解一下数组,数组就是把一组数据按顺序放在一起。PHP的数组和其它的语言数组有一点点不同:第一,保存的数据是可以是任何类型的;第二,数组的索引可以是数字,也可以是字符串。
    2011-04-04
  • 一些php技巧与注意事项分析

    一些php技巧与注意事项分析

    很多人写程序时,用 header(location) 进行跳转往往不记得写 exit() 语句,事实上这种做法是存在严重风险的。
    2011-02-02
  • php接口报错解决分析记录

    php接口报错解决分析记录

    记一次解决php接口报错 The GET method is not supported for this route. Supported methods: POST.的bug,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • PHP使用内置函数生成图片的方法详解

    PHP使用内置函数生成图片的方法详解

    这篇文章主要介绍了PHP使用内置函数生成图片的方法,结合实例形式详细分析了php生成图片的步骤与相关实现技巧,需要的朋友可以参考下
    2016-05-05
  • 关于PHP数组迭代器的使用方法实例

    关于PHP数组迭代器的使用方法实例

    在PHP的日常操作中,数组是最常出现的结构,而我们几乎每天都在处理数组相关的内容,这篇文章主要给大家介绍了关于PHP数组迭代器的使用方法,需要的朋友可以参考下
    2021-11-11
  • PHP使用CURL模拟登录的方法

    PHP使用CURL模拟登录的方法

    本文给大家介绍的是PHP使用CURL模拟登录的方法,思路和其他模拟登陆的程序不同,有需要的小伙伴可以详细看下。
    2015-07-07
  • PHP生成指定随机字符串的简单实现方法

    PHP生成指定随机字符串的简单实现方法

    这篇文章主要介绍了PHP生成指定随机字符串的简单实现方法,涉及php操作数组与字符串的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-04-04
  • PHP中错误与异常的日志记录用法分析

    PHP中错误与异常的日志记录用法分析

    这篇文章主要介绍了PHP中错误与异常的日志记录用法,较为详细的分析了php中错误与异常的区别以及日志记录的相应使用技巧,需要的朋友可以参考下
    2016-08-08
  • ThinkPHP中公共函数路径和配置项路径的映射分析

    ThinkPHP中公共函数路径和配置项路径的映射分析

    这篇文章主要介绍了ThinkPHP中公共函数路径和配置项路径的映射,较为通俗的分析了ThinkPHP中公共函数路径和配置项路径的映射关系与对应修改位置,有助于更好的理解ThinkPHP底层代码原理,需要的朋友可以参考下
    2014-11-11
  • php中实现xml与mysql数据相互转换的方法

    php中实现xml与mysql数据相互转换的方法

    这篇文章主要介绍了php中实现xml与mysql数据相互转换的方法,实例封装了一个类文件,可实现XML与MySQL数据的相互转换,具有一定的参考借鉴价值,需要的朋友可以参考下
    2014-12-12

最新评论