1 /* Copyright (C) 2002 Manuel Novoa III
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Library General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
13 * You should have received a copy of the GNU Library General Public
14 * License along with this library; if not, write to the Free
15 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
20 * Besides uClibc, I'm using this code in my libc for elks, which is
21 * a 16-bit environment with a fairly limited compiler. It would make
22 * things much easier for me if this file isn't modified unnecessarily.
23 * In particular, please put any new or replacement functions somewhere
24 * else, and modify the makefile to use your version instead.
27 * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
29 #if !defined(_CTYPE_H) && !defined(_WCTYPE_H)
30 #error Always include <{w}ctype.h> rather than <bits/uClibc_ctype.h>
36 #ifdef __UCLIBC_GEN_LOCALE
38 /* Taking advantage of the C99 mutual-exclusion guarantees for the various
39 * (w)ctype classes, including the descriptions of printing and control
40 * (w)chars, we can place each in one of the following mutually-exlusive
41 * subsets. Since there are less than 16, we can store the data for
42 * each (w)chars in a nibble. In contrast, glibc uses an unsigned int
43 * per (w)char, with one bit flag for each is* type. While this allows
44 * a simple '&' operation to determine the type vs. a range test and a
45 * little special handling for the "blank" and "xdigit" types in my
46 * approach, it also uses 8 times the space for the tables on the typical
47 * 32-bit archs we supported.*/
49 __CTYPE_unclassified = 0,
50 __CTYPE_alpha_nonupper_nonlower,
52 __CTYPE_alpha_upper_lower,
57 __CTYPE_print_space_nonblank,
58 __CTYPE_print_space_blank,
59 __CTYPE_space_nonblank_noncntrl,
60 __CTYPE_space_blank_noncntrl,
61 __CTYPE_cntrl_space_nonblank,
62 __CTYPE_cntrl_space_blank,
63 __CTYPE_cntrl_nonspace
66 /* Some macros that test for various (w)ctype classes when passed one of the
67 * designator values enumerated above. */
68 #define __CTYPE_isalnum(D) ((unsigned int)(D-1) <= (__CTYPE_digit-1))
69 #define __CTYPE_isalpha(D) ((unsigned int)(D-1) <= (__CTYPE_alpha_upper-1))
70 #define __CTYPE_isblank(D) \
71 ((((unsigned int)(D - __CTYPE_print_space_nonblank)) <= 5) && (D & 1))
72 #define __CTYPE_iscntrl(D) (((unsigned int)(D - __CTYPE_cntrl_space_nonblank)) <= 2)
73 #define __CTYPE_isdigit(D) (D == __CTYPE_digit)
74 #define __CTYPE_isgraph(D) ((unsigned int)(D-1) <= (__CTYPE_graph-1))
75 #define __CTYPE_islower(D) (((unsigned int)(D - __CTYPE_alpha_lower)) <= 1)
76 #define __CTYPE_isprint(D) ((unsigned int)(D-1) <= (__CTYPE_print_space_blank-1))
77 #define __CTYPE_ispunct(D) (D == __CTYPE_punct)
78 #define __CTYPE_isspace(D) (((unsigned int)(D - __CTYPE_print_space_nonblank)) <= 5)
79 #define __CTYPE_isupper(D) (((unsigned int)(D - __CTYPE_alpha_upper_lower)) <= 1)
80 /* #define __CTYPE_isxdigit(D) -- isxdigit is untestable this way.
81 * But that's ok as isxdigit() (and isdigit() too) are locale-invariant. */
83 #else /* __UCLIBC_GEN_LOCALE *****************************************/
85 /* Define some ctype macros valid for the C/POSIX locale. */
87 /* ASCII ords of \t, \f, \n, \r, and \v are 9, 12, 10, 13, 11 respectively. */
88 #define __C_isspace(c) \
89 ((sizeof(c) == sizeof(char)) \
90 ? ((((c) == ' ') || (((unsigned char)((c) - 9)) <= (13 - 9)))) \
91 : ((((c) == ' ') || (((unsigned int)((c) - 9)) <= (13 - 9)))))
92 #define __C_isblank(c) (((c) == ' ') || ((c) == '\t'))
93 #define __C_isdigit(c) \
94 ((sizeof(c) == sizeof(char)) \
95 ? (((unsigned char)((c) - '0')) < 10) \
96 : (((unsigned int)((c) - '0')) < 10))
97 #define __C_isxdigit(c) \
99 || ((sizeof(c) == sizeof(char)) \
100 ? (((unsigned char)((((c)) | 0x20) - 'a')) < 6) \
101 : (((unsigned int)((((c)) | 0x20) - 'a')) < 6)))
102 #define __C_iscntrl(c) \
103 ((sizeof(c) == sizeof(char)) \
104 ? ((((unsigned char)(c)) < 0x20) || ((c) == 0x7f)) \
105 : ((((unsigned int)(c)) < 0x20) || ((c) == 0x7f)))
106 #define __C_isalpha(c) \
107 ((sizeof(c) == sizeof(char)) \
108 ? (((unsigned char)(((c) | 0x20) - 'a')) < 26) \
109 : (((unsigned int)(((c) | 0x20) - 'a')) < 26))
110 #define __C_isalnum(c) (__C_isalpha(c) || __C_isdigit(c))
111 #define __C_isprint(c) \
112 ((sizeof(c) == sizeof(char)) \
113 ? (((unsigned char)((c) - 0x20)) <= (0x7e - 0x20)) \
114 : (((unsigned int)((c) - 0x20)) <= (0x7e - 0x20)))
115 #define __C_islower(c) \
116 ((sizeof(c) == sizeof(char)) \
117 ? (((unsigned char)((c) - 'a')) < 26) \
118 : (((unsigned int)((c) - 'a')) < 26))
119 #define __C_isupper(c) \
120 ((sizeof(c) == sizeof(char)) \
121 ? (((unsigned char)((c) - 'A')) < 26) \
122 : (((unsigned int)((c) - 'A')) < 26))
123 #define __C_ispunct(c) \
125 && ((sizeof(c) == sizeof(char)) \
126 ? (((unsigned char)((c) - 0x21)) <= (0x7e - 0x21)) \
127 : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21))))
128 #define __C_isgraph(c) \
129 ((sizeof(c) == sizeof(char)) \
130 ? (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21)) \
131 : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21)))
133 #define __C_tolower(c) (__C_isupper(c) ? ((c) | 0x20) : (c))
134 #define __C_toupper(c) (__C_islower(c) ? ((c) ^ 0x20) : (c))
136 #define __C_isxlower(c) \
138 || ((sizeof(c) == sizeof(char)) \
139 ? (((unsigned char)(((c)) - 'a')) < 6) \
140 : (((unsigned int)(((c)) - 'a')) < 6)))
141 #define __C_isxupper(c) \
143 || ((sizeof(c) == sizeof(char)) \
144 ? (((unsigned char)(((c)) - 'A')) < 6) \
145 : (((unsigned int)(((c)) - 'A')) < 6)))
147 /**********************************************************************/
150 extern int isalnum(int c) __THROW;
151 extern int isalpha(int c) __THROW;
153 extern int isblank(int c) __THROW;
155 extern int iscntrl(int c) __THROW;
156 extern int isdigit(int c) __THROW;
157 extern int isgraph(int c) __THROW;
158 extern int islower(int c) __THROW;
159 extern int isprint(int c) __THROW;
160 extern int ispunct(int c) __THROW;
161 extern int isspace(int c) __THROW;
162 extern int isupper(int c) __THROW;
163 extern int isxdigit(int c) __THROW;
165 extern int tolower(int c) __THROW;
166 extern int toupper(int c) __THROW;
168 #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
169 extern int isascii(int c) __THROW;
170 extern int toascii(int c) __THROW;
173 /* The following are included for compatibility with older versions of
174 * uClibc; but now they're only visible if MISC funcctionality is requested.
175 * However, as they are locale-independent, the hidden macro versions are
178 extern int isxlower(int c) __THROW; /* uClibc-specific. */
179 extern int isxupper(int c) __THROW; /* uClibc-specific. */
181 /* isdigit() is really locale-invariant, so provide some small fast macros.
182 * These are uClibc-specific. */
183 #define __isdigit_char(C) (((unsigned char)((C) - '0')) <= 9)
184 #define __isdigit_int(C) (((unsigned int)((C) - '0')) <= 9)
187 /* Next, some ctype macros which are valid for all supported locales. */
188 /* WARNING: isspace and isblank need to be reverified if more 8-bit codesets
189 * are added!!! But isdigit and isxdigit are always valid. */
191 /* #define __isspace(c) __C_isspace(c) */
192 /* #define __isblank(c) __C_isblank(c) */
194 /* #define __isdigit(c) __C_isdigit(c) */
195 /* #define __isxdigit(c) __C_isxdigit(c) */
197 /* Now some non-ansi/iso c99 macros. */
199 #define __isascii(c) (((c) & ~0x7f) == 0)
200 #define __toascii(c) ((c) & 0x7f)
201 #define _toupper(c) ((c) ^ 0x20)
202 #define _tolower(c) ((c) | 0x20)
205 /* For compatibility with older versions of uClibc. Are these ever used? */
207 #define __isxlower(c) __C_isxlower(c) /* uClibc-specific. */
208 #define __isxupper(c) __C_isxupper(c) /* uClibc-specific. */
211 /* Apparently, glibc implements things as macros if __NO_CTYPE isn't defined.
212 * If we don't have locale support, we'll do the same. Otherwise, we'll
213 * only use macros for the supported-locale-invariant cases. */
214 #ifndef __UCLIBC_HAS_LOCALE__
216 #endif /* __UCLIBC_HAS_LOCALE__ */
220 /**********************************************************************/
223 #define __isbody_C_macro(f,args) __C_ ## f args
225 #define __isbody(f,c) \
228 if (sizeof(c) > sizeof(char)) { \
230 __res = __isbody_C_macro(f,(__c)); \
232 unsigned char __c = (c); \
233 __res = __isbody_C_macro(f,(__c)); \
238 #define __body_C_macro(f,args) __C_ ## f args
240 #define __body(f,c) \
243 if (sizeof(c) > sizeof(char)) { \
245 __res = __body_C_macro(f,(__c)); \
247 unsigned char __c = (c); \
248 __res = __body_C_macro(f,(__c)); \
253 #define __isspace(c) __body(isspace,c)
254 #define __isblank(c) __body(isblank,c)
255 #define __isdigit(c) __body(isdigit,c)
256 #define __isxdigit(c) __body(isxdigit,c)
257 #define __iscntrl(c) __body(iscntrl,c)
258 #define __isalpha(c) __body(isalpha,c)
259 #define __isalnum(c) __body(isalnum,c)
260 #define __isprint(c) __body(isprint,c)
261 #define __islower(c) __body(islower,c)
262 #define __isupper(c) __body(isupper,c)
263 #define __ispunct(c) __body(ispunct,c)
264 #define __isgraph(c) __body(isgraph,c)
266 #define __isxlower(c) __body(isxlower,c)
267 #define __isxupper(c) __body(isxupper,c)
269 #define __tolower(c) __body(tolower,c)
270 #define __toupper(c) __body(toupper,c)
272 #if !defined __NO_CTYPE && !defined __cplusplus
274 #define isspace(c) __isspace(c)
275 #define isblank(c) __isblank(c)
276 #define isdigit(c) __isdigit(c)
277 #define isxdigit(c) __isxdigit(c)
278 #define iscntrl(c) __iscntrl(c)
279 #define isalpha(c) __isalpha(c)
280 #define isalnum(c) __isalnum(c)
281 #define isprint(c) __isprint(c)
282 #define islower(c) __islower(c)
283 #define isupper(c) __isupper(c)
284 #define ispunct(c) __ispunct(c)
285 #define isgraph(c) __isgraph(c)
287 #define isxlower(c) __isxlower(c)
288 #define isxupper(c) __isxupper(c)
290 #define tolower(c) __tolower(c)
291 #define toupper(c) __toupper(c)
296 #else /* _GNUC__ ***************************************************/
298 #if !defined __NO_CTYPE && !defined __cplusplus
300 /* These macros should be safe from side effects. */
302 #define isdigit(c) __C_isdigit(c)
303 #define isalpha(c) __C_isalpha(c)
304 #define isprint(c) __C_isprint(c)
305 #define islower(c) __C_islower(c)
306 #define isupper(c) __C_isupper(c)
307 #define isgraph(c) __C_isgraph(c)
311 #endif /* __GNUC__ */
312 /**********************************************************************/
314 #endif /* __UCLIBC_GEN_LOCALE */
316 #endif /* _BITS_CTYPE_H */