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.
34 * 2013-08-23: Skip leading zero's for any run of digits, except
35 * when a decimal point was seen immediatley before.
36 * Patch by LoRd_MuldeR <mulder2@gmx.de>
44 #include "../include/strnatcmp.h"
46 /* These are defined as macros to make it easier to adapt this code to
47 * different characters types or comparison functions. */
48 static inline int nat_isdigit(MUtils::Internal::NaturalSort::nat_char a)
53 static inline int nat_isspace(MUtils::Internal::NaturalSort::nat_char a)
58 static inline MUtils::Internal::NaturalSort::nat_char nat_isdecpoint(MUtils::Internal::NaturalSort::nat_char a)
60 return (a == L'.') || (a == L',');
63 static inline MUtils::Internal::NaturalSort::nat_char nat_toupper(MUtils::Internal::NaturalSort::nat_char a)
68 static int compare_right(MUtils::Internal::NaturalSort::nat_char const *a, MUtils::Internal::NaturalSort::nat_char const *b)
72 /* The longest run of digits wins. That aside, the greatest
73 value wins, but we can't know that it will until we've scanned
74 both numbers to know that they have the same magnitude, so we
75 remember it in BIAS. */
78 if (!nat_isdigit(*a) && !nat_isdigit(*b))
80 else if (!nat_isdigit(*a))
82 else if (!nat_isdigit(*b))
101 static int compare_left(MUtils::Internal::NaturalSort::nat_char const *a, MUtils::Internal::NaturalSort::nat_char const *b)
103 /* Compare two left-aligned numbers: the first to have a
104 different value wins. */
107 if (!nat_isdigit(*a) && !nat_isdigit(*b))
109 else if (!nat_isdigit(*a))
111 else if (!nat_isdigit(*b))
122 static int strnatcmp0(MUtils::Internal::NaturalSort::nat_char const *a, MUtils::Internal::NaturalSort::nat_char const *b, const bool fold_case)
125 MUtils::Internal::NaturalSort::nat_char ca, cb;
136 ca = a[ai]; cb = b[bi];
138 /* skip over leading spaces or zeros */
139 while (nat_isspace(ca))
142 while (nat_isspace(cb))
145 /* process run of digits */
146 if (nat_isdigit(ca) && nat_isdigit(cb))
164 if ((result = compare_left(a+ai, b+bi)) != 0)
169 if ((result = compare_right(a+ai, b+bi)) != 0)
173 /* on tie, the string with the longer leading zero's sequence wins */
182 /* The strings compare the same. Perhaps the caller
183 will want to call strcmp to break the tie. */
184 return (fold_case) ? _wcsicmp(a, b) : wcscmp(a, b);
189 ca = nat_toupper(ca);
190 cb = nat_toupper(cb);
198 /* skipp leading zero's, unless previously seen char was a decimal point */
199 fractional = nat_isdecpoint(ca) && nat_isdecpoint(cb);
205 int MUtils::Internal::NaturalSort::strnatcmp(nat_char const *a, nat_char const *b)
207 return strnatcmp0(a, b, false);
210 /* Compare, recognizing numeric string and ignoring case. */
211 int MUtils::Internal::NaturalSort::strnatcasecmp(nat_char const *a, nat_char const *b)
213 return strnatcmp0(a, b, true);