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 /* Taking advantage of the C99 mutual-exclusion guarantees for the various
37 * (w)ctype classes, including the descriptions of printing and control
38 * (w)chars, we can place each in one of the following mutually-exlusive
39 * subsets. Since there are less than 16, we can store the data for
40 * each (w)chars in a nibble. In contrast, glibc uses an unsigned int
41 * per (w)char, with one bit flag for each is* type. While this allows
42 * a simple '&' operation to determine the type vs. a range test and a
43 * little special handling for the "blank" and "xdigit" types in my
44 * approach, it also uses 8 times the space for the tables on the typical
45 * 32-bit archs we supported.*/
47 __CTYPE_unclassified = 0,
48 __CTYPE_alpha_nonupper_nonlower,
50 __CTYPE_alpha_upper_lower,
55 __CTYPE_print_space_nonblank,
56 __CTYPE_print_space_blank,
57 __CTYPE_space_nonblank_noncntrl,
58 __CTYPE_space_blank_noncntrl,
59 __CTYPE_cntrl_space_nonblank,
60 __CTYPE_cntrl_space_blank,
61 __CTYPE_cntrl_nonspace
64 /* Some macros that test for various (w)ctype classes when passed one of the
65 * designator values enumerated above. */
66 #define __CTYPE_isalnum(D) ((unsigned int)(D-1) <= (__CTYPE_digit-1))
67 #define __CTYPE_isalpha(D) ((unsigned int)(D-1) <= (__CTYPE_alpha_upper-1))
68 #define __CTYPE_isblank(D) \
69 ((((unsigned int)(D - __CTYPE_print_space_nonblank)) <= 5) && (D & 1))
70 #define __CTYPE_iscntrl(D) (((unsigned int)(D - __CTYPE_cntrl_space_nonblank)) <= 2)
71 #define __CTYPE_isdigit(D) (D == __CTYPE_digit)
72 #define __CTYPE_isgraph(D) ((unsigned int)(D-1) <= (__CTYPE_graph-1))
73 #define __CTYPE_islower(D) (((unsigned int)(D - __CTYPE_alpha_lower)) <= 1)
74 #define __CTYPE_isprint(D) ((unsigned int)(D-1) <= (__CTYPE_print_space_blank-1))
75 #define __CTYPE_ispunct(D) (D == __CTYPE_punct)
76 #define __CTYPE_isspace(D) (((unsigned int)(D - __CTYPE_print_space_nonblank)) <= 5)
77 #define __CTYPE_isupper(D) (((unsigned int)(D - __CTYPE_alpha_upper_lower)) <= 1)
78 /* #define __CTYPE_isxdigit(D) -- isxdigit is untestable this way.
79 * But that's ok as isxdigit() (and isdigit() too) are locale-invariant. */
81 /* The values for wctype_t. */
83 _CTYPE_unclassified = 0,
95 _CTYPE_isxdigit /* _MUST_ be last of the standard classes! */
99 /* The following is used to implement wctype(), but it is defined
100 * here because the ordering must agree with that of the enumeration
101 * above (ignoring unclassified). */
102 #define __CTYPE_TYPESTRING \
103 "\6alnum\0\6alpha\0\6blank\0\6cntrl\0\6digit\0\6graph\0\6lower\0" \
104 "\6print\0\6punct\0\6space\0\6upper\0\7xdigit\0\0"
106 /* Used in implementing iswctype(), but defined here as it must agree
107 * in ordering with the string above. */
108 #define __CTYPE_RANGES \
109 0, -1, /* unclassified */ \
110 1, __CTYPE_digit - 1, /* alnum */ \
111 1, __CTYPE_alpha_upper - 1, /* alpha */ \
112 __CTYPE_print_space_blank, 5, /* blank -- also must be odd! */ \
113 __CTYPE_cntrl_space_nonblank, 2, /* cntrl */ \
114 __CTYPE_digit, 0, /* digit */ \
115 1, __CTYPE_graph - 1, /* graph */ \
116 __CTYPE_alpha_lower, 1, /* lower */ \
117 1, __CTYPE_print_space_blank - 1, /* print */ \
118 __CTYPE_punct, 0, /* punct */ \
119 __CTYPE_print_space_nonblank, 5, /* space */ \
120 __CTYPE_alpha_upper_lower, 1, /* upper */ \
121 /* No entry for xdigit as it is handled specially. */
123 #define _CTYPE_iswalnum _CTYPE_isalnum
124 #define _CTYPE_iswalpha _CTYPE_isalpha
125 #define _CTYPE_iswblank _CTYPE_isblank
126 #define _CTYPE_iswcntrl _CTYPE_iscntrl
127 #define _CTYPE_iswdigit _CTYPE_isdigit
128 #define _CTYPE_iswgraph _CTYPE_isgraph
129 #define _CTYPE_iswlower _CTYPE_islower
130 #define _CTYPE_iswprint _CTYPE_isprint
131 #define _CTYPE_iswpunct _CTYPE_ispunct
132 #define _CTYPE_iswspace _CTYPE_isspace
133 #define _CTYPE_iswupper _CTYPE_isupper
134 #define _CTYPE_iswxdigit _CTYPE_isxdigit
136 /* The following is used to implement wctrans(). */
144 #define __CTYPE_TRANSTRING "\10tolower\0\10toupper\0\10totitle\0\0"
146 /* Now define some ctype macros valid for the C/POSIX locale. */
148 /* ASCII ords of \t, \f, \n, \r, and \v are 9, 12, 10, 13, 11 respectively. */
149 #define __C_isspace(c) \
150 ((sizeof(c) == sizeof(char)) \
151 ? ((((c) == ' ') || (((unsigned char)((c) - 9)) <= (13 - 9)))) \
152 : ((((c) == ' ') || (((unsigned int)((c) - 9)) <= (13 - 9)))))
153 #define __C_isblank(c) (((c) == ' ') || ((c) == '\t'))
154 #define __C_isdigit(c) \
155 ((sizeof(c) == sizeof(char)) \
156 ? (((unsigned char)((c) - '0')) < 10) \
157 : (((unsigned int)((c) - '0')) < 10))
158 #define __C_isxdigit(c) \
160 || ((sizeof(c) == sizeof(char)) \
161 ? (((unsigned char)((((c)) | 0x20) - 'a')) < 6) \
162 : (((unsigned int)((((c)) | 0x20) - 'a')) < 6)))
163 #define __C_iscntrl(c) \
164 ((sizeof(c) == sizeof(char)) \
165 ? ((((unsigned char)(c)) < 0x20) || ((c) == 0x7f)) \
166 : ((((unsigned int)(c)) < 0x20) || ((c) == 0x7f)))
167 #define __C_isalpha(c) \
168 ((sizeof(c) == sizeof(char)) \
169 ? (((unsigned char)(((c) | 0x20) - 'a')) < 26) \
170 : (((unsigned int)(((c) | 0x20) - 'a')) < 26))
171 #define __C_isalnum(c) (__C_isalpha(c) || __C_isdigit(c))
172 #define __C_isprint(c) \
173 ((sizeof(c) == sizeof(char)) \
174 ? (((unsigned char)((c) - 0x20)) <= (0x7e - 0x20)) \
175 : (((unsigned int)((c) - 0x20)) <= (0x7e - 0x20)))
176 #define __C_islower(c) \
177 ((sizeof(c) == sizeof(char)) \
178 ? (((unsigned char)((c) - 'a')) < 26) \
179 : (((unsigned int)((c) - 'a')) < 26))
180 #define __C_isupper(c) \
181 ((sizeof(c) == sizeof(char)) \
182 ? (((unsigned char)((c) - 'A')) < 26) \
183 : (((unsigned int)((c) - 'A')) < 26))
184 #define __C_ispunct(c) \
186 && ((sizeof(c) == sizeof(char)) \
187 ? (((unsigned char)((c) - 0x21)) <= (0x7e - 0x21)) \
188 : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21))))
189 #define __C_isgraph(c) \
190 ((sizeof(c) == sizeof(char)) \
191 ? (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21)) \
192 : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21)))
194 #define __C_tolower(c) (__C_isupper(c) ? ((c) | 0x20) : (c))
195 #define __C_toupper(c) (__C_islower(c) ? ((c) ^ 0x20) : (c))
197 #define __C_isxlower(c) \
199 || ((sizeof(c) == sizeof(char)) \
200 ? (((unsigned char)(((c)) - 'a')) < 6) \
201 : (((unsigned int)(((c)) - 'a')) < 6)))
202 #define __C_isxupper(c) \
204 || ((sizeof(c) == sizeof(char)) \
205 ? (((unsigned char)(((c)) - 'A')) < 6) \
206 : (((unsigned int)(((c)) - 'A')) < 6)))
208 /* TODO: Replace the above with expressions like the following? */
209 /* #define __C_isdigit(c) ((sizeof(c) == sizeof(char)) \ */
210 /* ? (((unsigned char)((c) - '0')) < 10) \ */
211 /* : (((unsigned int)((c) - '0')) < 10)) */
213 /* Similarly, define some wctype macros valid for the C/POSIX locale. */
215 /* First, we need some way to make sure the arg is in range. */
216 #define __C_classed(c) \
217 ((sizeof(c) <= sizeof(int)) || (c == ((unsigned char)c)))
219 #define __C_iswspace(c) (__C_classed(c) && __C_isspace(c))
220 #define __C_iswblank(c) (__C_classed(c) && __C_isblank(c))
221 #define __C_iswdigit(c) (__C_classed(c) && __C_isdigit(c))
222 #define __C_iswxdigit(c) (__C_classed(c) && __C_isxdigit(c))
223 #define __C_iswcntrl(c) (__C_classed(c) && __C_iscntrl(c))
224 #define __C_iswalpha(c) (__C_classed(c) && __C_isalpha(c))
225 #define __C_iswalnum(c) (__C_classed(c) && __C_isalnum(c))
226 #define __C_iswprint(c) (__C_classed(c) && __C_isprint(c))
227 #define __C_iswlower(c) (__C_classed(c) && __C_islower(c))
228 #define __C_iswupper(c) (__C_classed(c) && __C_isupper(c))
229 #define __C_iswpunct(c) (__C_classed(c) && __C_ispunct(c))
230 #define __C_iswgraph(c) (__C_classed(c) && __C_isgraph(c))
231 #define __C_towlower(c) \
232 ((__C_classed(c) && __C_isupper(c)) ? ((c) | 0x20) : (c))
233 #define __C_towupper(c) \
234 ((__C_classed(c) && __C_islower(c)) ? ((c) ^ 0x20) : (c))
236 /* Now define some macros to aviod the extra wrapper-function call. */
237 #define __iswalnum(c) iswctype(c, _CTYPE_iswalnum)
238 #define __iswalpha(c) iswctype(c, _CTYPE_iswalpha)
239 #define __iswblank(c) iswctype(c, _CTYPE_iswblank)
240 #define __iswcntrl(c) iswctype(c, _CTYPE_iswcntrl)
241 #define __iswgraph(c) iswctype(c, _CTYPE_iswgraph)
242 #define __iswlower(c) iswctype(c, _CTYPE_iswlower)
243 #define __iswprint(c) iswctype(c, _CTYPE_iswprint)
244 #define __iswpunct(c) iswctype(c, _CTYPE_iswpunct)
245 #define __iswspace(c) iswctype(c, _CTYPE_iswspace)
246 #define __iswupper(c) iswctype(c, _CTYPE_iswupper)
247 #define __iswdigit(c) __C_iswdigit(c)
248 #define __iswxdigit(c) __C_iswxdigit(c)
250 #endif /* _BITS_CTYPE_H */