comparison lib/strverscmp.c @ 2162:f791c89a9a49

(ISDIGIT): Define. (strverscmp): Use ISDIGIT, not isdigit.
author Jim Meyering <jim@meyering.net>
date Sat, 22 Jan 2000 09:43:23 +0000
parents 38fd8f5d359d
children 9b7ce618e1fb
comparison
equal deleted inserted replaced
2161:ed7e808dc3f9 2162:f791c89a9a49
1 /* Compare strings while treating digits characters numerically. 1 /* Compare strings while treating digits characters numerically.
2 Copyright (C) 1997 Free Software Foundation, Inc. 2 Copyright (C) 1997, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library.
4 Contributed by Jean-Francois Bignolles <bignolle@ecoledoc.ibp.fr>, 1997. 4 Contributed by Jean-Francois Bignolles <bignolle@ecoledoc.ibp.fr>, 1997.
5 5
6 The GNU C Library is free software; you can redistribute it and/or 6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as 7 modify it under the terms of the GNU Library General Public License as
34 34
35 /* result_type: CMP: return diff; LEN: compare using len_diff/diff */ 35 /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
36 #define CMP 2 36 #define CMP 2
37 #define LEN 3 37 #define LEN 3
38 38
39 /* ISDIGIT differs from isdigit, as follows:
40 - Its arg may be any int or unsigned int; it need not be an unsigned char.
41 - It's guaranteed to evaluate its argument exactly once.
42 - It's typically faster.
43 Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
44 only '0' through '9' are digits. Prefer ISDIGIT to isdigit unless
45 it's important to use the locale's definition of `digit' even when the
46 host does not conform to Posix. */
47 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
39 48
40 /* Compare S1 and S2 as strings holding indices/version numbers, 49 /* Compare S1 and S2 as strings holding indices/version numbers,
41 returning less than, equal to or greater than zero if S1 is less than, 50 returning less than, equal to or greater than zero if S1 is less than,
42 equal to or greater than S2 (for more info, see the texinfo doc). 51 equal to or greater than S2 (for more info, see the texinfo doc).
43 */ 52 */
81 return 0; 90 return 0;
82 91
83 c1 = *p1++; 92 c1 = *p1++;
84 c2 = *p2++; 93 c2 = *p2++;
85 /* Hint: '0' is a digit too. */ 94 /* Hint: '0' is a digit too. */
86 state = S_N | ((c1 == '0') + (isdigit (c1) != 0)); 95 state = S_N | ((c1 == '0') + (ISDIGIT (c1) != 0));
87 96
88 while ((diff = c1 - c2) == 0 && c1 != '\0') 97 while ((diff = c1 - c2) == 0 && c1 != '\0')
89 { 98 {
90 state = next_state[state]; 99 state = next_state[state];
91 c1 = *p1++; 100 c1 = *p1++;
92 c2 = *p2++; 101 c2 = *p2++;
93 state |= (c1 == '0') + (isdigit (c1) != 0); 102 state |= (c1 == '0') + (ISDIGIT (c1) != 0);
94 } 103 }
95 104
96 state = result_type[state << 2 | ((c2 == '0') + (isdigit (c2) != 0))]; 105 state = result_type[state << 2 | ((c2 == '0') + (ISDIGIT (c2) != 0))];
97 106
98 switch (state) 107 switch (state)
99 { 108 {
100 case CMP: 109 case CMP:
101 return diff; 110 return diff;
102 111
103 case LEN: 112 case LEN:
104 while (isdigit (*p1++)) 113 while (ISDIGIT (*p1++))
105 if (!isdigit (*p2++)) 114 if (!ISDIGIT (*p2++))
106 return 1; 115 return 1;
107 116
108 return isdigit (*p2) ? -1 : diff; 117 return ISDIGIT (*p2) ? -1 : diff;
109 118
110 default: 119 default:
111 return state; 120 return state;
112 } 121 }
113 } 122 }