1/* -*- mode: c; c-file-style: "k&r" -*-
2
3  strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
4  Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au>
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#include <ctype.h>
24#include <string.h>
25#include "apr_strings.h"
26#include "apr_lib.h"            /* for apr_is*() */
27
28#if defined(__GNUC__)
29#  define UNUSED __attribute__((__unused__))
30#else
31#  define UNUSED
32#endif
33
34/* based on "strnatcmp.c,v 1.6 2000/04/20 07:30:11 mbp Exp $" */
35
36static int
37compare_right(char const *a, char const *b)
38{
39     int bias = 0;
40
41     /* The longest run of digits wins.  That aside, the greatest
42	value wins, but we can't know that it will until we've scanned
43	both numbers to know that they have the same magnitude, so we
44	remember it in BIAS. */
45     for (;; a++, b++) {
46	  if (!apr_isdigit(*a)  &&  !apr_isdigit(*b))
47	       break;
48	  else if (!apr_isdigit(*a))
49	       return -1;
50	  else if (!apr_isdigit(*b))
51	       return +1;
52	  else if (*a < *b) {
53	       if (!bias)
54		    bias = -1;
55	  } else if (*a > *b) {
56	       if (!bias)
57		    bias = +1;
58	  } else if (!*a  &&  !*b)
59	       break;
60     }
61
62     return bias;
63}
64
65
66static int
67compare_left(char const *a, char const *b)
68{
69     /* Compare two left-aligned numbers: the first to have a
70        different value wins. */
71     for (;; a++, b++) {
72	  if (!apr_isdigit(*a)  &&  !apr_isdigit(*b))
73	       break;
74	  else if (!apr_isdigit(*a))
75	       return -1;
76	  else if (!apr_isdigit(*b))
77	       return +1;
78	  else if (*a < *b)
79	       return -1;
80	  else if (*a > *b)
81	       return +1;
82     }
83
84     return 0;
85}
86
87
88static int strnatcmp0(char const *a, char const *b, int fold_case)
89{
90     int ai, bi;
91     char ca, cb;
92     int fractional, result;
93     ai = bi = 0;
94     while (1) {
95	  ca = a[ai]; cb = b[bi];
96
97	  /* skip over leading spaces or zeros */
98	  while (apr_isspace(ca))
99	       ca = a[++ai];
100
101	  while (apr_isspace(cb))
102	       cb = b[++bi];
103
104	  /* process run of digits */
105	  if (apr_isdigit(ca)  &&  apr_isdigit(cb)) {
106	       fractional = (ca == '0' || cb == '0');
107
108	       if (fractional) {
109		    if ((result = compare_left(a+ai, b+bi)) != 0)
110			 return result;
111	       } else {
112		    if ((result = compare_right(a+ai, b+bi)) != 0)
113			 return result;
114	       }
115	  }
116
117	  if (!ca && !cb) {
118	       /* The strings compare the same.  Perhaps the caller
119                  will want to call strcmp to break the tie. */
120	       return 0;
121	  }
122
123	  if (fold_case) {
124	       ca = apr_toupper(ca);
125	       cb = apr_toupper(cb);
126	  }
127
128	  if (ca < cb)
129	       return -1;
130	  else if (ca > cb)
131	       return +1;
132
133	  ++ai; ++bi;
134     }
135}
136
137
138
139APR_DECLARE(int) apr_strnatcmp(char const *a, char const *b)
140{
141     return strnatcmp0(a, b, 0);
142}
143
144
145/* Compare, recognizing numeric string and ignoring case. */
146APR_DECLARE(int) apr_strnatcasecmp(char const *a, char const *b)
147{
148     return strnatcmp0(a, b, 1);
149}
150