dcerpc-nt: add UNION_ALIGN_TO... helpers
[wireshark-sm.git] / wsutil / strnatcmp.c
blob6a9a6a5fd720d95d14ae67cc5356c7cda330bbcd
1 /* strnatcmp.c
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 SPDX-License-Identifier: Zlib
9 */
12 /* partial change history:
14 * 2004-10-10 mbp: Lift out character type dependencies into macros.
16 * Eric Sosman pointed out that ctype functions take a parameter whose
17 * value must be that of an unsigned int, even on platforms that have
18 * negative chars in their default char type.
22 * Modified 2014-10-29 to use the g_ascii_XXX() routines; this avoids
23 * locale-dependent behavior. The routine names were changed to
24 * ws_ascii_XXX() to reflect this.
27 #include "strnatcmp.h"
29 #include <glib.h>
31 /* These are defined as macros to make it easier to adapt this code to
32 * different characters types or comparison functions. */
33 static int
34 nat_isdigit(nat_char a)
36 return g_ascii_isdigit(a);
40 static int
41 nat_isspace(nat_char a)
43 return g_ascii_isspace(a);
47 static nat_char
48 nat_toupper(nat_char a)
50 return g_ascii_toupper(a);
54 static int
55 compare_right(nat_char const *a, nat_char const *b)
57 int bias = 0;
59 /* The longest run of digits wins. That aside, the greatest
60 value wins, but we can't know that it will until we've scanned
61 both numbers to know that they have the same magnitude, so we
62 remember it in BIAS. */
63 for (;; a++, b++) {
64 if (!nat_isdigit(*a) && !nat_isdigit(*b))
65 return bias;
66 else if (!nat_isdigit(*a))
67 return -1;
68 else if (!nat_isdigit(*b))
69 return +1;
70 else if (*a < *b) {
71 if (!bias)
72 bias = -1;
73 } else if (*a > *b) {
74 if (!bias)
75 bias = +1;
76 } else if (!*a && !*b)
77 return bias;
80 return 0;
84 static int
85 compare_left(nat_char const *a, nat_char const *b)
87 /* Compare two left-aligned numbers: the first to have a
88 different value wins. */
89 for (;; a++, b++) {
90 if (!nat_isdigit(*a) && !nat_isdigit(*b))
91 return 0;
92 else if (!nat_isdigit(*a))
93 return -1;
94 else if (!nat_isdigit(*b))
95 return +1;
96 else if (*a < *b)
97 return -1;
98 else if (*a > *b)
99 return +1;
102 return 0;
106 static int strnatcmp0(nat_char const *a, nat_char const *b, int fold_case)
108 int ai, bi;
109 nat_char ca, cb;
110 int fractional, result;
112 if (!a || !b) {
113 if (!a && !b)
114 return 0;
115 if (!a)
116 return -1;
117 return +1;
119 ai = bi = 0;
120 while (1) {
121 ca = a[ai]; cb = b[bi];
123 /* skip over leading spaces or zeros */
124 while (nat_isspace(ca))
125 ca = a[++ai];
127 while (nat_isspace(cb))
128 cb = b[++bi];
130 /* process run of digits */
131 if (nat_isdigit(ca) && nat_isdigit(cb)) {
132 fractional = (ca == '0' || cb == '0');
134 if (fractional) {
135 if ((result = compare_left(a+ai, b+bi)) != 0)
136 return result;
137 } else {
138 if ((result = compare_right(a+ai, b+bi)) != 0)
139 return result;
143 if (!ca && !cb) {
144 /* The strings compare the same. Perhaps the caller
145 will want to call strcmp to break the tie. */
146 return 0;
149 if (fold_case) {
150 ca = nat_toupper(ca);
151 cb = nat_toupper(cb);
154 if (ca < cb)
155 return -1;
156 else if (ca > cb)
157 return +1;
159 ++ai; ++bi;
164 int ws_ascii_strnatcmp(nat_char const *a, nat_char const *b)
166 return strnatcmp0(a, b, 0);
170 /* Compare, recognizing numeric string and ignoring case. */
171 int ws_ascii_strnatcasecmp(nat_char const *a, nat_char const *b)
173 return strnatcmp0(a, b, 1);
178 * Editor modelines - https://www.wireshark.org/tools/modelines.html
180 * Local variables:
181 * c-basic-offset: 4
182 * tab-width: 8
183 * indent-tabs-mode: nil
184 * End:
186 * vi: set shiftwidth=4 tabstop=8 expandtab:
187 * :indentSize=4:tabSize=8:noTabs=true: