1 /* Copyright (C) 1991,92,93,95,96,97,98,99,2001,02
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 * ISO C99 Standard 7.4: Character handling <ctype.h>
28 #include <bits/types.h>
33 /* The following names are all functions:
34 int isCHARACTERISTIC(int c);
35 which return nonzero iff C has CHARACTERISTIC.
36 For the meaning of the characteristic names, see the `enum' above. */
37 extern int isalnum(int __c) __THROW;
38 libc_hidden_proto(isalnum)
39 extern int isalpha(int __c) __THROW;
40 libc_hidden_proto(isalpha)
41 extern int iscntrl(int __c) __THROW;
42 libc_hidden_proto(iscntrl)
43 extern int isdigit(int __c) __THROW;
44 libc_hidden_proto(isdigit)
45 extern int islower(int __c) __THROW;
46 libc_hidden_proto(islower)
47 extern int isgraph(int __c) __THROW;
48 libc_hidden_proto(isgraph)
49 extern int isprint(int __c) __THROW;
50 libc_hidden_proto(isprint)
51 extern int ispunct(int __c) __THROW;
52 libc_hidden_proto(ispunct)
53 extern int isspace(int __c) __THROW;
54 libc_hidden_proto(isspace)
55 extern int isupper(int __c) __THROW;
56 libc_hidden_proto(isupper)
57 extern int isxdigit(int __c) __THROW;
58 libc_hidden_proto(isxdigit)
61 /* Return the lowercase version of C. */
62 extern int tolower(int __c) __THROW;
63 libc_hidden_proto(tolower)
65 /* Return the uppercase version of C. */
66 extern int toupper(int __c) __THROW;
67 libc_hidden_proto(toupper)
69 #if (defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN) && \
70 defined __UCLIBC_SUSV4_LEGACY__
71 /* Return nonzero iff C is in the ASCII set
72 (i.e., is no more than 7 bits wide). */
73 extern int isascii(int __c) __THROW;
74 libc_hidden_proto(isascii)
75 /* Return the part of C that is in the ASCII set
76 (i.e., the low-order 7 bits of C). */
77 extern int toascii(int __c) __THROW;
83 /* ISO C99 introduced one new function. */
87 extern int isblank(int __c) __THROW;
88 libc_hidden_proto(isblank)
94 #ifndef __UCLIBC_HAS_CTYPE_TABLES__
96 /* "Stub locale": we are permanently in C/POSIX locale.
97 * Using simple(r) ctype.h machinery in this header instead: */
98 #include <bits/uClibc_ctype.h>
105 /* These are all the characteristics of characters.
106 If there get to be more than 16 distinct characteristics,
107 __ctype_mask_t will need to be adjusted. */
109 /* libstdc++ from gcc toolchain needs this macro. */
110 # define _ISbit(bit) (1 << (bit))
114 _ISupper = _ISbit (0), /* UPPERCASE. */
115 _ISlower = _ISbit (1), /* lowercase. */
116 _ISalpha = _ISbit (2), /* Alphabetic. */
117 _ISdigit = _ISbit (3), /* Numeric. */
118 _ISxdigit = _ISbit (4), /* Hexadecimal numeric. */
119 _ISspace = _ISbit (5), /* Whitespace. */
120 _ISprint = _ISbit (6), /* Printing. */
121 _ISgraph = _ISbit (7), /* Graphical. */
122 _ISblank = _ISbit (8), /* Blank (usually SPC and TAB). */
123 _IScntrl = _ISbit (9), /* Control character. */
124 _ISpunct = _ISbit (10), /* Punctuation. */
125 _ISalnum = _ISbit (11) /* Alphanumeric. */
128 #error _ISbit already defined!
129 #endif /* ! _ISbit */
131 /* __ctype_XXX_t types and __UCLIBC_CTYPE_x_TBL_OFFSET constants */
132 #include <bits/uClibc_touplow.h>
134 #ifdef __UCLIBC_HAS_CTYPE_SIGNED__
135 # define __UCLIBC_CTYPE_IN_TO_DOMAIN(c) (((unsigned int)((c) + 128)) < 384)
137 # define __UCLIBC_CTYPE_IN_TO_DOMAIN(c) (((unsigned int)(c)) < 256)
140 /* In the thread-specific locale model (see `uselocale' in <locale.h>)
141 we cannot use global variables for these as was done in the past.
142 Instead, the following accessor functions return the address of
143 each variable, which is local to the current thread if multithreaded.
145 These point into arrays of 384, so they can be indexed by any `unsigned
146 char' value [0,255]; by EOF (-1); or by any `signed char' value
147 [-128,-1). ISO C requires that the ctype functions work for `unsigned
148 char' values and for EOF; we also support negative `signed char' values
149 for broken old programs. The case conversion arrays are of `int's
150 rather than `unsigned char's because tolower (EOF) must be EOF, which
151 doesn't fit into an `unsigned char'. But today more important is that
152 the arrays are also used for multi-byte character sets. */
154 /* uClibc differences:
156 * When __UCLIBC_HAS_CTYPE_SIGNED is defined,
158 * The upper and lower mapping arrays are type int16_t, so that
159 * they may store all char values plus EOF. The glibc reasoning
160 * given above for these being type int is questionable, as the
161 * ctype mapping functions map from the set of (unsigned) char
162 * and EOF back into the set. They have no awareness of multi-byte
163 * or wide characters.
167 * The ctype array is defined for -1..255.
168 * The upper and lower mapping arrays are defined for 0..255.
169 * The upper and lower mapping arrays are type unsigned char.
172 /* Pointers to the default C-locale data. */
173 extern const __ctype_mask_t *__C_ctype_b;
174 libc_hidden_proto(__C_ctype_b)
175 extern const __ctype_touplow_t *__C_ctype_toupper;
176 libc_hidden_proto(__C_ctype_toupper)
177 extern const __ctype_touplow_t *__C_ctype_tolower;
178 libc_hidden_proto(__C_ctype_tolower)
180 #ifdef __UCLIBC_HAS_XLOCALE__
182 const __ctype_mask_t **__ctype_b_loc(void) __attribute__ ((const));
183 libc_hidden_proto(__ctype_b_loc)
184 const __ctype_touplow_t **__ctype_tolower_loc(void) __attribute__ ((const));
185 libc_hidden_proto(__ctype_tolower_loc)
186 const __ctype_touplow_t **__ctype_toupper_loc(void) __attribute__ ((const));
187 libc_hidden_proto(__ctype_toupper_loc)
188 #define __UCLIBC_CTYPE_B (*__ctype_b_loc())
189 #define __UCLIBC_CTYPE_TOLOWER (*__ctype_tolower_loc())
190 #define __UCLIBC_CTYPE_TOUPPER (*__ctype_toupper_loc())
192 #else /* __UCLIBC_HAS_XLOCALE__ */
194 /* Pointers to the current global locale data in use. */
195 extern const __ctype_mask_t *__ctype_b;
196 libc_hidden_proto(__ctype_b)
197 extern const __ctype_touplow_t *__ctype_toupper;
198 libc_hidden_proto(__ctype_toupper)
199 extern const __ctype_touplow_t *__ctype_tolower;
200 libc_hidden_proto(__ctype_tolower)
201 #define __UCLIBC_CTYPE_B (__ctype_b)
202 #define __UCLIBC_CTYPE_TOLOWER (__ctype_tolower)
203 #define __UCLIBC_CTYPE_TOUPPER (__ctype_toupper)
205 #endif /* __UCLIBC_HAS_XLOCALE__ */
207 #ifdef __UCLIBC_SUSV4_LEGACY__
208 #define __isascii(c) (((c) & ~0x7f) == 0) /* If C is a 7 bit value. */
209 #define __toascii(c) ((c) & 0x7f) /* Mask off high bits. */
213 /* These are uClibc-specific. */
214 #define __isdigit_char(C) (((unsigned char)((C) - '0')) <= 9)
215 #define __isdigit_int(C) (((unsigned int)((C) - '0')) <= 9)
219 /* Test C for a set of character classes according to MASK. */
220 extern int isctype(int __c, int __mask) __THROW;
223 #if (defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN) \
224 && defined __UCLIBC_SUSV4_LEGACY__
225 /* These are the same as `toupper' and `tolower' except that they do not
226 check the argument for being in the range of a `char'. */
227 extern int _toupper(int __c) __THROW;
228 extern int _tolower(int __c) __THROW;
229 #endif /* Use SVID or use misc. */
231 /* This code is needed for the optimized mapping functions. */
232 #define __tobody(c, f, table, args) \
235 if (sizeof(c) > 1) { \
236 if (__builtin_constant_p(c)) { \
238 __res = __UCLIBC_CTYPE_IN_TO_DOMAIN(__c) ? (table)[__c] : __c; \
242 __res = (table)[(int) (c)]; \
246 #define __isctype(c, type) ((__UCLIBC_CTYPE_B)[(int)(c)] & (__ctype_mask_t)type)
247 /* Do not combine in one #if - unifdef tool is not that clever */
250 # define isalnum(c) __isctype((c), _ISalnum)
251 # define isalpha(c) __isctype((c), _ISalpha)
252 # define iscntrl(c) __isctype((c), _IScntrl)
253 # define isdigit(c) __isctype((c), _ISdigit)
254 # define islower(c) __isctype((c), _ISlower)
255 # define isgraph(c) __isctype((c), _ISgraph)
256 # define isprint(c) __isctype((c), _ISprint)
257 # define ispunct(c) __isctype((c), _ISpunct)
258 # define isspace(c) __isctype((c), _ISspace)
259 # define isupper(c) __isctype((c), _ISupper)
260 # define isxdigit(c) __isctype((c), _ISxdigit)
262 # define isblank(c) __isctype((c), _ISblank)
265 # ifdef __USE_EXTERN_INLINES
267 __NTH (tolower (int __c))
269 return __UCLIBC_CTYPE_IN_TO_DOMAIN(__c) ? (__UCLIBC_CTYPE_TOLOWER)[__c] : __c;
272 __NTH (toupper (int __c))
274 return __UCLIBC_CTYPE_IN_TO_DOMAIN(__c) ? (__UCLIBC_CTYPE_TOUPPER)[__c] : __c;
278 # if __GNUC__ >= 2 && defined __OPTIMIZE__ && !defined __cplusplus
279 # define tolower(c) __tobody(c, tolower, __UCLIBC_CTYPE_TOLOWER, (c))
280 # define toupper(c) __tobody(c, toupper, __UCLIBC_CTYPE_TOUPPER, (c))
281 # endif /* Optimizing gcc */
283 # if (defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN) \
284 && defined __UCLIBC_SUSV4_LEGACY__
285 # define isascii(c) __isascii (c)
286 # define toascii(c) __toascii (c)
287 # define _tolower(c) ((int) (__UCLIBC_CTYPE_TOLOWER)[(int) (c)])
288 # define _toupper(c) ((int) (__UCLIBC_CTYPE_TOUPPER)[(int) (c)])
291 #endif /* not __cplusplus */
292 #endif /* not __NO_CTYPE */
295 #if defined __USE_GNU && defined __UCLIBC_HAS_XLOCALE__
296 /* The concept of one static locale per category is not very well
297 thought out. Many applications will need to process its data using
298 information from several different locales. Another application is
299 the implementation of the internationalization handling in the
300 upcoming ISO C++ standard library. To support this another set of
301 the functions using locale data exist which have an additional
304 Attention: all these functions are *not* standardized in any form.
305 This is a proof-of-concept implementation. */
307 /* Structure for reentrant locale using functions. This is an
308 (almost) opaque type for the user level programs. */
309 # include <xlocale.h>
311 /* These definitions are similar to the ones above but all functions
312 take as an argument a handle for the locale which shall be used. */
313 extern int isalnum_l(int, __locale_t) __THROW;
314 libc_hidden_proto(isalnum_l)
315 extern int isalpha_l(int, __locale_t) __THROW;
316 libc_hidden_proto(isalpha_l)
317 extern int iscntrl_l(int, __locale_t) __THROW;
318 libc_hidden_proto(iscntrl_l)
319 extern int isdigit_l(int, __locale_t) __THROW;
320 libc_hidden_proto(isdigit_l)
321 extern int islower_l(int, __locale_t) __THROW;
322 libc_hidden_proto(islower_l)
323 extern int isgraph_l(int, __locale_t) __THROW;
324 libc_hidden_proto(isgraph_l)
325 extern int isprint_l(int, __locale_t) __THROW;
326 libc_hidden_proto(isprint_l)
327 extern int ispunct_l(int, __locale_t) __THROW;
328 libc_hidden_proto(ispunct_l)
329 extern int isspace_l(int, __locale_t) __THROW;
330 libc_hidden_proto(isspace_l)
331 extern int isupper_l(int, __locale_t) __THROW;
332 libc_hidden_proto(isupper_l)
333 extern int isxdigit_l(int, __locale_t) __THROW;
334 libc_hidden_proto(isxdigit_l)
335 extern int isblank_l(int, __locale_t) __THROW;
336 libc_hidden_proto(isblank_l)
338 # if (defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN) \
339 && defined __UCLIBC_SUSV4_LEGACY__
340 /* Return nonzero iff C is in the ASCII set
341 (i.e., is no more than 7 bits wide). */
342 extern int isascii_l (int __c) __THROW;
343 libc_hidden_proto(isascii_l)
347 /* Return the lowercase version of C in locale L. */
348 /* "ordinary" ctype.h has no __tolower, why we try to have it?
349 * remove after 0.9.31
350 *extern int __tolower_l (int __c, __locale_t __l) __THROW; */
351 extern int tolower_l (int __c, __locale_t __l) __THROW;
352 libc_hidden_proto(tolower_l)
354 /* Return the uppercase version of C. */
355 /*extern int __toupper_l (int __c, __locale_t __l) __THROW; */
356 extern int toupper_l (int __c, __locale_t __l) __THROW;
358 # if __GNUC__ >= 2 && defined __OPTIMIZE__ && !defined __cplusplus
359 # define tolower_l(c, locale) __tobody(c, tolower_l, (locale)->__ctype_tolower, (c, locale))
360 # define toupper_l(c, locale) __tobody(c, toupper_l, (locale)->__ctype_toupper, (c, locale))
361 /*# define __tolower_l(c, locale) tolower_l((c), (locale)) */
362 /*# define __toupper_l(c, locale) toupper_l((c), (locale)) */
363 # endif /* Optimizing gcc */
366 # define __isctype_l(c, type, locale) ((locale)->__ctype_b[(int) (c)] & (__ctype_mask_t) type)
368 # define __isalnum_l(c,l) __isctype_l((c), _ISalnum, (l))
369 # define __isalpha_l(c,l) __isctype_l((c), _ISalpha, (l))
370 # define __iscntrl_l(c,l) __isctype_l((c), _IScntrl, (l))
371 # define __isdigit_l(c,l) __isctype_l((c), _ISdigit, (l))
372 # define __islower_l(c,l) __isctype_l((c), _ISlower, (l))
373 # define __isgraph_l(c,l) __isctype_l((c), _ISgraph, (l))
374 # define __isprint_l(c,l) __isctype_l((c), _ISprint, (l))
375 # define __ispunct_l(c,l) __isctype_l((c), _ISpunct, (l))
376 # define __isspace_l(c,l) __isctype_l((c), _ISspace, (l))
377 # define __isupper_l(c,l) __isctype_l((c), _ISupper, (l))
378 # define __isxdigit_l(c,l) __isctype_l((c), _ISxdigit, (l))
379 # define __isblank_l(c,l) __isctype_l((c), _ISblank, (l))
381 # if (defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN) \
382 && defined __UCLIBC_SUSV4_LEGACY__
383 # define __isascii_l(c,l) ((l), __isascii (c))
384 # define __toascii_l(c,l) ((l), __toascii (c))
387 # define isalnum_l(c,l) __isalnum_l ((c), (l))
388 # define isalpha_l(c,l) __isalpha_l ((c), (l))
389 # define iscntrl_l(c,l) __iscntrl_l ((c), (l))
390 # define isdigit_l(c,l) __isdigit_l ((c), (l))
391 # define islower_l(c,l) __islower_l ((c), (l))
392 # define isgraph_l(c,l) __isgraph_l ((c), (l))
393 # define isprint_l(c,l) __isprint_l ((c), (l))
394 # define ispunct_l(c,l) __ispunct_l ((c), (l))
395 # define isspace_l(c,l) __isspace_l ((c), (l))
396 # define isupper_l(c,l) __isupper_l ((c), (l))
397 # define isxdigit_l(c,l) __isxdigit_l ((c), (l))
398 # define isblank_l(c,l) __isblank_l ((c), (l))
400 # if (defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN) \
401 && defined __UCLIBC_SUSV4_LEGACY__
402 # define isascii_l(c,l) __isascii_l ((c), (l))
403 # define toascii_l(c,l) __toascii_l ((c), (l))
406 # endif /* Not __NO_CTYPE. */
408 #endif /* Use GNU. */
412 #endif /* __UCLIBC_HAS_CTYPE_TABLES__ */
414 /* We define {__,}isascii for internal use only */
415 #if defined _LIBC && !defined __UCLIBC_SUSV4_LEGACY__
416 # define __isascii(c) (((c) & ~0x7f) == 0)
417 # define isascii(c) __isascii (c)