|
| 1 | +/* -*- mode: c; c-file-style: "k&r" -*- |
| 2 | + strnatcmp.c -- Perform 'natural order' comparisons of strings in C. |
| 3 | + Copyright (C) 2000, 2004 by Martin Pool <mbp sourcefrog net> |
| 4 | + This software is provided 'as-is', without any express or implied |
| 5 | + warranty. In no event will the authors be held liable for any damages |
| 6 | + arising from the use of this software. |
| 7 | + Permission is granted to anyone to use this software for any purpose, |
| 8 | + including commercial applications, and to alter it and redistribute it |
| 9 | + freely, subject to the following restrictions: |
| 10 | + 1. The origin of this software must not be misrepresented; you must not |
| 11 | + claim that you wrote the original software. If you use this software |
| 12 | + in a product, an acknowledgment in the product documentation would be |
| 13 | + appreciated but is not required. |
| 14 | + 2. Altered source versions must be plainly marked as such, and must not be |
| 15 | + misrepresented as being the original software. |
| 16 | + 3. This notice may not be removed or altered from any source distribution. |
| 17 | +*/ |
| 18 | + |
| 19 | + |
| 20 | +/* partial change history: |
| 21 | + * |
| 22 | + * 2004-10-10 mbp: Lift out character type dependencies into macros. |
| 23 | + * |
| 24 | + * Eric Sosman pointed out that ctype functions take a parameter whose |
| 25 | + * value must be that of an unsigned int, even on platforms that have |
| 26 | + * negative chars in their default char type. |
| 27 | + */ |
| 28 | + |
| 29 | +#include <stddef.h> /* size_t */ |
| 30 | +#include <ctype.h> |
| 31 | + |
| 32 | +#include "strnatcmp.h" |
| 33 | + |
| 34 | + |
| 35 | +/* These are defined as macros to make it easier to adapt this code to |
| 36 | + * different characters types or comparison functions. */ |
| 37 | +static inline int |
| 38 | +nat_isdigit(nat_char a) |
| 39 | +{ |
| 40 | + return isdigit((unsigned char) a); |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +static inline int |
| 45 | +nat_isspace(nat_char a) |
| 46 | +{ |
| 47 | + return isspace((unsigned char) a); |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +static inline nat_char |
| 52 | +nat_toupper(nat_char a) |
| 53 | +{ |
| 54 | + return toupper((unsigned char) a); |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +static int |
| 59 | +compare_right(nat_char const *a, nat_char const *b) |
| 60 | +{ |
| 61 | + int bias = 0; |
| 62 | + |
| 63 | + /* The longest run of digits wins. That aside, the greatest |
| 64 | + value wins, but we can't know that it will until we've scanned |
| 65 | + both numbers to know that they have the same magnitude, so we |
| 66 | + remember it in BIAS. */ |
| 67 | + for (;; a++, b++) { |
| 68 | + if (!nat_isdigit(*a) && !nat_isdigit(*b)) |
| 69 | + return bias; |
| 70 | + if (!nat_isdigit(*a)) |
| 71 | + return -1; |
| 72 | + if (!nat_isdigit(*b)) |
| 73 | + return +1; |
| 74 | + if (*a < *b) { |
| 75 | + if (!bias) |
| 76 | + bias = -1; |
| 77 | + } else if (*a > *b) { |
| 78 | + if (!bias) |
| 79 | + bias = +1; |
| 80 | + } else if (!*a && !*b) |
| 81 | + return bias; |
| 82 | + } |
| 83 | + |
| 84 | + return 0; |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +static int |
| 89 | +compare_left(nat_char const *a, nat_char const *b) |
| 90 | +{ |
| 91 | + /* Compare two left-aligned numbers: the first to have a |
| 92 | + different value wins. */ |
| 93 | + for (;; a++, b++) { |
| 94 | + if (!nat_isdigit(*a) && !nat_isdigit(*b)) |
| 95 | + return 0; |
| 96 | + if (!nat_isdigit(*a)) |
| 97 | + return -1; |
| 98 | + if (!nat_isdigit(*b)) |
| 99 | + return +1; |
| 100 | + if (*a < *b) |
| 101 | + return -1; |
| 102 | + if (*a > *b) |
| 103 | + return +1; |
| 104 | + } |
| 105 | + |
| 106 | + return 0; |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +static int |
| 111 | +strnatcmp0(nat_char const *a, nat_char const *b, int fold_case) |
| 112 | +{ |
| 113 | + int ai, bi; |
| 114 | + nat_char ca, cb; |
| 115 | + int fractional, result; |
| 116 | + |
| 117 | + ai = bi = 0; |
| 118 | + while (1) { |
| 119 | + ca = a[ai]; cb = b[bi]; |
| 120 | + |
| 121 | + /* skip over leading spaces or zeros */ |
| 122 | + while (nat_isspace(ca)) |
| 123 | + ca = a[++ai]; |
| 124 | + |
| 125 | + while (nat_isspace(cb)) |
| 126 | + cb = b[++bi]; |
| 127 | + |
| 128 | + /* process run of digits */ |
| 129 | + if (nat_isdigit(ca) && nat_isdigit(cb)) { |
| 130 | + fractional = (ca == '0' || cb == '0'); |
| 131 | + |
| 132 | + if (fractional) { |
| 133 | + if ((result = compare_left(a+ai, b+bi)) != 0) |
| 134 | + return result; |
| 135 | + } else { |
| 136 | + if ((result = compare_right(a+ai, b+bi)) != 0) |
| 137 | + return result; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if (!ca && !cb) { |
| 142 | + /* The strings compare the same. Perhaps the caller |
| 143 | + will want to call strcmp to break the tie. */ |
| 144 | + return 0; |
| 145 | + } |
| 146 | + |
| 147 | + if (fold_case) { |
| 148 | + ca = nat_toupper(ca); |
| 149 | + cb = nat_toupper(cb); |
| 150 | + } |
| 151 | + |
| 152 | + if (ca < cb) |
| 153 | + return -1; |
| 154 | + |
| 155 | + if (ca > cb) |
| 156 | + return +1; |
| 157 | + |
| 158 | + ++ai; ++bi; |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | + |
| 163 | +int |
| 164 | +strnatcmp(nat_char const *a, nat_char const *b) { |
| 165 | + return strnatcmp0(a, b, 0); |
| 166 | +} |
| 167 | + |
| 168 | + |
| 169 | +/* Compare, recognizing numeric string and ignoring case. */ |
| 170 | +int |
| 171 | +strnatcasecmp(nat_char const *a, nat_char const *b) { |
| 172 | + return strnatcmp0(a, b, 1); |
| 173 | +} |
0 commit comments