Update translations from Transifex
[midnight-commander.git] / lib / strutil / strverscmp.c
blob5a00fa4c0b0e23389c179c47b79cfcc1218488fa
1 /*
2 Compare strings while treating digits characters numerically.
4 Copyright (C) 1997-2019
5 Free Software Foundation, Inc.
7 This file is part of the GNU C Library.
8 Contributed by Jean-François Bignolles <bignolle@ecoledoc.ibp.fr>, 1997.
10 The GNU C Library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2.1 of the License, or (at your option) any later version.
15 The GNU C Library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with the GNU C Library; if not, see
22 <https://www.gnu.org/licenses/>.
24 This file is part of the Midnight Commander.
26 The Midnight Commander is free software: you can redistribute it
27 and/or modify it under the terms of the GNU General Public License as
28 published by the Free Software Foundation, either version 3 of the License,
29 or (at your option) any later version.
31 The Midnight Commander is distributed in the hope that it will be useful,
32 but WITHOUT ANY WARRANTY; without even the implied warranty of
33 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 GNU General Public License for more details.
36 You should have received a copy of the GNU General Public License
37 along with this program. If not, see <http://www.gnu.org/licenses/>.
40 #include <config.h>
42 #include <ctype.h>
43 #ifdef HAVE_STRVERSCMP
44 #include <string.h>
45 #endif /* HAVE_STRVERSCMP */
47 #include "lib/strutil.h"
49 /*** global variables ****************************************************************************/
51 /*** file scope macro definitions ****************************************************************/
53 #ifndef HAVE_STRVERSCMP
55 /* states: S_N: normal, S_I: comparing integral part, S_F: comparing
56 fractional parts, S_Z: idem but with leading Zeroes only */
57 #define S_N 0x0
58 #define S_I 0x3
59 #define S_F 0x6
60 #define S_Z 0x9
62 /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
63 #define CMP 2
64 #define LEN 3
66 #endif /* HAVE_STRVERSCMP */
68 /*** file scope type declarations ****************************************************************/
70 /*** file scope variables ************************************************************************/
72 /* --------------------------------------------------------------------------------------------- */
73 /*** file scope functions ************************************************************************/
74 /* --------------------------------------------------------------------------------------------- */
76 /* --------------------------------------------------------------------------------------------- */
77 /*** public functions ****************************************************************************/
78 /* --------------------------------------------------------------------------------------------- */
79 /* Compare S1 and S2 as strings holding indices/version numbers,
80 returning less than, equal to or greater than zero if S1 is less than,
81 equal to or greater than S2 (for more info, see the texinfo doc).
83 int
84 str_verscmp (const char *s1, const char *s2)
86 #ifdef HAVE_STRVERSCMP
87 return strverscmp (s1, s2);
89 #else /* HAVE_STRVERSCMP */
90 const unsigned char *p1 = (const unsigned char *) s1;
91 const unsigned char *p2 = (const unsigned char *) s2;
92 unsigned char c1, c2;
93 int state;
94 int diff;
96 /* *INDENT-OFF* */
97 /* Symbol(s) 0 [1-9] others
98 Transition (10) 0 (01) d (00) x */
99 static const unsigned char next_state[] =
101 /* state x d 0 */
102 /* S_N */ S_N, S_I, S_Z,
103 /* S_I */ S_N, S_I, S_I,
104 /* S_F */ S_N, S_F, S_F,
105 /* S_Z */ S_N, S_F, S_Z
108 static const signed char result_type[] =
110 /* state x/x x/d x/0 d/x d/d d/0 0/x 0/d 0/0 */
112 /* S_N */ CMP, CMP, CMP, CMP, LEN, CMP, CMP, CMP, CMP,
113 /* S_I */ CMP, -1, -1, +1, LEN, LEN, +1, LEN, LEN,
114 /* S_F */ CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
115 /* S_Z */ CMP, +1, +1, -1, CMP, CMP, -1, CMP, CMP
117 /* *INDENT-ON* */
119 if (p1 == p2)
120 return 0;
122 c1 = *p1++;
123 c2 = *p2++;
124 /* Hint: '0' is a digit too. */
125 state = S_N + ((c1 == '0') + (isdigit (c1) != 0));
127 while ((diff = c1 - c2) == 0)
129 if (c1 == '\0')
130 return diff;
132 state = next_state[state];
133 c1 = *p1++;
134 c2 = *p2++;
135 state += (c1 == '0') + (isdigit (c1) != 0);
138 state = result_type[state * 3 + (((c2 == '0') + (isdigit (c2) != 0)))];
140 switch (state)
142 case CMP:
143 return diff;
145 case LEN:
146 while (isdigit (*p1++))
147 if (!isdigit (*p2++))
148 return 1;
150 return isdigit (*p2) ? -1 : diff;
152 default:
153 return state;
155 #endif /* HAVE_STRVERSCMP */
158 /* --------------------------------------------------------------------------------------------- */