OSDN Git Service

New locale support (in development). Supports LC_CTYPE, LC_NUMERIC,
[uclinux-h8/uclibc-ng.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 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.
7  *
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.
12  *
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.
16  */
17
18 /*  ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION!
19  *
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.
25  *  Thanks.  Manuel
26  *
27  *  ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION! */
28
29 #if !defined(_CTYPE_H) && !defined(_WCTYPE_H)
30 #error Always include <{w}ctype.h> rather than <bits/uClibc_ctype.h>
31 #endif
32
33 #ifndef _BITS_CTYPE_H
34 #define _BITS_CTYPE_H
35
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.*/
46 enum {
47         __CTYPE_unclassified = 0,
48         __CTYPE_alpha_nonupper_nonlower,
49         __CTYPE_alpha_lower,
50         __CTYPE_alpha_upper_lower,
51         __CTYPE_alpha_upper,
52         __CTYPE_digit,
53         __CTYPE_punct,
54         __CTYPE_graph,
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,
62 };
63
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. */
80
81 /* The values for wctype_t. */
82 enum {
83         _CTYPE_unclassified = 0,
84         _CTYPE_isalnum,
85         _CTYPE_isalpha,
86         _CTYPE_isblank,
87         _CTYPE_iscntrl,
88         _CTYPE_isdigit,
89         _CTYPE_isgraph,
90         _CTYPE_islower,
91         _CTYPE_isprint,
92         _CTYPE_ispunct,
93         _CTYPE_isspace,
94         _CTYPE_isupper,
95         _CTYPE_isxdigit                         /* _MUST_ be last of the standard classes! */
96 };
97
98
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"
105
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. */
122
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
135
136 /* The following is used to implement wctrans(). */
137
138 enum {
139         _CTYPE_tolower = 1,
140         _CTYPE_toupper,
141         _CTYPE_totitle
142 };
143
144 #define __CTYPE_TRANSTRING      "\10tolower\0\10toupper\0\10totitle\0\0"
145
146 /* Now define some ctype macros valid for the C/POSIX locale. */
147
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) \
159         (__C_isdigit(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) \
185         ((!__C_isalnum(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)))
193
194 #define __C_tolower(c) (__C_isupper(c) ? ((c) | 0x20) : (c))
195 #define __C_toupper(c) (__C_islower(c) ? ((c) ^ 0x20) : (c))
196
197 #define __C_isxlower(c) \
198         (__C_isdigit(c) \
199          || ((sizeof(c) == sizeof(char)) \
200                  ? (((unsigned char)(((c)) - 'a')) < 6) \
201                  : (((unsigned int)(((c)) - 'a')) < 6)))
202 #define __C_isxupper(c) \
203         (__C_isdigit(c) \
204          || ((sizeof(c) == sizeof(char)) \
205                  ? (((unsigned char)(((c)) - 'A')) < 6) \
206                  : (((unsigned int)(((c)) - 'A')) < 6)))
207
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)) */
212
213 /* Similarly, define some wctype macros valid for the C/POSIX locale. */
214
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)))
218
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))
235
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)
249
250 #endif /* _BITS_CTYPE_H */