最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php natsort內(nèi)核函數(shù)淺析第2/2頁(yè)

 更新時(shí)間:2009年08月10日 10:32:08   作者:  
今天發(fā)現(xiàn)了php有個(gè)自然排序的函數(shù)----natsort,第一次聽說了原來還有一種叫做“自然排序”的算法,很好奇

復(fù)制代碼 代碼如下:

/* {{{ 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函數(shù)中的:
復(fù)制代碼 代碼如下:

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;

所以,我覺得應(yīng)該字符串(當(dāng)前位置開始)中前面的空字符和數(shù)字前面的‘0'不會(huì)參與比較,比較的結(jié)果應(yīng)該和

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

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

所說的一樣,但是在我的php5.2.9中對(duì)于“0”的處理結(jié)果卻不一樣(例如“img002.png”與“img1.png”,我的理解應(yīng)該是前者大于后者,不過在我的5.2.9中卻是前者小于后者),原因還沒想清楚,可能是5.2.9的一個(gè)bug,也可能是自己還沒有理解清楚源碼的意思。下次配置好環(huán)境再好好測(cè)試,好好消化~~

在array.c中有兩個(gè)重要的數(shù)據(jù)結(jié)構(gòu)很值得我們關(guān)注

相關(guān)文章

  • PHP學(xué)習(xí)之?dāng)?shù)組的定義和填充

    PHP學(xué)習(xí)之?dāng)?shù)組的定義和填充

    先了解一下數(shù)組,數(shù)組就是把一組數(shù)據(jù)按順序放在一起。PHP的數(shù)組和其它的語(yǔ)言數(shù)組有一點(diǎn)點(diǎn)不同:第一,保存的數(shù)據(jù)是可以是任何類型的;第二,數(shù)組的索引可以是數(shù)字,也可以是字符串。
    2011-04-04
  • 一些php技巧與注意事項(xiàng)分析

    一些php技巧與注意事項(xiàng)分析

    很多人寫程序時(shí),用 header(location) 進(jìn)行跳轉(zhuǎn)往往不記得寫 exit() 語(yǔ)句,事實(shí)上這種做法是存在嚴(yán)重風(fēng)險(xiǎn)的。
    2011-02-02
  • php接口報(bào)錯(cuò)解決分析記錄

    php接口報(bào)錯(cuò)解決分析記錄

    記一次解決php接口報(bào)錯(cuò) The GET method is not supported for this route. Supported methods: POST.的bug,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • PHP使用內(nèi)置函數(shù)生成圖片的方法詳解

    PHP使用內(nèi)置函數(shù)生成圖片的方法詳解

    這篇文章主要介紹了PHP使用內(nèi)置函數(shù)生成圖片的方法,結(jié)合實(shí)例形式詳細(xì)分析了php生成圖片的步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-05-05
  • 關(guān)于PHP數(shù)組迭代器的使用方法實(shí)例

    關(guān)于PHP數(shù)組迭代器的使用方法實(shí)例

    在PHP的日常操作中,數(shù)組是最常出現(xiàn)的結(jié)構(gòu),而我們幾乎每天都在處理數(shù)組相關(guān)的內(nèi)容,這篇文章主要給大家介紹了關(guān)于PHP數(shù)組迭代器的使用方法,需要的朋友可以參考下
    2021-11-11
  • PHP使用CURL模擬登錄的方法

    PHP使用CURL模擬登錄的方法

    本文給大家介紹的是PHP使用CURL模擬登錄的方法,思路和其他模擬登陸的程序不同,有需要的小伙伴可以詳細(xì)看下。
    2015-07-07
  • PHP生成指定隨機(jī)字符串的簡(jiǎn)單實(shí)現(xiàn)方法

    PHP生成指定隨機(jī)字符串的簡(jiǎn)單實(shí)現(xiàn)方法

    這篇文章主要介紹了PHP生成指定隨機(jī)字符串的簡(jiǎn)單實(shí)現(xiàn)方法,涉及php操作數(shù)組與字符串的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • PHP中錯(cuò)誤與異常的日志記錄用法分析

    PHP中錯(cuò)誤與異常的日志記錄用法分析

    這篇文章主要介紹了PHP中錯(cuò)誤與異常的日志記錄用法,較為詳細(xì)的分析了php中錯(cuò)誤與異常的區(qū)別以及日志記錄的相應(yīng)使用技巧,需要的朋友可以參考下
    2016-08-08
  • ThinkPHP中公共函數(shù)路徑和配置項(xiàng)路徑的映射分析

    ThinkPHP中公共函數(shù)路徑和配置項(xiàng)路徑的映射分析

    這篇文章主要介紹了ThinkPHP中公共函數(shù)路徑和配置項(xiàng)路徑的映射,較為通俗的分析了ThinkPHP中公共函數(shù)路徑和配置項(xiàng)路徑的映射關(guān)系與對(duì)應(yīng)修改位置,有助于更好的理解ThinkPHP底層代碼原理,需要的朋友可以參考下
    2014-11-11
  • php中實(shí)現(xiàn)xml與mysql數(shù)據(jù)相互轉(zhuǎn)換的方法

    php中實(shí)現(xiàn)xml與mysql數(shù)據(jù)相互轉(zhuǎn)換的方法

    這篇文章主要介紹了php中實(shí)現(xiàn)xml與mysql數(shù)據(jù)相互轉(zhuǎn)換的方法,實(shí)例封裝了一個(gè)類文件,可實(shí)現(xiàn)XML與MySQL數(shù)據(jù)的相互轉(zhuǎn)換,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2014-12-12

最新評(píng)論

东乌| 巩留县| 石首市| 和林格尔县| 班戈县| 郸城县| 安化县| 黄浦区| 抚松县| 日土县| 重庆市| 修水县| 乌兰浩特市| 桐庐县| 弥渡县| 上虞市| 左云县| 武义县| 湖北省| 商洛市| 静安区| 讷河市| 民权县| 宜川县| 治县。| 和平县| 渑池县| 乐陵市| 东山县| 湖口县| 安达市| 明星| 凤山县| 东阿县| 石家庄市| 始兴县| 荔波县| 元阳县| 高雄市| 博野县| 武平县|