/src/php-src/ext/standard/strnatcmp.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | |
3 | | Modified for PHP by Andrei Zmievski <andrei@ispi.net> |
4 | | |
5 | | strnatcmp.c -- Perform 'natural order' comparisons of strings in C. |
6 | | Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au> |
7 | | |
8 | | This software is provided 'as-is', without any express or implied |
9 | | warranty. In no event will the authors be held liable for any damages |
10 | | arising from the use of this software. |
11 | | |
12 | | Permission is granted to anyone to use this software for any purpose, |
13 | | including commercial applications, and to alter it and redistribute it |
14 | | freely, subject to the following restrictions: |
15 | | |
16 | | 1. The origin of this software must not be misrepresented; you must not |
17 | | claim that you wrote the original software. If you use this software |
18 | | in a product, an acknowledgment in the product documentation would be |
19 | | appreciated but is not required. |
20 | | 2. Altered source versions must be plainly marked as such, and must not be |
21 | | misrepresented as being the original software. |
22 | | 3. This notice may not be removed or altered from any source distribution. |
23 | | */ |
24 | | |
25 | | #include <ctype.h> |
26 | | #include <string.h> |
27 | | #include <stdio.h> |
28 | | |
29 | | #include "php.h" |
30 | | #include "php_string.h" |
31 | | |
32 | | /* {{{ compare_right */ |
33 | | static int |
34 | | compare_right(char const **a, char const *aend, char const **b, char const *bend) |
35 | 5.61k | { |
36 | 5.61k | int bias = 0; |
37 | | |
38 | | /* The longest run of digits wins. That aside, the greatest |
39 | | value wins, but we can't know that it will until we've scanned |
40 | | both numbers to know that they have the same magnitude, so we |
41 | | remember it in BIAS. */ |
42 | 16.2k | for(;; (*a)++, (*b)++) { |
43 | 16.2k | if ((*a == aend || !isdigit((int)(unsigned char)**a)) && |
44 | 4.96k | (*b == bend || !isdigit((int)(unsigned char)**b))) |
45 | 4.15k | return bias; |
46 | 12.1k | else if (*a == aend || !isdigit((int)(unsigned char)**a)) |
47 | 817 | return -1; |
48 | 11.3k | else if (*b == bend || !isdigit((int)(unsigned char)**b)) |
49 | 643 | return +1; |
50 | 10.6k | else if (**a < **b) { |
51 | 2.58k | if (!bias) |
52 | 2.01k | bias = -1; |
53 | 8.08k | } else if (**a > **b) { |
54 | 1.22k | if (!bias) |
55 | 1.00k | bias = +1; |
56 | 1.22k | } |
57 | 16.2k | } |
58 | | |
59 | 0 | return 0; |
60 | 5.61k | } |
61 | | /* }}} */ |
62 | | |
63 | | /* {{{ compare_left */ |
64 | | static int |
65 | | compare_left(char const **a, char const *aend, char const **b, char const *bend) |
66 | 627 | { |
67 | | /* Compare two left-aligned numbers: the first to have a |
68 | | different value wins. */ |
69 | 683 | for(;; (*a)++, (*b)++) { |
70 | 683 | if ((*a == aend || !isdigit((int)(unsigned char)**a)) && |
71 | 56 | (*b == bend || !isdigit((int)(unsigned char)**b))) |
72 | 56 | return 0; |
73 | 627 | else if (*a == aend || !isdigit((int)(unsigned char)**a)) |
74 | 0 | return -1; |
75 | 627 | else if (*b == bend || !isdigit((int)(unsigned char)**b)) |
76 | 0 | return +1; |
77 | 627 | else if (**a < **b) |
78 | 371 | return -1; |
79 | 256 | else if (**a > **b) |
80 | 200 | return +1; |
81 | 683 | } |
82 | | |
83 | 0 | return 0; |
84 | 627 | } |
85 | | /* }}} */ |
86 | | |
87 | | /* {{{ strnatcmp_ex */ |
88 | | PHPAPI int strnatcmp_ex(char const *a, size_t a_len, char const *b, size_t b_len, int fold_case) |
89 | 8.88k | { |
90 | 8.88k | unsigned char ca, cb; |
91 | 8.88k | char const *ap, *bp; |
92 | 8.88k | char const *aend = a + a_len, |
93 | 8.88k | *bend = b + b_len; |
94 | 8.88k | int fractional, result; |
95 | 8.88k | short leading = 1; |
96 | | |
97 | 8.88k | if (a_len == 0 || b_len == 0) { |
98 | 95 | return (a_len == b_len ? 0 : (a_len > b_len ? 1 : -1)); |
99 | 100 | } |
100 | | |
101 | 8.78k | ap = a; |
102 | 8.78k | bp = b; |
103 | 9.98k | while (1) { |
104 | 9.98k | ca = *ap; cb = *bp; |
105 | | |
106 | | /* skip over leading zeros */ |
107 | 10.0k | while (leading && ca == '0' && (ap+1 < aend) && isdigit((int)(unsigned char)*(ap+1))) { |
108 | 92 | ca = *++ap; |
109 | 92 | } |
110 | | |
111 | 10.2k | while (leading && cb == '0' && (bp+1 < bend) && isdigit((int)(unsigned char)*(bp+1))) { |
112 | 262 | cb = *++bp; |
113 | 262 | } |
114 | | |
115 | 9.98k | leading = 0; |
116 | | |
117 | | /* Skip consecutive whitespace */ |
118 | 157 | while (isspace((int)(unsigned char)ca)) { |
119 | 157 | ca = *++ap; |
120 | 157 | } |
121 | | |
122 | 122 | while (isspace((int)(unsigned char)cb)) { |
123 | 122 | cb = *++bp; |
124 | 122 | } |
125 | | |
126 | | /* process run of digits */ |
127 | 9.98k | if (isdigit((int)(unsigned char)ca) && isdigit((int)(unsigned char)cb)) { |
128 | 6.23k | fractional = (ca == '0' || cb == '0'); |
129 | | |
130 | 6.23k | if (fractional) |
131 | 627 | result = compare_left(&ap, aend, &bp, bend); |
132 | 5.61k | else |
133 | 5.61k | result = compare_right(&ap, aend, &bp, bend); |
134 | | |
135 | 6.23k | if (result != 0) |
136 | 3.86k | return result; |
137 | 2.37k | else if (ap == aend && bp == bend) |
138 | | /* End of the strings. Let caller sort them out. */ |
139 | 1.65k | return 0; |
140 | 722 | else if (ap == aend) |
141 | 196 | return -1; |
142 | 526 | else if (bp == bend) |
143 | 15 | return 1; |
144 | 511 | else { |
145 | | /* Keep on comparing from the current point. */ |
146 | 511 | ca = *ap; cb = *bp; |
147 | 511 | } |
148 | 6.23k | } |
149 | | |
150 | 4.25k | if (fold_case) { |
151 | 3.91k | ca = toupper((int)(unsigned char)ca); |
152 | 3.91k | cb = toupper((int)(unsigned char)cb); |
153 | 3.91k | } |
154 | | |
155 | 4.25k | if (ca < cb) |
156 | 1.30k | return -1; |
157 | 2.95k | else if (ca > cb) |
158 | 1.42k | return +1; |
159 | | |
160 | 1.52k | ++ap; ++bp; |
161 | 1.52k | if (ap >= aend && bp >= bend) |
162 | | /* The strings compare the same. Perhaps the caller |
163 | | will want to call strcmp to break the tie. */ |
164 | 24 | return 0; |
165 | 1.50k | else if (ap >= aend) |
166 | 271 | return -1; |
167 | 1.23k | else if (bp >= bend) |
168 | 39 | return 1; |
169 | 1.52k | } |
170 | 8.78k | } |
171 | | /* }}} */ |