/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 | 4.93k | { |
36 | 4.93k | 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 | 30.5k | for(;; (*a)++, (*b)++) { |
43 | 30.5k | if ((*a == aend || !isdigit((int)(unsigned char)**a)) && |
44 | 30.5k | (*b == bend || !isdigit((int)(unsigned char)**b))) |
45 | 4.16k | return bias; |
46 | 26.4k | else if (*a == aend || !isdigit((int)(unsigned char)**a)) |
47 | 235 | return -1; |
48 | 26.1k | else if (*b == bend || !isdigit((int)(unsigned char)**b)) |
49 | 535 | return +1; |
50 | 25.6k | else if (**a < **b) { |
51 | 1.89k | if (!bias) |
52 | 529 | bias = -1; |
53 | 23.7k | } else if (**a > **b) { |
54 | 1.96k | if (!bias) |
55 | 831 | bias = +1; |
56 | 1.96k | } |
57 | 30.5k | } |
58 | | |
59 | 0 | return 0; |
60 | 4.93k | } |
61 | | /* }}} */ |
62 | | |
63 | | /* {{{ compare_left */ |
64 | | static int |
65 | | compare_left(char const **a, char const *aend, char const **b, char const *bend) |
66 | 3.24k | { |
67 | | /* Compare two left-aligned numbers: the first to have a |
68 | | different value wins. */ |
69 | 21.4k | for(;; (*a)++, (*b)++) { |
70 | 21.4k | if ((*a == aend || !isdigit((int)(unsigned char)**a)) && |
71 | 21.4k | (*b == bend || !isdigit((int)(unsigned char)**b))) |
72 | 2.08k | return 0; |
73 | 19.3k | else if (*a == aend || !isdigit((int)(unsigned char)**a)) |
74 | 113 | return -1; |
75 | 19.2k | else if (*b == bend || !isdigit((int)(unsigned char)**b)) |
76 | 196 | return +1; |
77 | 19.0k | else if (**a < **b) |
78 | 301 | return -1; |
79 | 18.7k | else if (**a > **b) |
80 | 547 | return +1; |
81 | 21.4k | } |
82 | | |
83 | 0 | return 0; |
84 | 3.24k | } |
85 | | /* }}} */ |
86 | | |
87 | | /* {{{ strnatcmp_ex */ |
88 | | PHPAPI int strnatcmp_ex(char const *a, size_t a_len, char const *b, size_t b_len, bool is_case_insensitive) |
89 | 35.5k | { |
90 | 35.5k | unsigned char ca, cb; |
91 | 35.5k | char const *ap, *bp; |
92 | 35.5k | char const *aend = a + a_len, |
93 | 35.5k | *bend = b + b_len; |
94 | 35.5k | int fractional, result; |
95 | | |
96 | 35.5k | if (a_len == 0 || b_len == 0) { |
97 | 953 | return (a_len == b_len ? 0 : (a_len > b_len ? 1 : -1)); |
98 | 953 | } |
99 | | |
100 | 34.6k | ap = a; |
101 | 34.6k | bp = b; |
102 | | |
103 | 34.6k | ca = *ap; cb = *bp; |
104 | | |
105 | | /* skip over leading zeros */ |
106 | 34.7k | while (ca == '0' && (ap+1 < aend) && isdigit((int)(unsigned char)*(ap+1))) { |
107 | 83 | ca = *++ap; |
108 | 83 | } |
109 | | |
110 | 34.6k | while (cb == '0' && (bp+1 < bend) && isdigit((int)(unsigned char)*(bp+1))) { |
111 | 75 | cb = *++bp; |
112 | 75 | } |
113 | | |
114 | 431k | while (1) { |
115 | | |
116 | | /* Skip consecutive whitespace */ |
117 | 431k | while (isspace((int)(unsigned char)ca)) { |
118 | 44.5k | ca = *++ap; |
119 | 44.5k | } |
120 | | |
121 | 431k | while (isspace((int)(unsigned char)cb)) { |
122 | 45.0k | cb = *++bp; |
123 | 45.0k | } |
124 | | |
125 | | /* process run of digits */ |
126 | 431k | if (isdigit((int)(unsigned char)ca) && isdigit((int)(unsigned char)cb)) { |
127 | 8.17k | fractional = (ca == '0' || cb == '0'); |
128 | | |
129 | 8.17k | if (fractional) |
130 | 3.24k | result = compare_left(&ap, aend, &bp, bend); |
131 | 4.93k | else |
132 | 4.93k | result = compare_right(&ap, aend, &bp, bend); |
133 | | |
134 | 8.17k | if (result != 0) |
135 | 2.80k | return result; |
136 | 5.36k | else if (ap == aend && bp == bend) |
137 | | /* End of the strings. Let caller sort them out. */ |
138 | 364 | return 0; |
139 | 5.00k | else if (ap == aend) |
140 | 11 | return -1; |
141 | 4.99k | else if (bp == bend) |
142 | 10 | return 1; |
143 | 4.98k | else { |
144 | | /* Keep on comparing from the current point. */ |
145 | 4.98k | ca = *ap; cb = *bp; |
146 | 4.98k | } |
147 | 8.17k | } |
148 | | |
149 | 428k | if (is_case_insensitive) { |
150 | 0 | ca = toupper((int)(unsigned char)ca); |
151 | 0 | cb = toupper((int)(unsigned char)cb); |
152 | 0 | } |
153 | | |
154 | 428k | if (ca < cb) |
155 | 7.51k | return -1; |
156 | 420k | else if (ca > cb) |
157 | 16.2k | return +1; |
158 | | |
159 | 404k | ++ap; ++bp; |
160 | 404k | if (ap >= aend && bp >= bend) |
161 | | /* The strings compare the same. Perhaps the caller |
162 | | will want to call strcmp to break the tie. */ |
163 | 6.02k | return 0; |
164 | 398k | else if (ap >= aend) |
165 | 442 | return -1; |
166 | 398k | else if (bp >= bend) |
167 | 1.22k | return 1; |
168 | | |
169 | 396k | ca = *ap; cb = *bp; |
170 | 396k | } |
171 | 34.6k | } |
172 | | /* }}} */ |