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 */
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. */
42 nat_isdigit(nat_char a
)
44 return isdigit((unsigned char) a
);
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
);
63 compare_right(nat_char
const *a
, nat_char
const *b
)
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. */
72 if (!nat_isdigit(*a
) && !nat_isdigit(*b
))
84 } else if (!*a
&& !*b
)
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. */
98 if (!nat_isdigit(*a
) && !nat_isdigit(*b
))
100 if (!nat_isdigit(*a
))
102 if (!nat_isdigit(*b
))
115 strnatcmp0(nat_char
const *a
, nat_char
const *b
, int fold_case
)
119 int fractional
, result
;
123 ca
= a
[ai
]; cb
= b
[bi
];
125 /* skip over leading spaces or zeros */
126 while (nat_isspace(ca
))
129 while (nat_isspace(cb
))
132 /* process run of digits */
133 if (nat_isdigit(ca
) && nat_isdigit(cb
)) {
134 fractional
= (ca
== '0' || cb
== '0');
137 if ((result
= compare_left(a
+ai
, b
+bi
)) != 0)
140 if ((result
= compare_right(a
+ai
, b
+bi
)) != 0)
146 /* The strings compare the same. Perhaps the caller
147 will want to call strcmp to break the tie. */
152 ca
= nat_toupper(ca
);
153 cb
= nat_toupper(cb
);
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);