OSDN Git Service

Part of the ctype locale support.
[uclinux-h8/uClibc.git] / libc / misc / ctype / ctype.c
1 /* ctype.c
2  * Character classification and conversion
3  * Copyright (C) 2000 Lineo, Inc.
4  * Written by Erik Andersen
5  * This file is part of the uClibc C library and is distributed
6  * under the GNU Library General Public License.
7  *
8  * not C-locale only code
9  * written by Vladimir Oleynik (c) vodz@usa.net
10  * and Manuel Novoa III <mnovoa3@bellsouth.net>
11  * used ideas is part of the GNU C Library.
12  */
13
14 #include <ctype.h>
15
16 #ifdef L_isascii
17 int
18 isascii( int c )
19 {
20     return (c > 0 && c <= 0x7f);
21 }
22 #endif
23
24 #ifdef L_isdigit
25 int
26 isdigit( int c )
27 {
28     return (c >= '0' && c <= '9');
29 }
30 #endif
31
32 #ifdef L_toascii
33 int
34 toascii( int c )
35 {
36     return (c & 0x7f);
37 }
38 #endif
39
40
41 /* locale depended */
42 #if !__UCLIBC_HAS_LOCALE__
43
44 #ifdef L_isalpha
45 int
46 isalpha( int c )
47 {
48     return (isupper(c) || islower(c));
49 }
50 #endif
51
52 #ifdef L_isalnum
53 int
54 isalnum( int c )
55 {
56     return (isalpha(c) || isdigit(c));
57 }
58 #endif
59
60 #ifdef L_iscntrl
61 int
62 iscntrl( int c )
63 {
64     return ((c >= 0) && ((c <= 0x1f) || (c == 0x7f)));
65 }
66 #endif
67
68 #ifdef L_isgraph
69 int
70 isgraph( int c )
71 {
72     return (c > ' ' && isprint(c));
73 }
74 #endif
75
76 #ifdef L_islower
77 int
78 islower( int c )
79 {
80     return (c >=  'a' && c <= 'z');
81 }
82 #endif
83
84 #ifdef L_isprint
85 int
86 isprint( int c )
87 {
88     return (c >= ' ' && c <= '~');
89 }
90 #endif
91
92 #ifdef L_ispunct
93 int
94 ispunct( int c )
95 {
96     return ((c > ' ' && c <= '~') && !isalnum(c));
97 }
98 #endif
99
100 #ifdef L_isspace
101 int
102 isspace( int c )
103 {
104     return (c == ' ' || c == '\f' || c == '\n' || c == '\r' ||
105             c == '\t' || c == '\v');
106 }
107 #endif
108
109 #ifdef L_isupper
110 int
111 isupper( int c )
112 {
113     return (c >=  'A' && c <= 'Z');
114 }
115 #endif
116
117 #ifdef L_isxdigit
118 int
119 isxdigit( int c )
120 {
121     return (isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
122 }
123 #endif
124
125 #ifdef L_isxlower
126 int
127 isxlower( int c )
128 {
129     return (isdigit(c) || (c >= 'a' && c <= 'f'));
130 }
131 #endif
132
133 #ifdef L_isxupper
134 int
135 isxupper( int c )
136 {
137     return (isdigit(c) || (c >= 'A' && c <= 'F'));
138 }
139 #endif
140
141 #ifdef L_tolower
142 int
143 tolower( int c )
144 {
145     return (isupper(c) ? (c - 'A' + 'a') : (c));
146 }
147 #endif
148
149 #ifdef L_toupper
150 int
151 toupper( int c )
152 {
153     return (islower(c) ? (c - 'a' + 'A') : (c));
154 }
155 #endif
156
157 #else   /* __UCLIBC_HAS_LOCALE__ == 1 */
158
159 #include <limits.h>
160 #include "./ctype.h"
161
162 #define _UC_ISCTYPE(c, type) \
163 ((c != -1) && ((_uc_ctype_b[(int)((unsigned char)c)] & type) != 0))
164
165 #define _UC_ISCTYPE2(c, type, type2) \
166 ((c != -1) && ((_uc_ctype_b[(int)((unsigned char)c)] & type) == type2))
167
168
169 #ifdef L_ctype_C
170
171 /* startup setlocale(LC_TYPE, "C"); */
172 #include "ctype_C.c"
173
174 const unsigned char *_uc_ctype_b     = _uc_ctype_b_C;
175 const unsigned char *_uc_ctype_trans = _uc_ctype_b_C+LOCALE_BUF_SIZE/2;
176
177 #endif  /* L_ctype_C */
178
179 #ifdef L_isalpha
180 int
181 isalpha( int c )
182 {
183     return _UC_ISCTYPE(c, ISalpha);
184 }
185 #endif
186
187 #ifdef L_isalnum
188 int
189 isalnum( int c )
190 {
191     return _UC_ISCTYPE(c, (ISalpha|ISxdigit));
192 }
193 #endif
194
195 #ifdef L_iscntrl
196 int
197 iscntrl( int c )
198 {
199     return _UC_ISCTYPE(c, IScntrl);
200 }
201 #endif
202
203 #ifdef L_isgraph
204 int
205 isgraph( int c )
206 {
207     return _UC_ISCTYPE2(c, (ISprint|ISspace), ISprint);
208 }
209 #endif
210
211 #ifdef L_islower
212 int
213 islower( int c )
214 {
215     return _UC_ISCTYPE(c, ISlower);
216 }
217 #endif
218
219 #ifdef L_isprint
220 int
221 isprint( int c )
222 {
223     return _UC_ISCTYPE(c, ISprint);
224 }
225 #endif
226
227 #ifdef L_ispunct
228 int
229 ispunct( int c )
230 {
231     return _UC_ISCTYPE(c, ISpunct);
232 }
233 #endif
234
235 #ifdef L_isspace
236 int
237 isspace( int c )
238 {
239     return _UC_ISCTYPE(c, ISspace);
240 }
241 #endif
242
243 #ifdef L_isupper
244 int
245 isupper( int c )
246 {
247     return _UC_ISCTYPE(c, ISupper);
248 }
249 #endif
250
251 #ifdef L_isxdigit
252 int
253 isxdigit( int c )
254 {
255     return _UC_ISCTYPE(c, ISxdigit);
256 }
257 #endif
258
259 #ifdef L_isxlower
260 int
261 isxlower( int c )
262 {
263     return _UC_ISCTYPE2(c, (ISxdigit|ISupper), ISxdigit);
264 }
265 #endif
266
267 #ifdef L_isxupper
268 int
269 isxupper( int c )
270 {
271     return _UC_ISCTYPE2(c, (ISxdigit|ISlower), ISxdigit);
272 }
273 #endif
274
275 #ifdef L_tolower
276 int
277 tolower( int c )
278 {
279     if((c < CHAR_MIN) || (c > UCHAR_MAX))
280                 return c;
281     if(isupper(c))
282                 return _uc_ctype_trans[(int)((unsigned char)c)];
283     else
284                 return c;
285 }
286 #endif
287
288 #ifdef L_toupper
289 int
290 toupper( int c )
291 {
292     if((c < CHAR_MIN) || (c > UCHAR_MAX))
293                 return c;
294     if(islower(c))
295                 return _uc_ctype_trans[(int)((unsigned char)c)];
296     else
297                 return c;
298 }
299 #endif
300
301 #endif