Style change for strnatcmp
o9000 mrovi9000@gmail.com
2 files changed,
48 insertions(+),
63 deletions(-)
M
src/util/strnatcmp.c
→
src/util/strnatcmp.c
@@ -36,37 +36,21 @@ #include <stdio.h>
#include "strnatcmp.h" -/* These are defined as macros to make it easier to adapt this code to - * different characters types or comparison functions. */ -static inline int nat_isdigit(nat_char a) -{ - return isdigit((unsigned char)a); -} - -static inline int nat_isspace(nat_char a) -{ - return isspace((unsigned char)a); -} - -static inline nat_char nat_toupper(nat_char a) -{ - return toupper((unsigned char)a); -} - -static int compare_right(nat_char const *a, nat_char const *b) +// Compare two right-aligned numbers: +// The longest run of digits wins. That aside, the greatest +// value wins, but we can't know that it will until we've scanned +// both numbers to know that they have the same magnitude, so we +// remember it in BIAS. +static int compare_right(char const *a, char const *b) { int bias = 0; - /* The longest run of digits wins. That aside, the greatest - * value wins, but we can't know that it will until we've scanned - * both numbers to know that they have the same magnitude, so we - * remember it in BIAS. */ for (;; a++, b++) { - if (!nat_isdigit(*a) && !nat_isdigit(*b)) + if (!isdigit(*a) && !isdigit(*b)) return bias; - else if (!nat_isdigit(*a)) + else if (!isdigit(*a)) return -1; - else if (!nat_isdigit(*b)) + else if (!isdigit(*b)) return +1; else if (*a < *b) { if (!bias)@@ -81,15 +65,16 @@
return 0; } -static int compare_left(nat_char const *a, nat_char const *b) +// Compare two left-aligned numbers: +// The first to have a different value wins. +static int compare_left(char const *a, char const *b) { - /* Compare two left-aligned numbers: the first to have a different value wins. */ for (;; a++, b++) { - if (!nat_isdigit(*a) && !nat_isdigit(*b)) + if (!isdigit(*a) && !isdigit(*b)) return 0; - else if (!nat_isdigit(*a)) + else if (!isdigit(*a)) return -1; - else if (!nat_isdigit(*b)) + else if (!isdigit(*b)) return +1; else if (*a < *b) return -1;@@ -100,46 +85,50 @@
return 0; } -static int strnatcmp0(nat_char const *a, nat_char const *b, int fold_case) +static int strnatcmp0(char const *a, char const *b, int ignore_case) { - int ai, bi; - nat_char ca, cb; - int fractional, result; - assert(a && b); + + int ai, bi; ai = bi = 0; while (1) { - ca = a[ai]; - cb = b[bi]; + char ca = a[ai]; + char cb = b[bi]; - /* skip over leading spaces or zeros */ - while (nat_isspace(ca)) - ca = a[++ai]; + // Skip over leading spaces + while (isspace(ca)) { + ai++; + ca = a[ai]; + } - while (nat_isspace(cb)) - cb = b[++bi]; + while (isspace(cb)) { + bi++; + cb = b[bi]; + } - /* process run of digits */ - if (nat_isdigit(ca) && nat_isdigit(cb)) { - fractional = (ca == '0' || cb == '0'); + // Process run of digits + if (isdigit(ca) && isdigit(cb)) { + int fractional = (ca == '0' || cb == '0'); if (fractional) { - if ((result = compare_left(a + ai, b + bi)) != 0) + int result = compare_left(a + ai, b + bi); + if (result) return result; } else { - if ((result = compare_right(a + ai, b + bi)) != 0) + int result = compare_right(a + ai, b + bi); + if (result) return result; } } if (!ca && !cb) { - /* The strings compare the same. Perhaps the caller will want to call strcmp to break the tie. */ + // The strings compare the same. Perhaps the caller will want to call strcmp to break the tie. return 0; } - if (fold_case) { - ca = nat_toupper(ca); - cb = nat_toupper(cb); + if (ignore_case) { + ca = toupper(ca); + cb = toupper(cb); } if (ca < cb)@@ -147,18 +136,17 @@ return -1;
else if (ca > cb) return +1; - ++ai; - ++bi; + ai++; + bi++; } } -int strnatcmp(nat_char const *a, nat_char const *b) +int strnatcmp(char const *a, char const *b) { return strnatcmp0(a, b, 0); } -/* Compare, recognizing numeric string and ignoring case. */ -int strnatcasecmp(nat_char const *a, nat_char const *b) +int strnatcasecmp(char const *a, char const *b) { return strnatcmp0(a, b, 1); }
M
src/util/strnatcmp.h
→
src/util/strnatcmp.h
@@ -20,11 +20,8 @@ misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution. */ -/* CUSTOMIZATION SECTION - * - * You can change this typedef, but must then also change the inline - * functions in strnatcmp.c */ -typedef char nat_char; +// Compare, recognizing numeric strings. Case sensitive. +int strnatcmp(char const *a, char const *b); -int strnatcmp(nat_char const *a, nat_char const *b); -int strnatcasecmp(nat_char const *a, nat_char const *b); +// Compare, recognizing numeric strings. Case insensitive. +int strnatcasecmp(char const *a, char const *b);