modified: makesrc3.mk
[GalaxyCodeBases.git] / c_cpp / lib / natsort / strnatcmp.c
blobe253eecac7484690f713858bd1fb18194e6fd1b3
1 /* -*- mode: c; c-file-style: "k&r" -*-
3 strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
4 Copyright (C) 2000, 2004 by Martin Pool <mbp sourcefrog net>
6 This software is provided 'as-is', without any express or implied
7 warranty. In no event will the authors be held liable for any damages
8 arising from the use of this software.
10 Permission is granted to anyone to use this software for any purpose,
11 including commercial applications, and to alter it and redistribute it
12 freely, subject to the following restrictions:
14 1. The origin of this software must not be misrepresented; you must not
15 claim that you wrote the original software. If you use this software
16 in a product, an acknowledgment in the product documentation would be
17 appreciated but is not required.
18 2. Altered source versions must be plainly marked as such, and must not be
19 misrepresented as being the original software.
20 3. This notice may not be removed or altered from any source distribution.
24 /* partial change history:
26 * 2004-10-10 mbp: Lift out character type dependencies into macros.
28 * Eric Sosman pointed out that ctype functions take a parameter whose
29 * value must be that of an unsigned int, even on platforms that have
30 * negative chars in their default char type.
33 #include <stddef.h> /* size_t */
34 #include <ctype.h>
36 #include "strnatcmp.h"
39 /* These are defined as macros to make it easier to adapt this code to
40 * different characters types or comparison functions. */
41 static inline int
42 nat_isdigit(nat_char a)
44 return isdigit((unsigned char) a);
48 static inline int
49 nat_isspace(nat_char a)
51 return isspace((unsigned char) a);
55 static inline nat_char
56 nat_toupper(nat_char a)
58 return toupper((unsigned char) a);
62 static int
63 compare_right(nat_char const *a, nat_char const *b)
65 int bias = 0;
67 /* The longest run of digits wins. That aside, the greatest
68 value wins, but we can't know that it will until we've scanned
69 both numbers to know that they have the same magnitude, so we
70 remember it in BIAS. */
71 for (;; a++, b++) {
72 if (!nat_isdigit(*a) && !nat_isdigit(*b))
73 return bias;
74 if (!nat_isdigit(*a))
75 return -1;
76 if (!nat_isdigit(*b))
77 return +1;
78 if (*a < *b) {
79 if (!bias)
80 bias = -1;
81 } else if (*a > *b) {
82 if (!bias)
83 bias = +1;
84 } else if (!*a && !*b)
85 return bias;
88 return 0;
92 static int
93 compare_left(nat_char const *a, nat_char const *b)
95 /* Compare two left-aligned numbers: the first to have a
96 different value wins. */
97 for (;; a++, b++) {
98 if (!nat_isdigit(*a) && !nat_isdigit(*b))
99 return 0;
100 if (!nat_isdigit(*a))
101 return -1;
102 if (!nat_isdigit(*b))
103 return +1;
104 if (*a < *b)
105 return -1;
106 if (*a > *b)
107 return +1;
110 return 0;
114 static int
115 strnatcmp0(nat_char const *a, nat_char const *b, int fold_case)
117 int ai, bi;
118 nat_char ca, cb;
119 int fractional, result;
121 ai = bi = 0;
122 while (1) {
123 ca = a[ai]; cb = b[bi];
125 /* skip over leading spaces or zeros */
126 while (nat_isspace(ca))
127 ca = a[++ai];
129 while (nat_isspace(cb))
130 cb = b[++bi];
132 /* process run of digits */
133 if (nat_isdigit(ca) && nat_isdigit(cb)) {
134 fractional = (ca == '0' || cb == '0');
136 if (fractional) {
137 if ((result = compare_left(a+ai, b+bi)) != 0)
138 return result;
139 } else {
140 if ((result = compare_right(a+ai, b+bi)) != 0)
141 return result;
145 if (!ca && !cb) {
146 /* The strings compare the same. Perhaps the caller
147 will want to call strcmp to break the tie. */
148 return 0;
151 if (fold_case) {
152 ca = nat_toupper(ca);
153 cb = nat_toupper(cb);
156 if (ca < cb)
157 return -1;
159 if (ca > cb)
160 return +1;
162 ++ai; ++bi;
168 strnatcmp(nat_char const *a, nat_char const *b) {
169 return strnatcmp0(a, b, 0);
173 /* Compare, recognizing numeric string and ignoring case. */
175 strnatcasecmp(nat_char const *a, nat_char const *b) {
176 return strnatcmp0(a, b, 1);