OSDN Git Service

- trim any trailing whitespace
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / common / bits / uClibc_ctype.h
1 /*  Copyright (C) 2002     Manuel Novoa III
2  *
3  *  This library is free software; you can redistribute it and/or
4  *  modify it under the terms of the GNU Lesser General Public
5  *  License as published by the Free Software Foundation; either
6  *  version 2.1 of the License, or (at your option) any later version.
7  *
8  *  The GNU C 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  *  Lesser General Public License for more details.
12  *
13  *  You should have received a copy of the GNU Lesser General Public
14  *  License along with the GNU C Library; if not, write to the Free
15  *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
16  *  02111-1307 USA.
17  */
18
19 /*  ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION!
20  *
21  *  Besides uClibc, I'm using this code in my libc for elks, which is
22  *  a 16-bit environment with a fairly limited compiler.  It would make
23  *  things much easier for me if this file isn't modified unnecessarily.
24  *  In particular, please put any new or replacement functions somewhere
25  *  else, and modify the makefile to use your version instead.
26  *  Thanks.  Manuel
27  *
28  *  ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION! */
29
30 #if !defined(_CTYPE_H) && !defined(_WCTYPE_H)
31 #error Always include <{w}ctype.h> rather than <bits/uClibc_ctype.h>
32 #endif
33
34 #ifndef _BITS_CTYPE_H
35 #define _BITS_CTYPE_H
36
37 #ifdef __UCLIBC_GEN_LOCALE
38
39 /* Taking advantage of the C99 mutual-exclusion guarantees for the various
40  * (w)ctype classes, including the descriptions of printing and control
41  * (w)chars, we can place each in one of the following mutually-exlusive
42  * subsets.  Since there are less than 16, we can store the data for
43  * each (w)chars in a nibble. In contrast, glibc uses an unsigned int
44  * per (w)char, with one bit flag for each is* type.  While this allows
45  * a simple '&' operation to determine the type vs. a range test and a
46  * little special handling for the "blank" and "xdigit" types in my
47  * approach, it also uses 8 times the space for the tables on the typical
48  * 32-bit archs we supported.*/
49 enum {
50         __CTYPE_unclassified = 0,
51         __CTYPE_alpha_nonupper_nonlower,
52         __CTYPE_alpha_lower,
53         __CTYPE_alpha_upper_lower,
54         __CTYPE_alpha_upper,
55         __CTYPE_digit,
56         __CTYPE_punct,
57         __CTYPE_graph,
58         __CTYPE_print_space_nonblank,
59         __CTYPE_print_space_blank,
60         __CTYPE_space_nonblank_noncntrl,
61         __CTYPE_space_blank_noncntrl,
62         __CTYPE_cntrl_space_nonblank,
63         __CTYPE_cntrl_space_blank,
64         __CTYPE_cntrl_nonspace
65 };
66
67 /* Some macros that test for various (w)ctype classes when passed one of the
68  * designator values enumerated above. */
69 #define __CTYPE_isalnum(D)              ((unsigned int)(D-1) <= (__CTYPE_digit-1))
70 #define __CTYPE_isalpha(D)              ((unsigned int)(D-1) <= (__CTYPE_alpha_upper-1))
71 #define __CTYPE_isblank(D) \
72         ((((unsigned int)(D - __CTYPE_print_space_nonblank)) <= 5) && (D & 1))
73 #define __CTYPE_iscntrl(D)              (((unsigned int)(D - __CTYPE_cntrl_space_nonblank)) <= 2)
74 #define __CTYPE_isdigit(D)              (D == __CTYPE_digit)
75 #define __CTYPE_isgraph(D)              ((unsigned int)(D-1) <= (__CTYPE_graph-1))
76 #define __CTYPE_islower(D)              (((unsigned int)(D - __CTYPE_alpha_lower)) <= 1)
77 #define __CTYPE_isprint(D)              ((unsigned int)(D-1) <= (__CTYPE_print_space_blank-1))
78 #define __CTYPE_ispunct(D)              (D == __CTYPE_punct)
79 #define __CTYPE_isspace(D)              (((unsigned int)(D - __CTYPE_print_space_nonblank)) <= 5)
80 #define __CTYPE_isupper(D)              (((unsigned int)(D - __CTYPE_alpha_upper_lower)) <= 1)
81 /*  #define __CTYPE_isxdigit(D) -- isxdigit is untestable this way.
82  *  But that's ok as isxdigit() (and isdigit() too) are locale-invariant. */
83
84 #else  /* __UCLIBC_GEN_LOCALE *****************************************/
85
86 /* Define some ctype macros valid for the C/POSIX locale. */
87
88 /* ASCII ords of \t, \f, \n, \r, and \v are 9, 12, 10, 13, 11 respectively. */
89 #define __C_isspace(c) \
90         ((sizeof(c) == sizeof(char)) \
91          ? ((((c) == ' ') || (((unsigned char)((c) - 9)) <= (13 - 9)))) \
92          : ((((c) == ' ') || (((unsigned int)((c) - 9)) <= (13 - 9)))))
93 #define __C_isblank(c) (((c) == ' ') || ((c) == '\t'))
94 #define __C_isdigit(c) \
95         ((sizeof(c) == sizeof(char)) \
96          ? (((unsigned char)((c) - '0')) < 10) \
97          : (((unsigned int)((c) - '0')) < 10))
98 #define __C_isxdigit(c) \
99         (__C_isdigit(c) \
100          || ((sizeof(c) == sizeof(char)) \
101                  ? (((unsigned char)((((c)) | 0x20) - 'a')) < 6) \
102                  : (((unsigned int)((((c)) | 0x20) - 'a')) < 6)))
103 #define __C_iscntrl(c) \
104         ((sizeof(c) == sizeof(char)) \
105          ? ((((unsigned char)(c)) < 0x20) || ((c) == 0x7f)) \
106          : ((((unsigned int)(c)) < 0x20) || ((c) == 0x7f)))
107 #define __C_isalpha(c) \
108         ((sizeof(c) == sizeof(char)) \
109          ? (((unsigned char)(((c) | 0x20) - 'a')) < 26) \
110          : (((unsigned int)(((c) | 0x20) - 'a')) < 26))
111 #define __C_isalnum(c) (__C_isalpha(c) || __C_isdigit(c))
112 #define __C_isprint(c) \
113         ((sizeof(c) == sizeof(char)) \
114          ? (((unsigned char)((c) - 0x20)) <= (0x7e - 0x20)) \
115          : (((unsigned int)((c) - 0x20)) <= (0x7e - 0x20)))
116 #define __C_islower(c) \
117         ((sizeof(c) == sizeof(char)) \
118          ? (((unsigned char)((c) - 'a')) < 26) \
119          : (((unsigned int)((c) - 'a')) < 26))
120 #define __C_isupper(c) \
121         ((sizeof(c) == sizeof(char)) \
122          ? (((unsigned char)((c) - 'A')) < 26) \
123          : (((unsigned int)((c) - 'A')) < 26))
124 #define __C_ispunct(c) \
125         ((!__C_isalnum(c)) \
126          && ((sizeof(c) == sizeof(char)) \
127                  ? (((unsigned char)((c) - 0x21)) <= (0x7e - 0x21)) \
128                  : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21))))
129 #define __C_isgraph(c) \
130         ((sizeof(c) == sizeof(char)) \
131          ? (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21)) \
132          : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21)))
133
134 #define __C_tolower(c) (__C_isupper(c) ? ((c) | 0x20) : (c))
135 #define __C_toupper(c) (__C_islower(c) ? ((c) ^ 0x20) : (c))
136
137 /**********************************************************************/
138 __BEGIN_DECLS
139
140 extern int isalnum(int c) __THROW;
141 extern int isalpha(int c) __THROW;
142 #ifdef __USE_ISOC99
143 extern int isblank(int c) __THROW;
144 #endif
145 extern int iscntrl(int c) __THROW;
146 extern int isdigit(int c) __THROW;
147 extern int isgraph(int c) __THROW;
148 extern int islower(int c) __THROW;
149 extern int isprint(int c) __THROW;
150 extern int ispunct(int c) __THROW;
151 extern int isspace(int c) __THROW;
152 extern int isupper(int c) __THROW;
153 extern int isxdigit(int c) __THROW;
154
155 extern int tolower(int c) __THROW;
156 extern int toupper(int c) __THROW;
157
158 #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
159 extern int isascii(int c) __THROW;
160 extern int toascii(int c) __THROW;
161 #endif
162
163 #if defined _LIBC && (defined NOT_IN_libc || defined IS_IN_libc)
164 /* isdigit() is really locale-invariant, so provide some small fast macros.
165  * These are uClibc-specific. */
166 #define __isdigit_char(C)    (((unsigned char)((C) - '0')) <= 9)
167 #define __isdigit_int(C)     (((unsigned int)((C) - '0')) <= 9)
168 #endif
169
170 /* Next, some ctype macros which are valid for all supported locales. */
171 /* WARNING: isspace and isblank need to be reverified if more 8-bit codesets
172  * are added!!!  But isdigit and isxdigit are always valid. */
173
174 /* #define __isspace(c) __C_isspace(c) */
175 /* #define __isblank(c) __C_isblank(c) */
176
177 /* #define __isdigit(c) __C_isdigit(c) */
178 /* #define __isxdigit(c)        __C_isxdigit(c) */
179
180 /* Now some non-ansi/iso c99 macros. */
181
182 #define __isascii(c) (((c) & ~0x7f) == 0)
183 #define __toascii(c) ((c) & 0x7f)
184 #define _toupper(c) ((c) ^ 0x20)
185 #define _tolower(c) ((c) | 0x20)
186
187 __END_DECLS
188
189 /**********************************************************************/
190 #ifdef __GNUC__
191
192 #define __isbody_C_macro(f,args)  __C_ ## f args
193
194 #define __isbody(f,c) \
195         (__extension__ ({ \
196                 int __res; \
197                 if (sizeof(c) > sizeof(char)) { \
198                         int __c = (c); \
199                         __res = __isbody_C_macro(f,(__c)); \
200                 } else { \
201                         unsigned char __c = (c); \
202                         __res = __isbody_C_macro(f,(__c)); \
203                 } \
204                 __res; \
205         }))
206
207 #define __body_C_macro(f,args)  __C_ ## f args
208
209 #define __body(f,c) \
210         (__extension__ ({ \
211                 int __res; \
212                 if (sizeof(c) > sizeof(char)) { \
213                         int __c = (c); \
214                         __res = __body_C_macro(f,(__c)); \
215                 } else { \
216                         unsigned char __c = (c); \
217                         __res = __body_C_macro(f,(__c)); \
218                 } \
219                 __res; \
220         }))
221
222 #define __isspace(c)            __body(isspace,c)
223 #define __isblank(c)            __body(isblank,c)
224 #define __isdigit(c)            __body(isdigit,c)
225 #define __isxdigit(c)           __body(isxdigit,c)
226 #define __iscntrl(c)            __body(iscntrl,c)
227 #define __isalpha(c)            __body(isalpha,c)
228 #define __isalnum(c)            __body(isalnum,c)
229 #define __isprint(c)            __body(isprint,c)
230 #define __islower(c)            __body(islower,c)
231 #define __isupper(c)            __body(isupper,c)
232 #define __ispunct(c)            __body(ispunct,c)
233 #define __isgraph(c)            __body(isgraph,c)
234
235 #define __tolower(c)            __body(tolower,c)
236 #define __toupper(c)            __body(toupper,c)
237
238 #if !defined __NO_CTYPE && !defined __cplusplus
239
240 #define isspace(c)                      __isspace(c)
241 #define isblank(c)                      __isblank(c)
242 #define isdigit(c)                      __isdigit(c)
243 #define isxdigit(c)                     __isxdigit(c)
244 #define iscntrl(c)                      __iscntrl(c)
245 #define isalpha(c)                      __isalpha(c)
246 #define isalnum(c)                      __isalnum(c)
247 #define isprint(c)                      __isprint(c)
248 #define islower(c)                      __islower(c)
249 #define isupper(c)                      __isupper(c)
250 #define ispunct(c)                      __ispunct(c)
251 #define isgraph(c)                      __isgraph(c)
252
253 #define tolower(c)                      __tolower(c)
254 #define toupper(c)                      __toupper(c)
255
256
257 #endif
258
259 #else  /* _GNUC__   ***************************************************/
260
261 #if !defined __NO_CTYPE && !defined __cplusplus
262
263 /* These macros should be safe from side effects. */
264
265 #define isdigit(c)                      __C_isdigit(c)
266 #define isalpha(c)                      __C_isalpha(c)
267 #define isprint(c)                      __C_isprint(c)
268 #define islower(c)                      __C_islower(c)
269 #define isupper(c)                      __C_isupper(c)
270 #define isgraph(c)                      __C_isgraph(c)
271
272 #endif
273
274 #endif /* __GNUC__ */
275 /**********************************************************************/
276
277 #endif  /* __UCLIBC_GEN_LOCALE */
278
279 #endif /* _BITS_CTYPE_H */