]>
Commit | Line | Data |
---|---|---|
7088bd88 WP |
1 | /** |
2 | * Character handling in C locale. | |
3 | * | |
4 | * This file is based on gnulib c-ctype.h-dd7a871 with the | |
5 | * other gnulib dependencies removed for use in util-linux. | |
6 | * | |
7 | * These functions work like the corresponding functions in <ctype.h>, | |
8 | * except that they have the C (POSIX) locale hardwired, whereas the | |
9 | * <ctype.h> functions' behaviour depends on the current locale set via | |
10 | * setlocale. | |
11 | * | |
12 | * Copyright (C) 2000-2003, 2006, 2008-2017 Free Software Foundation, Inc. | |
13 | * | |
14 | * This program is free software; you can redistribute it and/or modify | |
15 | * it under the terms of the GNU General Public License as published by | |
16 | * the Free Software Foundation; either version 2 of the License, or | |
17 | * (at your option) any later version. | |
18 | * | |
19 | * This program is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License | |
25 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
26 | */ | |
27 | ||
28 | #ifndef UTIL_LINUX_CCTYPE_H | |
29 | #define UTIL_LINUX_CCTYPE_H | |
30 | ||
31 | /** | |
32 | * The functions defined in this file assume the "C" locale and a character | |
33 | * set without diacritics (ASCII-US or EBCDIC-US or something like that). | |
34 | * Even if the "C" locale on a particular system is an extension of the ASCII | |
35 | * character set (like on BeOS, where it is UTF-8, or on AmigaOS, where it | |
36 | * is ISO-8859-1), the functions in this file recognize only the ASCII | |
37 | * characters. | |
38 | */ | |
39 | ||
40 | #if (' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \ | |
41 | && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \ | |
42 | && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \ | |
43 | && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \ | |
44 | && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \ | |
45 | && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \ | |
46 | && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \ | |
47 | && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \ | |
48 | && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \ | |
49 | && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \ | |
50 | && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \ | |
51 | && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \ | |
52 | && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \ | |
53 | && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \ | |
54 | && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \ | |
55 | && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \ | |
56 | && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \ | |
57 | && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \ | |
58 | && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \ | |
59 | && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \ | |
60 | && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \ | |
61 | && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \ | |
62 | && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126) | |
63 | ||
64 | /* | |
65 | * The character set is ASCII or one of its variants or extensions, not EBCDIC. | |
66 | * Testing the value of '\n' and '\r' is not relevant. | |
67 | */ | |
68 | # define C_CTYPE_ASCII 1 | |
69 | #elif ! (' ' == '\x40' && '0' == '\xf0' \ | |
70 | && 'A' == '\xc1' && 'J' == '\xd1' && 'S' == '\xe2' \ | |
71 | && 'a' == '\x81' && 'j' == '\x91' && 's' == '\xa2') | |
72 | # error "Only ASCII and EBCDIC are supported" | |
73 | #endif | |
74 | ||
75 | #if 'A' < 0 | |
76 | # error "EBCDIC and char is signed -- not supported" | |
77 | #endif | |
78 | ||
79 | /* Cases for control characters. */ | |
80 | #define _C_CTYPE_CNTRL \ | |
81 | case '\a': case '\b': case '\f': case '\n': \ | |
82 | case '\r': case '\t': case '\v': \ | |
83 | _C_CTYPE_OTHER_CNTRL | |
84 | ||
85 | /* ASCII control characters other than those with \-letter escapes. */ | |
86 | #if C_CTYPE_ASCII | |
87 | # define _C_CTYPE_OTHER_CNTRL \ | |
88 | case '\x00': case '\x01': case '\x02': case '\x03': \ | |
89 | case '\x04': case '\x05': case '\x06': case '\x0e': \ | |
90 | case '\x0f': case '\x10': case '\x11': case '\x12': \ | |
91 | case '\x13': case '\x14': case '\x15': case '\x16': \ | |
92 | case '\x17': case '\x18': case '\x19': case '\x1a': \ | |
93 | case '\x1b': case '\x1c': case '\x1d': case '\x1e': \ | |
94 | case '\x1f': case '\x7f' | |
95 | #else | |
96 | ||
97 | /* | |
98 | * Use EBCDIC code page 1047's assignments for ASCII control chars; | |
99 | * assume all EBCDIC code pages agree about these assignments. | |
100 | */ | |
101 | # define _C_CTYPE_OTHER_CNTRL \ | |
102 | case '\x00': case '\x01': case '\x02': case '\x03': \ | |
103 | case '\x07': case '\x0e': case '\x0f': case '\x10': \ | |
104 | case '\x11': case '\x12': case '\x13': case '\x18': \ | |
105 | case '\x19': case '\x1c': case '\x1d': case '\x1e': \ | |
106 | case '\x1f': case '\x26': case '\x27': case '\x2d': \ | |
107 | case '\x2e': case '\x32': case '\x37': case '\x3c': \ | |
108 | case '\x3d': case '\x3f' | |
109 | #endif | |
110 | ||
111 | /* Cases for lowercase hex letters, and lowercase letters, all offset by N. */ | |
112 | #define _C_CTYPE_LOWER_A_THRU_F_N(N) \ | |
113 | case 'a' + (N): case 'b' + (N): case 'c' + (N): case 'd' + (N): \ | |
114 | case 'e' + (N): case 'f' + (N) | |
115 | #define _C_CTYPE_LOWER_N(N) \ | |
116 | _C_CTYPE_LOWER_A_THRU_F_N(N): \ | |
117 | case 'g' + (N): case 'h' + (N): case 'i' + (N): case 'j' + (N): \ | |
118 | case 'k' + (N): case 'l' + (N): case 'm' + (N): case 'n' + (N): \ | |
119 | case 'o' + (N): case 'p' + (N): case 'q' + (N): case 'r' + (N): \ | |
120 | case 's' + (N): case 't' + (N): case 'u' + (N): case 'v' + (N): \ | |
121 | case 'w' + (N): case 'x' + (N): case 'y' + (N): case 'z' + (N) | |
122 | ||
123 | /* Cases for hex letters, digits, lower, punct, and upper. */ | |
124 | #define _C_CTYPE_A_THRU_F \ | |
125 | _C_CTYPE_LOWER_A_THRU_F_N (0): \ | |
126 | _C_CTYPE_LOWER_A_THRU_F_N ('A' - 'a') | |
127 | #define _C_CTYPE_DIGIT \ | |
128 | case '0': case '1': case '2': case '3': \ | |
129 | case '4': case '5': case '6': case '7': \ | |
130 | case '8': case '9' | |
131 | #define _C_CTYPE_LOWER _C_CTYPE_LOWER_N (0) | |
132 | #define _C_CTYPE_PUNCT \ | |
133 | case '!': case '"': case '#': case '$': \ | |
134 | case '%': case '&': case '\'': case '(': \ | |
135 | case ')': case '*': case '+': case ',': \ | |
136 | case '-': case '.': case '/': case ':': \ | |
137 | case ';': case '<': case '=': case '>': \ | |
138 | case '?': case '@': case '[': case '\\': \ | |
139 | case ']': case '^': case '_': case '`': \ | |
140 | case '{': case '|': case '}': case '~' | |
141 | #define _C_CTYPE_UPPER _C_CTYPE_LOWER_N ('A' - 'a') | |
142 | ||
143 | /** | |
144 | * Function definitions. | |
145 | * | |
146 | * Unlike the functions in <ctype.h>, which require an argument in the range | |
147 | * of the 'unsigned char' type, the functions here operate on values that are | |
148 | * in the 'unsigned char' range or in the 'char' range. In other words, | |
149 | * when you have a 'char' value, you need to cast it before using it as | |
150 | * argument to a <ctype.h> function: | |
151 | * | |
152 | * const char *s = ...; | |
153 | * if (isalpha ((unsigned char) *s)) ... | |
154 | * | |
155 | * but you don't need to cast it for the functions defined in this file: | |
156 | * | |
157 | * const char *s = ...; | |
158 | * if (c_isalpha (*s)) ... | |
159 | */ | |
160 | ||
161 | static inline int c_isalnum (int c) | |
162 | { | |
163 | switch (c) { | |
164 | _C_CTYPE_DIGIT: | |
165 | _C_CTYPE_LOWER: | |
166 | _C_CTYPE_UPPER: | |
167 | return 1; | |
168 | default: | |
169 | return 0; | |
170 | } | |
171 | } | |
172 | ||
173 | static inline int c_isalpha (int c) | |
174 | { | |
175 | switch (c) { | |
176 | _C_CTYPE_LOWER: | |
177 | _C_CTYPE_UPPER: | |
178 | return 1; | |
179 | default: | |
180 | return 0; | |
181 | } | |
182 | } | |
183 | ||
184 | /* The function isascii is not locale dependent. | |
185 | * Its use in EBCDIC is questionable. | |
186 | */ | |
187 | static inline int c_isascii (int c) | |
188 | { | |
189 | switch (c) { | |
190 | case ' ': | |
191 | _C_CTYPE_CNTRL: | |
192 | _C_CTYPE_DIGIT: | |
193 | _C_CTYPE_LOWER: | |
194 | _C_CTYPE_PUNCT: | |
195 | _C_CTYPE_UPPER: | |
196 | return 1; | |
197 | default: | |
198 | return 0; | |
199 | } | |
200 | } | |
201 | ||
202 | static inline int c_isblank (int c) | |
203 | { | |
204 | return c == ' ' || c == '\t'; | |
205 | } | |
206 | ||
207 | static inline int c_iscntrl (int c) | |
208 | { | |
209 | switch (c) { | |
210 | _C_CTYPE_CNTRL: | |
211 | return 1; | |
212 | default: | |
213 | return 0; | |
214 | } | |
215 | } | |
216 | ||
217 | static inline int c_isdigit (int c) | |
218 | { | |
219 | switch (c) { | |
220 | _C_CTYPE_DIGIT: | |
221 | return 1; | |
222 | default: | |
223 | return 0; | |
224 | } | |
225 | } | |
226 | ||
227 | static inline int c_isgraph (int c) | |
228 | { | |
229 | switch (c) { | |
230 | _C_CTYPE_DIGIT: | |
231 | _C_CTYPE_LOWER: | |
232 | _C_CTYPE_PUNCT: | |
233 | _C_CTYPE_UPPER: | |
234 | return 1; | |
235 | default: | |
236 | return 0; | |
237 | } | |
238 | } | |
239 | ||
240 | static inline int c_islower (int c) | |
241 | { | |
242 | switch (c) { | |
243 | _C_CTYPE_LOWER: | |
244 | return 1; | |
245 | default: | |
246 | return 0; | |
247 | } | |
248 | } | |
249 | ||
250 | static inline int c_isprint (int c) | |
251 | { | |
252 | switch (c) { | |
253 | case ' ': | |
254 | _C_CTYPE_DIGIT: | |
255 | _C_CTYPE_LOWER: | |
256 | _C_CTYPE_PUNCT: | |
257 | _C_CTYPE_UPPER: | |
258 | return 1; | |
259 | default: | |
260 | return 0; | |
261 | } | |
262 | } | |
263 | ||
264 | static inline int c_ispunct (int c) | |
265 | { | |
266 | switch (c) { | |
267 | _C_CTYPE_PUNCT: | |
268 | return 1; | |
269 | default: | |
270 | return 0; | |
271 | } | |
272 | } | |
273 | ||
274 | static inline int c_isspace (int c) | |
275 | { | |
276 | switch (c) { | |
277 | case ' ': case '\t': case '\n': case '\v': case '\f': case '\r': | |
278 | return 1; | |
279 | default: | |
280 | return 0; | |
281 | } | |
282 | } | |
283 | ||
284 | static inline int c_isupper (int c) | |
285 | { | |
286 | switch (c) { | |
287 | _C_CTYPE_UPPER: | |
288 | return 1; | |
289 | default: | |
290 | return 0; | |
291 | } | |
292 | } | |
293 | ||
294 | static inline int c_isxdigit (int c) | |
295 | { | |
296 | switch (c) { | |
297 | _C_CTYPE_DIGIT: | |
298 | _C_CTYPE_A_THRU_F: | |
299 | return 1; | |
300 | default: | |
301 | return 0; | |
302 | } | |
303 | } | |
304 | ||
305 | static inline int c_tolower (int c) | |
306 | { | |
307 | switch (c) { | |
308 | _C_CTYPE_UPPER: | |
309 | return c - 'A' + 'a'; | |
310 | default: | |
311 | return c; | |
312 | } | |
313 | } | |
314 | ||
315 | static inline int c_toupper (int c) | |
316 | { | |
317 | switch (c) { | |
318 | _C_CTYPE_LOWER: | |
319 | return c - 'a' + 'A'; | |
320 | default: | |
321 | return c; | |
322 | } | |
323 | } | |
324 | ||
325 | #endif /* UTIL_LINUX_CCTYPE_H */ |