5 * Original code downloaded from: http://sourcefrog.net/projects/natsort/
7 strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
8 Copyright (C) 2000, 2004 by Martin Pool <mbp sourcefrog net>
10 This software is provided 'as-is', without any express or implied
11 warranty. In no event will the authors be held liable for any damages
12 arising from the use of this software.
14 Permission is granted to anyone to use this software for any purpose,
15 including commercial applications, and to alter it and redistribute it
16 freely, subject to the following restrictions:
18 1. The origin of this software must not be misrepresented; you must not
19 claim that you wrote the original software. If you use this software
20 in a product, an acknowledgment in the product documentation would be
21 appreciated but is not required.
22 2. Altered source versions must be plainly marked as such, and must not be
23 misrepresented as being the original software.
24 3. This notice may not be removed or altered from any source distribution.
28 /* partial change history:
30 * 2004-10-10 mbp: Lift out character type dependencies into macros.
32 * Eric Sosman pointed out that ctype functions take a parameter whose
33 * value must be that of an unsigned int, even on platforms that have
34 * negative chars in their default char type.
41 #include "strnatcmp.h"
44 /* These are defined as macros to make it easier to adapt this code to
45 * different characters types or comparison functions. */
47 nat_isdigit(nat_char a
)
49 return isdigit((unsigned char) a
);
54 nat_isspace(nat_char a
)
56 return isspace((unsigned char) a
);
61 nat_toupper(nat_char a
)
63 return toupper((unsigned char) a
);
68 compare_right(nat_char
const *a
, 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. */
77 if (!nat_isdigit(*a
) && !nat_isdigit(*b
))
79 else if (!nat_isdigit(*a
))
81 else if (!nat_isdigit(*b
))
89 } else if (!*a
&& !*b
)
98 compare_left(nat_char
const *a
, nat_char
const *b
)
100 /* Compare two left-aligned numbers: the first to have a
101 different value wins. */
103 if (!nat_isdigit(*a
) && !nat_isdigit(*b
))
105 else if (!nat_isdigit(*a
))
107 else if (!nat_isdigit(*b
))
119 static int strnatcmp0(nat_char
const *a
, nat_char
const *b
, int fold_case
)
123 int fractional
, result
;
134 ca
= a
[ai
]; cb
= b
[bi
];
136 /* skip over leading spaces or zeros */
137 while (nat_isspace(ca
))
140 while (nat_isspace(cb
))
143 /* process run of digits */
144 if (nat_isdigit(ca
) && nat_isdigit(cb
)) {
145 fractional
= (ca
== '0' || cb
== '0');
148 if ((result
= compare_left(a
+ai
, b
+bi
)) != 0)
151 if ((result
= compare_right(a
+ai
, b
+bi
)) != 0)
157 /* The strings compare the same. Perhaps the caller
158 will want to call strcmp to break the tie. */
163 ca
= nat_toupper(ca
);
164 cb
= nat_toupper(cb
);
177 int strnatcmp(nat_char
const *a
, nat_char
const *b
)
179 return strnatcmp0(a
, b
, 0);
183 /* Compare, recognizing numeric string and ignoring case. */
184 int strnatcasecmp(nat_char
const *a
, nat_char
const *b
)
186 return strnatcmp0(a
, b
, 1);
191 * Editor modelines - http://www.wireshark.org/tools/modelines.html
196 * indent-tabs-mode: nil
199 * vi: set shiftwidth=4 tabstop=4 expandtab:
200 * :indentSize=4:tabSize=4:noTabs=true: