Actually apply the patch...thanks again to Bryan Vandyke (and Tom Ross for notifying...
[kugel-rb.git] / firmware / common / strnatcmp.c
blob2ec920b0a156e23e188b4fed19cd6a4cbabd2f72
1 /* Based on:
2 strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
3 Copyright (C) 2000, 2004 by Martin Pool <mbp sourcefrog net>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
23 /* partial change history:
25 * 2004-10-10 mbp: Lift out character type dependencies into macros.
27 * Eric Sosman pointed out that ctype functions take a parameter whose
28 * value must be that of an unsigned int, even on platforms that have
29 * negative chars in their default char type.
32 * This version is changed to ignore leading zeros, it does not equal to the
33 * original software.
36 #include <ctype.h>
37 #include <string.h>
38 #include <stdio.h>
40 #include "strnatcmp.h"
42 #define assert(x) /* nothing */
45 /* These are defined as macros to make it easier to adapt this code to
46 * different characters types or comparison functions. */
47 static inline int
48 nat_isdigit(char a)
50 return isdigit((unsigned char) a);
54 static inline int
55 nat_isspace(char a)
57 return a == '0' || isspace((unsigned char) a);
61 static inline char
62 nat_toupper(char a)
64 return toupper((unsigned char) a);
69 static int
70 compare_right(char const *a, char const *b)
72 int bias = 0;
74 /* The longest run of digits wins. That aside, the greatest
75 value wins, but we can't know that it will until we've scanned
76 both numbers to know that they have the same magnitude, so we
77 remember it in BIAS. */
78 for (;; a++, b++) {
79 if (!nat_isdigit(*a) && !nat_isdigit(*b))
80 return bias;
81 else if (!nat_isdigit(*a))
82 return -1;
83 else if (!nat_isdigit(*b))
84 return +1;
85 else if (*a < *b) {
86 if (!bias)
87 bias = -1;
88 } else if (*a > *b) {
89 if (!bias)
90 bias = +1;
91 } else if (!*a && !*b)
92 return bias;
95 return 0;
98 static int strnatcmp0(char const *a, char const *b, int fold_case)
100 int ai, bi;
101 char ca, cb;
102 int result;
104 assert(a && b);
105 ai = bi = 0;
106 while (1) {
107 ca = a[ai]; cb = b[bi];
109 /* skip over leading spaces or zeros */
110 while (nat_isspace(ca))
111 ca = a[++ai];
113 while (nat_isspace(cb))
114 cb = b[++bi];
116 /* process run of digits */
117 if (nat_isdigit(ca) && nat_isdigit(cb)) {
118 if ((result = compare_right(a+ai, b+bi)) != 0)
119 return result;
122 if (!ca && !cb) {
123 /* The strings compare the same. Perhaps the caller
124 will want to call strcmp to break the tie. */
125 return 0;
128 if (fold_case) {
129 ca = nat_toupper(ca);
130 cb = nat_toupper(cb);
133 if (ca < cb)
134 return -1;
135 else if (ca > cb)
136 return +1;
138 ++ai; ++bi;
144 int strnatcmp(const char *a, const char *b) {
145 return strnatcmp0(a, b, 0);
149 /* Compare, recognizing numeric string and ignoring case. */
150 int strnatcasecmp(const char *a, const char *b) {
151 return strnatcmp0(a, b, 1);