xref: /php-src/ext/standard/strnatcmp.c (revision ef287bfc)
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
compare_right(char const ** a,char const * aend,char const ** b,char const * bend)34 compare_right(char const **a, char const *aend, char const **b, char const *bend)
35 {
36 	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 	for(;; (*a)++, (*b)++) {
43 		if ((*a == aend || !isdigit((int)(unsigned char)**a)) &&
44 			(*b == bend || !isdigit((int)(unsigned char)**b)))
45 			return bias;
46 		else if (*a == aend || !isdigit((int)(unsigned char)**a))
47 			return -1;
48 		else if (*b == bend || !isdigit((int)(unsigned char)**b))
49 			return +1;
50 		else if (**a < **b) {
51 			if (!bias)
52 				bias = -1;
53 		} else if (**a > **b) {
54 			if (!bias)
55 				bias = +1;
56 		}
57 	}
58 
59 	return 0;
60 }
61 /* }}} */
62 
63 /* {{{ compare_left */
64 static int
compare_left(char const ** a,char const * aend,char const ** b,char const * bend)65 compare_left(char const **a, char const *aend, char const **b, char const *bend)
66 {
67 	/* Compare two left-aligned numbers: the first to have a
68 	   different value wins. */
69 	for(;; (*a)++, (*b)++) {
70 		if ((*a == aend || !isdigit((int)(unsigned char)**a)) &&
71 			(*b == bend || !isdigit((int)(unsigned char)**b)))
72 			return 0;
73 		else if (*a == aend || !isdigit((int)(unsigned char)**a))
74 			return -1;
75 		else if (*b == bend || !isdigit((int)(unsigned char)**b))
76 			return +1;
77 		 else if (**a < **b)
78 			 return -1;
79 		 else if (**a > **b)
80 			 return +1;
81 	}
82 
83 	return 0;
84 }
85 /* }}} */
86 
87 /* {{{ strnatcmp_ex */
strnatcmp_ex(char const * a,size_t a_len,char const * b,size_t b_len,bool is_case_insensitive)88 PHPAPI int strnatcmp_ex(char const *a, size_t a_len, char const *b, size_t b_len, bool is_case_insensitive)
89 {
90 	unsigned char ca, cb;
91 	char const *ap, *bp;
92 	char const *aend = a + a_len,
93 			   *bend = b + b_len;
94 	int fractional, result;
95 
96 	if (a_len == 0 || b_len == 0) {
97 		return (a_len == b_len ? 0 : (a_len > b_len ? 1 : -1));
98 	}
99 
100 	ap = a;
101 	bp = b;
102 
103 	ca = *ap; cb = *bp;
104 
105 	/* skip over leading zeros */
106 	while (ca == '0' && (ap+1 < aend) && isdigit((int)(unsigned char)*(ap+1))) {
107 		ca = *++ap;
108 	}
109 
110 	while (cb == '0' && (bp+1 < bend) && isdigit((int)(unsigned char)*(bp+1))) {
111 		cb = *++bp;
112 	}
113 
114 	while (1) {
115 
116 		/* Skip consecutive whitespace */
117 		while (isspace((int)(unsigned char)ca)) {
118 			ca = *++ap;
119 		}
120 
121 		while (isspace((int)(unsigned char)cb)) {
122 			cb = *++bp;
123 		}
124 
125 		/* process run of digits */
126 		if (isdigit((int)(unsigned char)ca)  &&  isdigit((int)(unsigned char)cb)) {
127 			fractional = (ca == '0' || cb == '0');
128 
129 			if (fractional)
130 				result = compare_left(&ap, aend, &bp, bend);
131 			else
132 				result = compare_right(&ap, aend, &bp, bend);
133 
134 			if (result != 0)
135 				return result;
136 			else if (ap == aend && bp == bend)
137 				/* End of the strings. Let caller sort them out. */
138 				return 0;
139 			else if (ap == aend)
140 				return -1;
141 			else if (bp == bend)
142 				return 1;
143 			else {
144 				/* Keep on comparing from the current point. */
145 				ca = *ap; cb = *bp;
146 			}
147 		}
148 
149 		if (is_case_insensitive) {
150 			ca = toupper((int)(unsigned char)ca);
151 			cb = toupper((int)(unsigned char)cb);
152 		}
153 
154 		if (ca < cb)
155 			return -1;
156 		else if (ca > cb)
157 			return +1;
158 
159 		++ap; ++bp;
160 		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 			return 0;
164 		else if (ap >= aend)
165 			return -1;
166 		else if (bp >= bend)
167 			return 1;
168 
169 		ca = *ap; cb = *bp;
170 	}
171 }
172 /* }}} */
173