Merged older cs.po file with newest pot file.
[gliv/czech_localization.git] / src / strnatcmp.c
bloba33976ba86e9c64dfd1f1c55c2dc733dac6b74e8
1 /* -*- mode: c; c-file-style: "k&r" -*-
3 strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
4 Copyright (C) 2000, 2004 by Martin Pool <mbp sourcefrog net>
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.
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:
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.
23 /* Modified by Guillaume Chazarain in order to be integrated in GLiv */
25 #include <ctype.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <stdio.h>
30 #include "strnatcmp.h"
33 static int compare_right(const char *a, const char *b)
35 int bias = 0;
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;
56 return 0;
60 static int compare_left(const char *a, const char *b)
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;
75 return 0;
79 int strnatcmp(const char *a, const char *b)
81 int ai, bi;
82 char ca, cb;
83 int fractional, result;
85 ai = bi = 0;
86 while (1) {
87 ca = a[ai];
88 cb = b[bi];
90 /* skip over leading spaces or zeros */
91 while (isspace(ca))
92 ca = a[++ai];
94 while (isspace(cb))
95 cb = b[++bi];
97 /* process run of digits */
98 if (isdigit(ca) && isdigit(cb)) {
99 fractional = (ca == '0' || cb == '0');
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;
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;
116 if (ca < cb)
117 return -1;
118 else if (ca > cb)
119 return +1;
121 ++ai;
122 ++bi;