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 #warning uClibc_ctype.h is deprecated
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 /* The values for wctype_t. */
85 _CTYPE_unclassified = 0,
97 _CTYPE_isxdigit /* _MUST_ be last of the standard classes! */
101 /* The following is used to implement wctype(), but it is defined
102 * here because the ordering must agree with that of the enumeration
103 * above (ignoring unclassified). */
104 #define __CTYPE_TYPESTRING \
105 "\6alnum\0\6alpha\0\6blank\0\6cntrl\0\6digit\0\6graph\0\6lower\0" \
106 "\6print\0\6punct\0\6space\0\6upper\0\7xdigit\0\0"
108 /* Used in implementing iswctype(), but defined here as it must agree
109 * in ordering with the string above. */
110 #define __CTYPE_RANGES \
111 0, -1, /* unclassified */ \
112 1, __CTYPE_digit - 1, /* alnum */ \
113 1, __CTYPE_alpha_upper - 1, /* alpha */ \
114 __CTYPE_print_space_blank, 5, /* blank -- also must be odd! */ \
115 __CTYPE_cntrl_space_nonblank, 2, /* cntrl */ \
116 __CTYPE_digit, 0, /* digit */ \
117 1, __CTYPE_graph - 1, /* graph */ \
118 __CTYPE_alpha_lower, 1, /* lower */ \
119 1, __CTYPE_print_space_blank - 1, /* print */ \
120 __CTYPE_punct, 0, /* punct */ \
121 __CTYPE_print_space_nonblank, 5, /* space */ \
122 __CTYPE_alpha_upper_lower, 1, /* upper */ \
123 /* No entry for xdigit as it is handled specially. */
125 #define _CTYPE_iswalnum _CTYPE_isalnum
126 #define _CTYPE_iswalpha _CTYPE_isalpha
127 #define _CTYPE_iswblank _CTYPE_isblank
128 #define _CTYPE_iswcntrl _CTYPE_iscntrl
129 #define _CTYPE_iswdigit _CTYPE_isdigit
130 #define _CTYPE_iswgraph _CTYPE_isgraph
131 #define _CTYPE_iswlower _CTYPE_islower
132 #define _CTYPE_iswprint _CTYPE_isprint
133 #define _CTYPE_iswpunct _CTYPE_ispunct
134 #define _CTYPE_iswspace _CTYPE_isspace
135 #define _CTYPE_iswupper _CTYPE_isupper
136 #define _CTYPE_iswxdigit _CTYPE_isxdigit
138 /* The following is used to implement wctrans(). */
146 #define __CTYPE_TRANSTRING "\10tolower\0\10toupper\0\10totitle\0\0"
148 /* Now define some ctype macros valid for the C/POSIX locale. */
150 /* ASCII ords of \t, \f, \n, \r, and \v are 9, 12, 10, 13, 11 respectively. */
151 #define __C_isspace(c) \
152 ((sizeof(c) == sizeof(char)) \
153 ? ((((c) == ' ') || (((unsigned char)((c) - 9)) <= (13 - 9)))) \
154 : ((((c) == ' ') || (((unsigned int)((c) - 9)) <= (13 - 9)))))
155 #define __C_isblank(c) (((c) == ' ') || ((c) == '\t'))
156 #define __C_isdigit(c) \
157 ((sizeof(c) == sizeof(char)) \
158 ? (((unsigned char)((c) - '0')) < 10) \
159 : (((unsigned int)((c) - '0')) < 10))
160 #define __C_isxdigit(c) \
162 || ((sizeof(c) == sizeof(char)) \
163 ? (((unsigned char)((((c)) | 0x20) - 'a')) < 6) \
164 : (((unsigned int)((((c)) | 0x20) - 'a')) < 6)))
165 #define __C_iscntrl(c) \
166 ((sizeof(c) == sizeof(char)) \
167 ? ((((unsigned char)(c)) < 0x20) || ((c) == 0x7f)) \
168 : ((((unsigned int)(c)) < 0x20) || ((c) == 0x7f)))
169 #define __C_isalpha(c) \
170 ((sizeof(c) == sizeof(char)) \
171 ? (((unsigned char)(((c) | 0x20) - 'a')) < 26) \
172 : (((unsigned int)(((c) | 0x20) - 'a')) < 26))
173 #define __C_isalnum(c) (__C_isalpha(c) || __C_isdigit(c))
174 #define __C_isprint(c) \
175 ((sizeof(c) == sizeof(char)) \
176 ? (((unsigned char)((c) - 0x20)) <= (0x7e - 0x20)) \
177 : (((unsigned int)((c) - 0x20)) <= (0x7e - 0x20)))
178 #define __C_islower(c) \
179 ((sizeof(c) == sizeof(char)) \
180 ? (((unsigned char)((c) - 'a')) < 26) \
181 : (((unsigned int)((c) - 'a')) < 26))
182 #define __C_isupper(c) \
183 ((sizeof(c) == sizeof(char)) \
184 ? (((unsigned char)((c) - 'A')) < 26) \
185 : (((unsigned int)((c) - 'A')) < 26))
186 #define __C_ispunct(c) \
188 && ((sizeof(c) == sizeof(char)) \
189 ? (((unsigned char)((c) - 0x21)) <= (0x7e - 0x21)) \
190 : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21))))
191 #define __C_isgraph(c) \
192 ((sizeof(c) == sizeof(char)) \
193 ? (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21)) \
194 : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21)))
196 #define __C_tolower(c) (__C_isupper(c) ? ((c) | 0x20) : (c))
197 #define __C_toupper(c) (__C_islower(c) ? ((c) ^ 0x20) : (c))
199 #define __C_isxlower(c) \
201 || ((sizeof(c) == sizeof(char)) \
202 ? (((unsigned char)(((c)) - 'a')) < 6) \
203 : (((unsigned int)(((c)) - 'a')) < 6)))
204 #define __C_isxupper(c) \
206 || ((sizeof(c) == sizeof(char)) \
207 ? (((unsigned char)(((c)) - 'A')) < 6) \
208 : (((unsigned int)(((c)) - 'A')) < 6)))
210 /* TODO: Replace the above with expressions like the following? */
211 /* #define __C_isdigit(c) ((sizeof(c) == sizeof(char)) \ */
212 /* ? (((unsigned char)((c) - '0')) < 10) \ */
213 /* : (((unsigned int)((c) - '0')) < 10)) */
215 /* Similarly, define some wctype macros valid for the C/POSIX locale. */
217 /* First, we need some way to make sure the arg is in range. */
218 #define __C_classed(c) \
219 ((sizeof(c) <= sizeof(int)) || (c == ((unsigned char)c)))
221 #define __C_iswspace(c) (__C_classed(c) && __C_isspace(c))
222 #define __C_iswblank(c) (__C_classed(c) && __C_isblank(c))
223 #define __C_iswdigit(c) (__C_classed(c) && __C_isdigit(c))
224 #define __C_iswxdigit(c) (__C_classed(c) && __C_isxdigit(c))
225 #define __C_iswcntrl(c) (__C_classed(c) && __C_iscntrl(c))
226 #define __C_iswalpha(c) (__C_classed(c) && __C_isalpha(c))
227 #define __C_iswalnum(c) (__C_classed(c) && __C_isalnum(c))
228 #define __C_iswprint(c) (__C_classed(c) && __C_isprint(c))
229 #define __C_iswlower(c) (__C_classed(c) && __C_islower(c))
230 #define __C_iswupper(c) (__C_classed(c) && __C_isupper(c))
231 #define __C_iswpunct(c) (__C_classed(c) && __C_ispunct(c))
232 #define __C_iswgraph(c) (__C_classed(c) && __C_isgraph(c))
233 #define __C_towlower(c) \
234 ((__C_classed(c) && __C_isupper(c)) ? ((c) | 0x20) : (c))
235 #define __C_towupper(c) \
236 ((__C_classed(c) && __C_islower(c)) ? ((c) ^ 0x20) : (c))
238 /* Now define some macros to aviod the extra wrapper-function call. */
239 #define __iswalnum(c) iswctype(c, _CTYPE_iswalnum)
240 #define __iswalpha(c) iswctype(c, _CTYPE_iswalpha)
241 #define __iswblank(c) iswctype(c, _CTYPE_iswblank)
242 #define __iswcntrl(c) iswctype(c, _CTYPE_iswcntrl)
243 #define __iswgraph(c) iswctype(c, _CTYPE_iswgraph)
244 #define __iswlower(c) iswctype(c, _CTYPE_iswlower)
245 #define __iswprint(c) iswctype(c, _CTYPE_iswprint)
246 #define __iswpunct(c) iswctype(c, _CTYPE_iswpunct)
247 #define __iswspace(c) iswctype(c, _CTYPE_iswspace)
248 #define __iswupper(c) iswctype(c, _CTYPE_iswupper)
249 #define __iswdigit(c) __C_iswdigit(c)
250 #define __iswxdigit(c) __C_iswxdigit(c)
252 #endif /* _BITS_CTYPE_H */