1 /* -*- mode: c; c-file-style: "k&r" -*-
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 <assert.h>
28 #include <stdio.h>
29
30 #include "php.h"
31 #include "php_string.h"
32
33 /* {{{ compare_right
34 */
35 static int
compare_right(char const ** a,char const * aend,char const ** b,char const * bend)36 compare_right(char const **a, char const *aend, char const **b, char const *bend)
37 {
38 int bias = 0;
39
40 /* The longest run of digits wins. That aside, the greatest
41 value wins, but we can't know that it will until we've scanned
42 both numbers to know that they have the same magnitude, so we
43 remember it in BIAS. */
44 for(;; (*a)++, (*b)++) {
45 if ((*a == aend || !isdigit((int)(unsigned char)**a)) &&
46 (*b == bend || !isdigit((int)(unsigned char)**b)))
47 return bias;
48 else if (*a == aend || !isdigit((int)(unsigned char)**a))
49 return -1;
50 else if (*b == bend || !isdigit((int)(unsigned char)**b))
51 return +1;
52 else if (**a < **b) {
53 if (!bias)
54 bias = -1;
55 } else if (**a > **b) {
56 if (!bias)
57 bias = +1;
58 }
59 }
60
61 return 0;
62 }
63 /* }}} */
64
65 /* {{{ compare_left
66 */
67 static int
compare_left(char const ** a,char const * aend,char const ** b,char const * bend)68 compare_left(char const **a, char const *aend, char const **b, char const *bend)
69 {
70 /* Compare two left-aligned numbers: the first to have a
71 different value wins. */
72 for(;; (*a)++, (*b)++) {
73 if ((*a == aend || !isdigit((int)(unsigned char)**a)) &&
74 (*b == bend || !isdigit((int)(unsigned char)**b)))
75 return 0;
76 else if (*a == aend || !isdigit((int)(unsigned char)**a))
77 return -1;
78 else if (*b == bend || !isdigit((int)(unsigned char)**b))
79 return +1;
80 else if (**a < **b)
81 return -1;
82 else if (**a > **b)
83 return +1;
84 }
85
86 return 0;
87 }
88 /* }}} */
89
90 /* {{{ strnatcmp_ex
91 */
strnatcmp_ex(char const * a,size_t a_len,char const * b,size_t b_len,int fold_case)92 PHPAPI int strnatcmp_ex(char const *a, size_t a_len, char const *b, size_t b_len, int fold_case)
93 {
94 unsigned char ca, cb;
95 char const *ap, *bp;
96 char const *aend = a + a_len,
97 *bend = b + b_len;
98 int fractional, result;
99 short leading = 1;
100
101 if (a_len == 0 || b_len == 0) {
102 return (a_len == b_len ? 0 : (a_len > b_len ? 1 : -1));
103 }
104
105 ap = a;
106 bp = b;
107 while (1) {
108 ca = *ap; cb = *bp;
109
110 /* skip over leading zeros */
111 while (leading && ca == '0' && (ap+1 < aend) && isdigit((int)(unsigned char)*(ap+1))) {
112 ca = *++ap;
113 }
114
115 while (leading && cb == '0' && (bp+1 < bend) && isdigit((int)(unsigned char)*(bp+1))) {
116 cb = *++bp;
117 }
118
119 leading = 0;
120
121 /* Skip consecutive whitespace */
122 while (isspace((int)(unsigned char)ca)) {
123 ca = *++ap;
124 }
125
126 while (isspace((int)(unsigned char)cb)) {
127 cb = *++bp;
128 }
129
130 /* process run of digits */
131 if (isdigit((int)(unsigned char)ca) && isdigit((int)(unsigned char)cb)) {
132 fractional = (ca == '0' || cb == '0');
133
134 if (fractional)
135 result = compare_left(&ap, aend, &bp, bend);
136 else
137 result = compare_right(&ap, aend, &bp, bend);
138
139 if (result != 0)
140 return result;
141 else if (ap == aend && bp == bend)
142 /* End of the strings. Let caller sort them out. */
143 return 0;
144 else if (ap == aend)
145 return -1;
146 else if (bp == bend)
147 return 1;
148 else {
149 /* Keep on comparing from the current point. */
150 ca = *ap; cb = *bp;
151 }
152 }
153
154 if (fold_case) {
155 ca = toupper((int)(unsigned char)ca);
156 cb = toupper((int)(unsigned char)cb);
157 }
158
159 if (ca < cb)
160 return -1;
161 else if (ca > cb)
162 return +1;
163
164 ++ap; ++bp;
165 if (ap >= aend && bp >= bend)
166 /* The strings compare the same. Perhaps the caller
167 will want to call strcmp to break the tie. */
168 return 0;
169 else if (ap >= aend)
170 return -1;
171 else if (bp >= bend)
172 return 1;
173 }
174 }
175 /* }}} */
176
177 /*
178 * Local variables:
179 * tab-width: 4
180 * c-basic-offset: 4
181 * End:
182 * vim600: sw=4 ts=4 fdm=marker
183 * vim<600: sw=4 ts=4
184 */
185