php natsort內(nèi)核函數(shù)淺析第2/2頁
/* {{{ 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ù)中的:
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'不會參與比較,比較的結(jié)果應(yīng)該和
http://us.php.net/manual/en/function.natsort.php
http://sourcefrog.net/projects/natsort/example-out.txt
所說的一樣,但是在我的php5.2.9中對于“0”的處理結(jié)果卻不一樣(例如“img002.png”與“img1.png”,我的理解應(yīng)該是前者大于后者,不過在我的5.2.9中卻是前者小于后者),原因還沒想清楚,可能是5.2.9的一個bug,也可能是自己還沒有理解清楚源碼的意思。下次配置好環(huán)境再好好測試,好好消化~~
在array.c中有兩個重要的數(shù)據(jù)結(jié)構(gòu)很值得我們關(guān)注
- php in_array 函數(shù)使用說明與in_array需要注意的地方說明
- PHP數(shù)組的交集array_intersect(),array_intersect_assoc(),array_inter_key()函數(shù)的小問題
- php array_intersect比array_diff快(附詳細(xì)的使用說明)
- PHP內(nèi)核介紹及擴(kuò)展開發(fā)指南—基礎(chǔ)知識
- php數(shù)組函數(shù)序列之in_array() 查找數(shù)組值是否存在
- php數(shù)組函數(shù)序列之a(chǎn)rray_combine() - 數(shù)組合并函數(shù)使用說明
- php數(shù)組函數(shù)序列之in_array() - 查找數(shù)組中是否存在指定值
- php數(shù)組函數(shù)序列之a(chǎn)rray_intersect() 返回兩個或多個數(shù)組的交集數(shù)組
- 使用js判斷數(shù)組中是否包含某一元素(類似于php中的in_array())
- PHP內(nèi)核探索:變量存儲與類型使用說明
- PHP內(nèi)核探索:變量概述
- php內(nèi)核解析:PHP中的哈希表
- 2個自定義的PHP in_array 函數(shù),解決大量數(shù)據(jù)判斷in_array的效率問題
- php數(shù)組查找函數(shù)in_array()、array_search()、array_key_exists()使用實(shí)例
- PHP函數(shù)in_array()使用詳解
- php提示W(wǎng)arning:mysql_fetch_array() expects的解決方法
- PHP內(nèi)核探索:哈希表碰撞攻擊原理
- 深入理解PHP內(nèi)核(一)
- 深入理解PHP內(nèi)核(二)之SAPI探究
- 深入php內(nèi)核之php in array
相關(guān)文章
PHP學(xué)習(xí)之?dāng)?shù)組的定義和填充
先了解一下數(shù)組,數(shù)組就是把一組數(shù)據(jù)按順序放在一起。PHP的數(shù)組和其它的語言數(shù)組有一點(diǎn)點(diǎn)不同:第一,保存的數(shù)據(jù)是可以是任何類型的;第二,數(shù)組的索引可以是數(shù)字,也可以是字符串。2011-04-04
關(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生成指定隨機(jī)字符串的簡單實(shí)現(xiàn)方法
這篇文章主要介紹了PHP生成指定隨機(jī)字符串的簡單實(shí)現(xiàn)方法,涉及php操作數(shù)組與字符串的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04
ThinkPHP中公共函數(shù)路徑和配置項路徑的映射分析
這篇文章主要介紹了ThinkPHP中公共函數(shù)路徑和配置項路徑的映射,較為通俗的分析了ThinkPHP中公共函數(shù)路徑和配置項路徑的映射關(guān)系與對應(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)換的方法,實(shí)例封裝了一個類文件,可實(shí)現(xiàn)XML與MySQL數(shù)據(jù)的相互轉(zhuǎn)換,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12

