]> granicus.if.org Git - php/commitdiff
Fixed bug #44929 - Better handling of leading zeros
authorRasmus Lerdorf <rasmus@php.net>
Wed, 8 Apr 2009 18:16:06 +0000 (18:16 +0000)
committerRasmus Lerdorf <rasmus@php.net>
Wed, 8 Apr 2009 18:16:06 +0000 (18:16 +0000)
ext/standard/strnatcmp.c
ext/standard/tests/array/bug44929.phpt [new file with mode: 0644]

index e1f491a3df48b8f743d65b70362509ad99ad0dde..dbed3c71cc50e6d8d456603e93f4dcbe1c4ebe7d 100644 (file)
@@ -116,10 +116,10 @@ PHPAPI int strnatcmp_ex(char const *a, size_t a_len, char const *b, size_t b_len
                ca = *ap; cb = *bp;
 
                /* skip over leading spaces or zeros */
-               while (isspace((int)(unsigned char)ca))
+               while (isspace((int)(unsigned char)ca) || (ca == '0' && ap+1 < aend))
                        ca = *++ap;
 
-               while (isspace((int)(unsigned char)cb))
+               while (isspace((int)(unsigned char)cb) || (cb == '0' && bp+1 < bend))
                        cb = *++bp;
 
                /* process run of digits */
diff --git a/ext/standard/tests/array/bug44929.phpt b/ext/standard/tests/array/bug44929.phpt
new file mode 100644 (file)
index 0000000..ae71e06
--- /dev/null
@@ -0,0 +1,31 @@
+--TEST--
+Bug #44929 (natsort doesn't handle leading zeros well)
+--FILE--
+<?php
+$a = array('001','008','005','00011','03','000014','-123','0.002','00','0');
+natsort($a);
+var_dump($a);
+?>
+--EXPECT--
+array(10) {
+  [6]=>
+  string(4) "-123"
+  [7]=>
+  string(5) "0.002"
+  [8]=>
+  string(2) "00"
+  [9]=>
+  string(1) "0"
+  [0]=>
+  string(3) "001"
+  [4]=>
+  string(2) "03"
+  [2]=>
+  string(3) "005"
+  [1]=>
+  string(3) "008"
+  [3]=>
+  string(5) "00011"
+  [5]=>
+  string(6) "000014"
+}