1 /* -*- mode: c; c-file-style: "k&r" -*-
2 
3   strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
4   Copyright (C) 2000, 2004 by Martin Pool <mbp sourcefrog net>
5 
6   This software is provided 'as-is', without any express or implied
7   warranty.  In no event will the authors be held liable for any damages
8   arising from the use of this software.
9 
10   Permission is granted to anyone to use this software for any purpose,
11   including commercial applications, and to alter it and redistribute it
12   freely, subject to the following restrictions:
13 
14   1. The origin of this software must not be misrepresented; you must not
15      claim that you wrote the original software. If you use this software
16      in a product, an acknowledgment in the product documentation would be
17      appreciated but is not required.
18   2. Altered source versions must be plainly marked as such, and must not be
19      misrepresented as being the original software.
20   3. This notice may not be removed or altered from any source distribution.
21 */
22 
23 /* Modified by Guillaume Chazarain in order to be integrated in GLiv */
24 
25 #include <ctype.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <stdio.h>
29 
30 #include "strnatcmp.h"
31 
32 
compare_right(const char * a,const char * b)33 static int compare_right(const char *a, const char *b)
34 {
35     int bias = 0;
36 
37     /* The longest run of digits wins.  That aside, the greatest
38        value wins, but we can't know that it will until we've scanned
39        both numbers to know that they have the same magnitude, so we
40        remember it in BIAS. */
41     for (;; a++, b++) {
42         if (!isdigit(*a))
43             return isdigit(*b) ? -1 : bias;
44         else if (!isdigit(*b))
45             return +1;
46         else if (*a < *b) {
47             if (!bias)
48                 bias = -1;
49         } else if (*a > *b) {
50             if (!bias)
51                 bias = +1;
52         } else if (!*a && !*b)
53             return bias;
54     }
55 
56     return 0;
57 }
58 
59 
compare_left(const char * a,const char * b)60 static int compare_left(const char *a, const char *b)
61 {
62     /* Compare two left-aligned numbers: the first to have a
63        different value wins. */
64     for (;; a++, b++) {
65         if (!isdigit(*a))
66             return !isdigit(*b) - 1;
67         else if (!isdigit(*b))
68             return +1;
69         else if (*a < *b)
70             return -1;
71         else if (*a > *b)
72             return +1;
73     }
74 
75     return 0;
76 }
77 
78 
strnatcmp(const char * a,const char * b)79 int strnatcmp(const char *a, const char *b)
80 {
81     int ai, bi;
82     char ca, cb;
83     int fractional, result;
84 
85     ai = bi = 0;
86     while (1) {
87         ca = a[ai];
88         cb = b[bi];
89 
90         /* skip over leading spaces or zeros */
91         while (isspace(ca))
92             ca = a[++ai];
93 
94         while (isspace(cb))
95             cb = b[++bi];
96 
97         /* process run of digits */
98         if (isdigit(ca) && isdigit(cb)) {
99             fractional = (ca == '0' || cb == '0');
100 
101             if (fractional) {
102                 if ((result = compare_left(a + ai, b + bi)) != 0)
103                     return result;
104             } else {
105                 if ((result = compare_right(a + ai, b + bi)) != 0)
106                     return result;
107             }
108         }
109 
110         if (!ca && !cb) {
111             /* The strings compare the same.  Perhaps the caller
112                will want to call strcmp to break the tie. */
113             return 0;
114         }
115 
116         if (ca < cb)
117             return -1;
118         else if (ca > cb)
119             return +1;
120 
121         ++ai;
122         ++bi;
123     }
124 }
125