1 /* Copyright (C) 1991-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18 /*
19 * ISO C99 Standard 7.4: Character handling <ctype.h>
20 */
21
22 #ifndef _CTYPE_H
23 #define _CTYPE_H 1
24
25 #include <features.h>
26 #include <bits/types.h>
27
28 __BEGIN_DECLS
29
30 #ifndef _ISbit
31 /* These are all the characteristics of characters.
32 If there get to be more than 16 distinct characteristics,
33 many things must be changed that use `unsigned short int's.
34
35 The characteristics are stored always in network byte order (big
36 endian). We define the bit value interpretations here dependent on the
37 machine's byte order. */
38
39 # include <bits/endian.h>
40 # if __BYTE_ORDER == __BIG_ENDIAN
41 # define _ISbit(bit) (1 << (bit))
42 # else /* __BYTE_ORDER == __LITTLE_ENDIAN */
43 # define _ISbit(bit) ((bit) < 8 ? ((1 << (bit)) << 8) : ((1 << (bit)) >> 8))
44 # endif
45
46 enum
47 {
48 _ISupper = _ISbit (0), /* UPPERCASE. */
49 _ISlower = _ISbit (1), /* lowercase. */
50 _ISalpha = _ISbit (2), /* Alphabetic. */
51 _ISdigit = _ISbit (3), /* Numeric. */
52 _ISxdigit = _ISbit (4), /* Hexadecimal numeric. */
53 _ISspace = _ISbit (5), /* Whitespace. */
54 _ISprint = _ISbit (6), /* Printing. */
55 _ISgraph = _ISbit (7), /* Graphical. */
56 _ISblank = _ISbit (8), /* Blank (usually SPC and TAB). */
57 _IScntrl = _ISbit (9), /* Control character. */
58 _ISpunct = _ISbit (10), /* Punctuation. */
59 _ISalnum = _ISbit (11) /* Alphanumeric. */
60 };
61 #endif /* ! _ISbit */
62
63 /* These are defined in ctype-info.c.
64 The declarations here must match those in localeinfo.h.
65
66 In the thread-specific locale model (see `uselocale' in <locale.h>)
67 we cannot use global variables for these as was done in the past.
68 Instead, the following accessor functions return the address of
69 each variable, which is local to the current thread if multithreaded.
70
71 These point into arrays of 384, so they can be indexed by any `unsigned
72 char' value [0,255]; by EOF (-1); or by any `signed char' value
73 [-128,-1). ISO C requires that the ctype functions work for `unsigned
74 char' values and for EOF; we also support negative `signed char' values
75 for broken old programs. The case conversion arrays are of `int's
76 rather than `unsigned char's because tolower (EOF) must be EOF, which
77 doesn't fit into an `unsigned char'. But today more important is that
78 the arrays are also used for multi-byte character sets. */
79 extern const unsigned short int **__ctype_b_loc (void)
80 __THROW __attribute__ ((__const__));
81 extern const __int32_t **__ctype_tolower_loc (void)
82 __THROW __attribute__ ((__const__));
83 extern const __int32_t **__ctype_toupper_loc (void)
84 __THROW __attribute__ ((__const__));
85
86
87 #ifndef __cplusplus
88 # define __isctype(c, type) \
89 ((*__ctype_b_loc ())[(int) (c)] & (unsigned short int) type)
90 #elif defined __USE_EXTERN_INLINES
91 # define __isctype_f(type) \
92 __extern_inline int \
93 is##type (int __c) __THROW \
94 { \
95 return (*__ctype_b_loc ())[(int) (__c)] & (unsigned short int) _IS##type; \
96 }
97 #endif
98
99 #define __isascii(c) (((c) & ~0x7f) == 0) /* If C is a 7 bit value. */
100 #define __toascii(c) ((c) & 0x7f) /* Mask off high bits. */
101
102 #define __exctype(name) extern int name (int) __THROW
103
104 /* The following names are all functions:
105 int isCHARACTERISTIC(int c);
106 which return nonzero iff C has CHARACTERISTIC.
107 For the meaning of the characteristic names, see the `enum' above. */
108 __exctype (isalnum);
109 __exctype (isalpha);
110 __exctype (iscntrl);
111 __exctype (isdigit);
112 __exctype (islower);
113 __exctype (isgraph);
114 __exctype (isprint);
115 __exctype (ispunct);
116 __exctype (isspace);
117 __exctype (isupper);
118 __exctype (isxdigit);
119
120
121 /* Return the lowercase version of C. */
122 extern int tolower (int __c) __THROW;
123
124 /* Return the uppercase version of C. */
125 extern int toupper (int __c) __THROW;
126
127
128 /* ISO C99 introduced one new function. */
129 #ifdef __USE_ISOC99
130 __exctype (isblank);
131 #endif
132
133 #ifdef __USE_GNU
134 /* Test C for a set of character classes according to MASK. */
135 extern int isctype (int __c, int __mask) __THROW;
136 #endif
137
138 #if defined __USE_MISC || defined __USE_XOPEN
139
140 /* Return nonzero iff C is in the ASCII set
141 (i.e., is no more than 7 bits wide). */
142 extern int isascii (int __c) __THROW;
143
144 /* Return the part of C that is in the ASCII set
145 (i.e., the low-order 7 bits of C). */
146 extern int toascii (int __c) __THROW;
147
148 /* These are the same as `toupper' and `tolower' except that they do not
149 check the argument for being in the range of a `char'. */
150 __exctype (_toupper);
151 __exctype (_tolower);
152 #endif /* Use X/Open or use misc. */
153
154 /* This code is needed for the optimized mapping functions. */
155 #define __tobody(c, f, a, args) \
156 (__extension__ \
157 ({ int __res; \
158 if (sizeof (c) > 1) \
159 { \
160 if (__builtin_constant_p (c)) \
161 { \
162 int __c = (c); \
163 __res = __c < -128 || __c > 255 ? __c : (a)[__c]; \
164 } \
165 else \
166 __res = f args; \
167 } \
168 else \
169 __res = (a)[(int) (c)]; \
170 __res; }))
171
172 #if !defined __NO_CTYPE
173 # ifdef __isctype_f
174 __isctype_f (alnum)
__isctype_f(alpha)175 __isctype_f (alpha)
176 __isctype_f (cntrl)
177 __isctype_f (digit)
178 __isctype_f (lower)
179 __isctype_f (graph)
180 __isctype_f (print)
181 __isctype_f (punct)
182 __isctype_f (space)
183 __isctype_f (upper)
184 __isctype_f (xdigit)
185 # ifdef __USE_ISOC99
186 __isctype_f (blank)
187 # endif
188 # elif defined __isctype
189 # define isalnum(c) __isctype((c), _ISalnum)
190 # define isalpha(c) __isctype((c), _ISalpha)
191 # define iscntrl(c) __isctype((c), _IScntrl)
192 # define isdigit(c) __isctype((c), _ISdigit)
193 # define islower(c) __isctype((c), _ISlower)
194 # define isgraph(c) __isctype((c), _ISgraph)
195 # define isprint(c) __isctype((c), _ISprint)
196 # define ispunct(c) __isctype((c), _ISpunct)
197 # define isspace(c) __isctype((c), _ISspace)
198 # define isupper(c) __isctype((c), _ISupper)
199 # define isxdigit(c) __isctype((c), _ISxdigit)
200 # ifdef __USE_ISOC99
201 # define isblank(c) __isctype((c), _ISblank)
202 # endif
203 # endif
204
205 # ifdef __USE_EXTERN_INLINES
206 __extern_inline int
207 __NTH (tolower (int __c))
208 {
209 return __c >= -128 && __c < 256 ? (*__ctype_tolower_loc ())[__c] : __c;
210 }
211
212 __extern_inline int
__NTH(toupper (int __c))213 __NTH (toupper (int __c))
214 {
215 return __c >= -128 && __c < 256 ? (*__ctype_toupper_loc ())[__c] : __c;
216 }
217 # endif
218
219 # if __GNUC__ >= 2 && defined __OPTIMIZE__ && !defined __cplusplus
220 # define tolower(c) __tobody (c, tolower, *__ctype_tolower_loc (), (c))
221 # define toupper(c) __tobody (c, toupper, *__ctype_toupper_loc (), (c))
222 # endif /* Optimizing gcc */
223
224 # if defined __USE_MISC || defined __USE_XOPEN
225 # define isascii(c) __isascii (c)
226 # define toascii(c) __toascii (c)
227
228 # define _tolower(c) ((int) (*__ctype_tolower_loc ())[(int) (c)])
229 # define _toupper(c) ((int) (*__ctype_toupper_loc ())[(int) (c)])
230 # endif
231
232 #endif /* Not __NO_CTYPE. */
233
234
235 #ifdef __USE_XOPEN2K8
236 /* POSIX.1-2008 extended locale interface (see locale.h). */
237 # include <bits/types/locale_t.h>
238
239 /* These definitions are similar to the ones above but all functions
240 take as an argument a handle for the locale which shall be used. */
241 # define __isctype_l(c, type, locale) \
242 ((locale)->__ctype_b[(int) (c)] & (unsigned short int) type)
243
244 # define __exctype_l(name) \
245 extern int name (int, locale_t) __THROW
246
247 /* The following names are all functions:
248 int isCHARACTERISTIC(int c, locale_t *locale);
249 which return nonzero iff C has CHARACTERISTIC.
250 For the meaning of the characteristic names, see the `enum' above. */
251 __exctype_l (isalnum_l);
252 __exctype_l (isalpha_l);
253 __exctype_l (iscntrl_l);
254 __exctype_l (isdigit_l);
255 __exctype_l (islower_l);
256 __exctype_l (isgraph_l);
257 __exctype_l (isprint_l);
258 __exctype_l (ispunct_l);
259 __exctype_l (isspace_l);
260 __exctype_l (isupper_l);
261 __exctype_l (isxdigit_l);
262
263 __exctype_l (isblank_l);
264
265
266 /* Return the lowercase version of C in locale L. */
267 extern int __tolower_l (int __c, locale_t __l) __THROW;
268 extern int tolower_l (int __c, locale_t __l) __THROW;
269
270 /* Return the uppercase version of C. */
271 extern int __toupper_l (int __c, locale_t __l) __THROW;
272 extern int toupper_l (int __c, locale_t __l) __THROW;
273
274 # if __GNUC__ >= 2 && defined __OPTIMIZE__ && !defined __cplusplus
275 # define __tolower_l(c, locale) \
276 __tobody (c, __tolower_l, (locale)->__ctype_tolower, (c, locale))
277 # define __toupper_l(c, locale) \
278 __tobody (c, __toupper_l, (locale)->__ctype_toupper, (c, locale))
279 # define tolower_l(c, locale) __tolower_l ((c), (locale))
280 # define toupper_l(c, locale) __toupper_l ((c), (locale))
281 # endif /* Optimizing gcc */
282
283
284 # ifndef __NO_CTYPE
285 # define __isalnum_l(c,l) __isctype_l((c), _ISalnum, (l))
286 # define __isalpha_l(c,l) __isctype_l((c), _ISalpha, (l))
287 # define __iscntrl_l(c,l) __isctype_l((c), _IScntrl, (l))
288 # define __isdigit_l(c,l) __isctype_l((c), _ISdigit, (l))
289 # define __islower_l(c,l) __isctype_l((c), _ISlower, (l))
290 # define __isgraph_l(c,l) __isctype_l((c), _ISgraph, (l))
291 # define __isprint_l(c,l) __isctype_l((c), _ISprint, (l))
292 # define __ispunct_l(c,l) __isctype_l((c), _ISpunct, (l))
293 # define __isspace_l(c,l) __isctype_l((c), _ISspace, (l))
294 # define __isupper_l(c,l) __isctype_l((c), _ISupper, (l))
295 # define __isxdigit_l(c,l) __isctype_l((c), _ISxdigit, (l))
296
297 # define __isblank_l(c,l) __isctype_l((c), _ISblank, (l))
298
299 # ifdef __USE_MISC
300 # define __isascii_l(c,l) ((l), __isascii (c))
301 # define __toascii_l(c,l) ((l), __toascii (c))
302 # endif
303
304 # define isalnum_l(c,l) __isalnum_l ((c), (l))
305 # define isalpha_l(c,l) __isalpha_l ((c), (l))
306 # define iscntrl_l(c,l) __iscntrl_l ((c), (l))
307 # define isdigit_l(c,l) __isdigit_l ((c), (l))
308 # define islower_l(c,l) __islower_l ((c), (l))
309 # define isgraph_l(c,l) __isgraph_l ((c), (l))
310 # define isprint_l(c,l) __isprint_l ((c), (l))
311 # define ispunct_l(c,l) __ispunct_l ((c), (l))
312 # define isspace_l(c,l) __isspace_l ((c), (l))
313 # define isupper_l(c,l) __isupper_l ((c), (l))
314 # define isxdigit_l(c,l) __isxdigit_l ((c), (l))
315
316 # define isblank_l(c,l) __isblank_l ((c), (l))
317
318 # ifdef __USE_MISC
319 # define isascii_l(c,l) __isascii_l ((c), (l))
320 # define toascii_l(c,l) __toascii_l ((c), (l))
321 # endif
322
323 # endif /* Not __NO_CTYPE. */
324
325 #endif /* Use POSIX 2008. */
326
327 __END_DECLS
328
329 #endif /* ctype.h */
330