OSDN Git Service

ctype: remove some trivial macros from ctype.h;
[uclinux-h8/uClibc.git] / include / ctype.h
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.
4
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.
9
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.
14
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
18    02111-1307 USA.  */
19
20 /*
21  *      ISO C99 Standard 7.4: Character handling        <ctype.h>
22  */
23
24 #ifndef _CTYPE_H
25 #define _CTYPE_H        1
26
27 #include <features.h>
28 #include <bits/types.h>
29
30 #ifndef __UCLIBC_HAS_CTYPE_TABLES__
31
32 /* "Stub locale": we are permanently in C/POSIX locale.
33  * Using simple(r) ctype.h machinery in this header instead: */
34 #include <bits/uClibc_ctype.h>
35
36 #else
37
38 __BEGIN_DECLS
39
40 /* These are all the characteristics of characters.
41    If there get to be more than 16 distinct characteristics,
42    __ctype_mask_t will need to be adjusted. */
43
44 enum
45 {
46   _ISupper  = 1 << 0,   /* UPPERCASE.  */
47   _ISlower  = 1 << 1,   /* lowercase.  */
48   _ISalpha  = 1 << 2,   /* Alphabetic.  */
49   _ISdigit  = 1 << 3,   /* Numeric.  */
50   _ISxdigit = 1 << 4,   /* Hexadecimal numeric.  */
51   _ISspace  = 1 << 5,   /* Whitespace.  */
52   _ISprint  = 1 << 6,   /* Printing.  */
53   _ISgraph  = 1 << 7,   /* Graphical.  */
54   _ISblank  = 1 << 8,   /* Blank (usually SPC and TAB).  */
55   _IScntrl  = 1 << 9,   /* Control character.  */
56   _ISpunct  = 1 << 10,  /* Punctuation.  */
57   _ISalnum  = 1 << 11,  /* Alphanumeric.  */
58 };
59
60 /* __ctype_XXX_t types and __UCLIBC_CTYPE_x_TBL_OFFSET constants */
61 #include <bits/uClibc_touplow.h>
62
63 #ifdef __UCLIBC_HAS_CTYPE_SIGNED__
64 # define __UCLIBC_CTYPE_IN_TO_DOMAIN(c) (((unsigned int)((c) + 128)) < 384)
65 #else
66 # define __UCLIBC_CTYPE_IN_TO_DOMAIN(c) (((unsigned int)(c)) < 256)
67 #endif
68
69 /* In the thread-specific locale model (see `uselocale' in <locale.h>)
70    we cannot use global variables for these as was done in the past.
71    Instead, the following accessor functions return the address of
72    each variable, which is local to the current thread if multithreaded.
73
74    These point into arrays of 384, so they can be indexed by any `unsigned
75    char' value [0,255]; by EOF (-1); or by any `signed char' value
76    [-128,-1).  ISO C requires that the ctype functions work for `unsigned
77    char' values and for EOF; we also support negative `signed char' values
78    for broken old programs.  The case conversion arrays are of `int's
79    rather than `unsigned char's because tolower (EOF) must be EOF, which
80    doesn't fit into an `unsigned char'.  But today more important is that
81    the arrays are also used for multi-byte character sets.  */
82
83 /* uClibc differences:
84  *
85  * When __UCLIBC_HAS_CTYPE_SIGNED is defined,
86  *
87  *    The upper and lower mapping arrays are type int16_t, so that
88  *    they may store all char values plus EOF.  The glibc reasoning
89  *    given above for these being type int is questionable, as the
90  *    ctype mapping functions map from the set of (unsigned) char
91  *    and EOF back into the set.  They have no awareness of multi-byte
92  *    or wide characters.
93  *
94  * Otherwise,
95  *
96  *    The ctype array is defined for -1..255.
97  *    The upper and lower mapping arrays are defined for 0..255.
98  *    The upper and lower mapping arrays are type unsigned char.
99  */
100
101 /* Pointers to the default C-locale data. */
102 extern const __ctype_mask_t *__C_ctype_b;
103 libc_hidden_proto(__C_ctype_b)
104 extern const __ctype_touplow_t *__C_ctype_toupper;
105 libc_hidden_proto(__C_ctype_toupper)
106 extern const __ctype_touplow_t *__C_ctype_tolower;
107 libc_hidden_proto(__C_ctype_tolower)
108
109 #ifdef __UCLIBC_HAS_XLOCALE__
110
111 const __ctype_mask_t **__ctype_b_loc(void) __attribute__ ((const));
112 libc_hidden_proto(__ctype_b_loc)
113 const __ctype_touplow_t **__ctype_tolower_loc(void) __attribute__ ((const));
114 libc_hidden_proto(__ctype_tolower_loc)
115 const __ctype_touplow_t **__ctype_toupper_loc(void) __attribute__ ((const));
116 libc_hidden_proto(__ctype_toupper_loc)
117 #define __UCLIBC_CTYPE_B        (*__ctype_b_loc())
118 #define __UCLIBC_CTYPE_TOLOWER  (*__ctype_tolower_loc())
119 #define __UCLIBC_CTYPE_TOUPPER  (*__ctype_toupper_loc())
120
121 #else  /* __UCLIBC_HAS_XLOCALE__ */
122
123 /* Pointers to the current global locale data in use. */
124 extern const __ctype_mask_t *__ctype_b;
125 libc_hidden_proto(__ctype_b)
126 extern const __ctype_touplow_t *__ctype_toupper;
127 libc_hidden_proto(__ctype_toupper)
128 extern const __ctype_touplow_t *__ctype_tolower;
129 libc_hidden_proto(__ctype_tolower)
130 #define __UCLIBC_CTYPE_B        (__ctype_b)
131 #define __UCLIBC_CTYPE_TOLOWER  (__ctype_tolower)
132 #define __UCLIBC_CTYPE_TOUPPER  (__ctype_toupper)
133
134 #endif /* __UCLIBC_HAS_XLOCALE__ */
135
136
137 #define __isascii(c)    (((c) & ~0x7f) == 0)    /* If C is a 7 bit value.  */
138 #define __toascii(c)    ((c) & 0x7f)            /* Mask off high bits.  */
139
140 #if defined _LIBC && (defined IS_IN_libc || defined NOT_IN_libc)
141 /* These are uClibc-specific. */
142 #define __isdigit_char(C)    (((unsigned char)((C) - '0')) <= 9)
143 #define __isdigit_int(C)     (((unsigned int)((C) - '0')) <= 9)
144 #endif
145
146 __BEGIN_NAMESPACE_STD
147
148 /* The following names are all functions:
149      int isCHARACTERISTIC(int c);
150    which return nonzero iff C has CHARACTERISTIC.
151    For the meaning of the characteristic names, see the `enum' above.  */
152 extern int isalnum(int __c) __THROW;
153 libc_hidden_proto(isalnum)
154 extern int isalpha(int __c) __THROW;
155 libc_hidden_proto(isalpha)
156 extern int iscntrl(int __c) __THROW;
157 libc_hidden_proto(iscntrl)
158 extern int isdigit(int __c) __THROW;
159 libc_hidden_proto(isdigit)
160 extern int islower(int __c) __THROW;
161 libc_hidden_proto(islower)
162 extern int isgraph(int __c) __THROW;
163 libc_hidden_proto(isgraph)
164 extern int isprint(int __c) __THROW;
165 libc_hidden_proto(isprint)
166 extern int ispunct(int __c) __THROW;
167 libc_hidden_proto(ispunct)
168 extern int isspace(int __c) __THROW;
169 libc_hidden_proto(isspace)
170 extern int isupper(int __c) __THROW;
171 libc_hidden_proto(isupper)
172 extern int isxdigit(int __c) __THROW;
173 libc_hidden_proto(isxdigit)
174
175
176 /* Return the lowercase version of C.  */
177 extern int tolower(int __c) __THROW;
178 libc_hidden_proto(tolower)
179
180 /* Return the uppercase version of C.  */
181 extern int toupper(int __c) __THROW;
182 libc_hidden_proto(toupper)
183
184 __END_NAMESPACE_STD
185
186
187 /* ISO C99 introduced one new function.  */
188 #ifdef  __USE_ISOC99
189 __BEGIN_NAMESPACE_C99
190
191 extern int isblank(int __c) __THROW;
192 libc_hidden_proto(isblank)
193
194 __END_NAMESPACE_C99
195 #endif
196
197 #ifdef __USE_GNU
198 /* Test C for a set of character classes according to MASK.  */
199 extern int isctype(int __c, int __mask) __THROW;
200 #endif
201
202 #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
203 /* Return nonzero iff C is in the ASCII set
204    (i.e., is no more than 7 bits wide).  */
205 extern int isascii(int __c) __THROW;
206 libc_hidden_proto(isascii)
207 /* Return the part of C that is in the ASCII set
208    (i.e., the low-order 7 bits of C).  */
209 extern int toascii(int __c) __THROW;
210 /* These are the same as `toupper' and `tolower' except that they do not
211    check the argument for being in the range of a `char'.  */
212 extern int _toupper(int __c) __THROW;
213 extern int _tolower(int __c) __THROW;
214 #endif /* Use SVID or use misc.  */
215
216 /* This code is needed for the optimized mapping functions.  */
217 #define __tobody(c, f, table, args) \
218 (__extension__ ({ \
219         int __res; \
220         if (sizeof(c) > 1) { \
221                 if (__builtin_constant_p(c)) { \
222                         int __c = (c); \
223                         __res = __UCLIBC_CTYPE_IN_TO_DOMAIN(__c) ? (table)[__c] : __c; \
224                 } else \
225                         __res = f args; \
226         } else \
227                 __res = (table)[(int) (c)]; \
228         __res; \
229 }))
230
231 #define __isctype(c, type) ((__UCLIBC_CTYPE_B)[(int)(c)] & (__ctype_mask_t)type)
232 #if !defined __NO_CTYPE && !defined __cplusplus
233 # define isalnum(c)     __isctype((c), _ISalnum)
234 # define isalpha(c)     __isctype((c), _ISalpha)
235 # define iscntrl(c)     __isctype((c), _IScntrl)
236 # define isdigit(c)     __isctype((c), _ISdigit)
237 # define islower(c)     __isctype((c), _ISlower)
238 # define isgraph(c)     __isctype((c), _ISgraph)
239 # define isprint(c)     __isctype((c), _ISprint)
240 # define ispunct(c)     __isctype((c), _ISpunct)
241 # define isspace(c)     __isctype((c), _ISspace)
242 # define isupper(c)     __isctype((c), _ISupper)
243 # define isxdigit(c)    __isctype((c), _ISxdigit)
244 # ifdef __USE_ISOC99
245 #  define isblank(c)    __isctype((c), _ISblank)
246 # endif
247
248 # ifdef __USE_EXTERN_INLINES
249 __extern_inline int
250 __NTH (tolower (int __c))
251 {
252   return __UCLIBC_CTYPE_IN_TO_DOMAIN(__c) ? (__UCLIBC_CTYPE_TOLOWER)[__c] : __c;
253 }
254 __extern_inline int
255 __NTH (toupper (int __c))
256 {
257   return __UCLIBC_CTYPE_IN_TO_DOMAIN(__c) ? (__UCLIBC_CTYPE_TOUPPER)[__c] : __c;
258 }
259 # endif
260
261 # if __GNUC__ >= 2 && defined __OPTIMIZE__ && !defined __cplusplus
262 #  define tolower(c)    __tobody(c, tolower, __UCLIBC_CTYPE_TOLOWER, (c))
263 #  define toupper(c)    __tobody(c, toupper, __UCLIBC_CTYPE_TOUPPER, (c))
264 # endif /* Optimizing gcc */
265
266 # if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
267 #  define isascii(c)    __isascii (c)
268 #  define toascii(c)    __toascii (c)
269 #  define _tolower(c)   ((int) (__UCLIBC_CTYPE_TOLOWER)[(int) (c)])
270 #  define _toupper(c)   ((int) (__UCLIBC_CTYPE_TOUPPER)[(int) (c)])
271 # endif
272
273 #endif /* Not __NO_CTYPE.  */
274
275
276 #if defined __USE_GNU && defined __UCLIBC_HAS_XLOCALE__
277 /* The concept of one static locale per category is not very well
278    thought out.  Many applications will need to process its data using
279    information from several different locales.  Another application is
280    the implementation of the internationalization handling in the
281    upcoming ISO C++ standard library.  To support this another set of
282    the functions using locale data exist which have an additional
283    argument.
284
285    Attention: all these functions are *not* standardized in any form.
286    This is a proof-of-concept implementation.  */
287
288 /* Structure for reentrant locale using functions.  This is an
289    (almost) opaque type for the user level programs.  */
290 # include <xlocale.h>
291
292 /* These definitions are similar to the ones above but all functions
293    take as an argument a handle for the locale which shall be used.  */
294 extern int isalnum_l(int, __locale_t) __THROW;
295 libc_hidden_proto(isalnum_l)
296 extern int isalpha_l(int, __locale_t) __THROW;
297 libc_hidden_proto(isalpha_l)
298 extern int iscntrl_l(int, __locale_t) __THROW;
299 libc_hidden_proto(iscntrl_l)
300 extern int isdigit_l(int, __locale_t) __THROW;
301 libc_hidden_proto(isdigit_l)
302 extern int islower_l(int, __locale_t) __THROW;
303 libc_hidden_proto(islower_l)
304 extern int isgraph_l(int, __locale_t) __THROW;
305 libc_hidden_proto(isgraph_l)
306 extern int isprint_l(int, __locale_t) __THROW;
307 libc_hidden_proto(isprint_l)
308 extern int ispunct_l(int, __locale_t) __THROW;
309 libc_hidden_proto(ispunct_l)
310 extern int isspace_l(int, __locale_t) __THROW;
311 libc_hidden_proto(isspace_l)
312 extern int isupper_l(int, __locale_t) __THROW;
313 libc_hidden_proto(isupper_l)
314 extern int isxdigit_l(int, __locale_t) __THROW;
315 libc_hidden_proto(isxdigit_l)
316 extern int isblank_l(int, __locale_t) __THROW;
317 libc_hidden_proto(isblank_l)
318
319 # if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
320
321 /* Return nonzero iff C is in the ASCII set
322    (i.e., is no more than 7 bits wide).  */
323 extern int isascii_l (int __c) __THROW;
324 libc_hidden_proto(isascii_l)
325
326 # endif
327
328 /* Return the lowercase version of C in locale L.  */
329 // "ordinary" ctype.h has no __tolower, why we try to have it?
330 // remove after 0.9.31
331 //extern int __tolower_l (int __c, __locale_t __l) __THROW;
332 extern int tolower_l (int __c, __locale_t __l) __THROW;
333 libc_hidden_proto(tolower_l)
334
335 /* Return the uppercase version of C.  */
336 //extern int __toupper_l (int __c, __locale_t __l) __THROW;
337 extern int toupper_l (int __c, __locale_t __l) __THROW;
338 libc_hidden_proto(toupper_l)
339
340 # if __GNUC__ >= 2 && defined __OPTIMIZE__ && !defined __cplusplus
341 #  define tolower_l(c, locale) __tobody(c, tolower_l, (locale)->__ctype_tolower, (c, locale))
342 #  define toupper_l(c, locale) __tobody(c, toupper_l, (locale)->__ctype_toupper, (c, locale))
343 //#  define __tolower_l(c, locale) tolower_l((c), (locale))
344 //#  define __toupper_l(c, locale) toupper_l((c), (locale))
345 # endif /* Optimizing gcc */
346
347
348 # define __isctype_l(c, type, locale) ((locale)->__ctype_b[(int) (c)] & (__ctype_mask_t) type)
349 # ifndef __NO_CTYPE
350 #  define __isalnum_l(c,l)      __isctype_l((c), _ISalnum, (l))
351 #  define __isalpha_l(c,l)      __isctype_l((c), _ISalpha, (l))
352 #  define __iscntrl_l(c,l)      __isctype_l((c), _IScntrl, (l))
353 #  define __isdigit_l(c,l)      __isctype_l((c), _ISdigit, (l))
354 #  define __islower_l(c,l)      __isctype_l((c), _ISlower, (l))
355 #  define __isgraph_l(c,l)      __isctype_l((c), _ISgraph, (l))
356 #  define __isprint_l(c,l)      __isctype_l((c), _ISprint, (l))
357 #  define __ispunct_l(c,l)      __isctype_l((c), _ISpunct, (l))
358 #  define __isspace_l(c,l)      __isctype_l((c), _ISspace, (l))
359 #  define __isupper_l(c,l)      __isctype_l((c), _ISupper, (l))
360 #  define __isxdigit_l(c,l)     __isctype_l((c), _ISxdigit, (l))
361 #  define __isblank_l(c,l)      __isctype_l((c), _ISblank, (l))
362
363 #  if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
364 #   define __isascii_l(c,l)     ((l), __isascii (c))
365 #   define __toascii_l(c,l)     ((l), __toascii (c))
366 #  endif
367
368 #  define isalnum_l(c,l)        __isalnum_l ((c), (l))
369 #  define isalpha_l(c,l)        __isalpha_l ((c), (l))
370 #  define iscntrl_l(c,l)        __iscntrl_l ((c), (l))
371 #  define isdigit_l(c,l)        __isdigit_l ((c), (l))
372 #  define islower_l(c,l)        __islower_l ((c), (l))
373 #  define isgraph_l(c,l)        __isgraph_l ((c), (l))
374 #  define isprint_l(c,l)        __isprint_l ((c), (l))
375 #  define ispunct_l(c,l)        __ispunct_l ((c), (l))
376 #  define isspace_l(c,l)        __isspace_l ((c), (l))
377 #  define isupper_l(c,l)        __isupper_l ((c), (l))
378 #  define isxdigit_l(c,l)       __isxdigit_l ((c), (l))
379 #  define isblank_l(c,l)        __isblank_l ((c), (l))
380
381 #  if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
382 #   define isascii_l(c,l)       __isascii_l ((c), (l))
383 #   define toascii_l(c,l)       __toascii_l ((c), (l))
384 #  endif
385
386 # endif /* Not __NO_CTYPE.  */
387
388 #endif /* Use GNU.  */
389
390 __END_DECLS
391
392 #endif /* __UCLIBC_HAS_CTYPE_TABLES__ */
393
394 #endif /* ctype.h  */