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