3 * Original code downloaded from: http://sourcefrog.net/projects/natsort/
5 strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
6 Copyright (C) 2000, 2004 by Martin Pool <mbp sourcefrog net>
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.
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:
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.
26 /* partial change history:
28 * 2004-10-10 mbp: Lift out character type dependencies into macros.
30 * Eric Sosman pointed out that ctype functions take a parameter whose
31 * value must be that of an unsigned int, even on platforms that have
32 * negative chars in their default char type.
36 * Modified 2014-10-29 to use the g_ascii_XXX() routines; this avoids
37 * locale-dependent behavior. The routine names were changed to
38 * ws_ascii_XXX() to reflect this.
44 #include "strnatcmp.h"
47 /* These are defined as macros to make it easier to adapt this code to
48 * different characters types or comparison functions. */
50 nat_isdigit(nat_char a)
52 return g_ascii_isdigit(a);
57 nat_isspace(nat_char a)
59 return g_ascii_isspace(a);
64 nat_toupper(nat_char a)
66 return g_ascii_toupper(a);
71 compare_right(nat_char const *a, nat_char const *b)
75 /* The longest run of digits wins. That aside, the greatest
76 value wins, but we can't know that it will until we've scanned
77 both numbers to know that they have the same magnitude, so we
78 remember it in BIAS. */
80 if (!nat_isdigit(*a) && !nat_isdigit(*b))
82 else if (!nat_isdigit(*a))
84 else if (!nat_isdigit(*b))
92 } else if (!*a && !*b)
101 compare_left(nat_char const *a, nat_char const *b)
103 /* Compare two left-aligned numbers: the first to have a
104 different value wins. */
106 if (!nat_isdigit(*a) && !nat_isdigit(*b))
108 else if (!nat_isdigit(*a))
110 else if (!nat_isdigit(*b))
122 static int strnatcmp0(nat_char const *a, nat_char const *b, int fold_case)
126 int fractional, result;
137 ca = a[ai]; cb = b[bi];
139 /* skip over leading spaces or zeros */
140 while (nat_isspace(ca))
143 while (nat_isspace(cb))
146 /* process run of digits */
147 if (nat_isdigit(ca) && nat_isdigit(cb)) {
148 fractional = (ca == '0' || cb == '0');
151 if ((result = compare_left(a+ai, b+bi)) != 0)
154 if ((result = compare_right(a+ai, b+bi)) != 0)
160 /* The strings compare the same. Perhaps the caller
161 will want to call strcmp to break the tie. */
166 ca = nat_toupper(ca);
167 cb = nat_toupper(cb);
180 int ws_ascii_strnatcmp(nat_char const *a, nat_char const *b)
182 return strnatcmp0(a, b, 0);
186 /* Compare, recognizing numeric string and ignoring case. */
187 int ws_ascii_strnatcasecmp(nat_char const *a, nat_char const *b)
189 return strnatcmp0(a, b, 1);
194 * Editor modelines - http://www.wireshark.org/tools/modelines.html
199 * indent-tabs-mode: nil
202 * vi: set shiftwidth=4 tabstop=8 expandtab:
203 * :indentSize=4:tabSize=8:noTabs=true: