xref: /dragonfly/include/ctype.h (revision 9d4f17d13bf5d8bcd1e924ee709578e6f59bc958)
1 /*
2  * Copyright (c) 1989, 1993
3  *        The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * Paul Borman at Krystal Technologies.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *        @(#)ctype.h         8.4 (Berkeley) 1/21/94
38  *      $FreeBSD: head/include/ctype.h 233600 2012-03-28 12:11:54Z theraven $
39  */
40 
41 #ifndef _CTYPE_H_
42 #define _CTYPE_H_
43 
44 #include <sys/cdefs.h>
45 #include <machine/stdint.h>
46 
47 #define   _CTYPE_A  0x00000100L                   /* Alpha */
48 #define   _CTYPE_C  0x00000200L                   /* Control */
49 #define   _CTYPE_D  0x00000400L                   /* Digit */
50 #define   _CTYPE_G  0x00000800L                   /* Graph */
51 #define   _CTYPE_L  0x00001000L                   /* Lower */
52 #define   _CTYPE_P  0x00002000L                   /* Punct */
53 #define   _CTYPE_S  0x00004000L                   /* Space */
54 #define   _CTYPE_U  0x00008000L                   /* Upper */
55 #define   _CTYPE_X  0x00010000L                   /* X digit */
56 #define   _CTYPE_B  0x00020000L                   /* Blank */
57 #define   _CTYPE_R  0x00040000L                   /* Print */
58 #define   _CTYPE_I  0x00080000L                   /* Ideogram */
59 #define   _CTYPE_T  0x00100000L                   /* Special */
60 #define   _CTYPE_Q  0x00200000L                   /* Phonogram */
61 #define   _CTYPE_N  0x00400000L                   /* Number (superset of digit) */
62 #define   _CTYPE_SW0          0x20000000L                   /* 0 width character */
63 #define   _CTYPE_SW1          0x40000000L                   /* 1 width character */
64 #define   _CTYPE_SW2          0x80000000L                   /* 2 width character */
65 #define   _CTYPE_SW3          0xc0000000L                   /* 3 width character */
66 #define   _CTYPE_SWM          0xe0000000L                   /* Mask for screen width data */
67 #define   _CTYPE_SWS          30                            /* Bits to shift to get width */
68 
69 /*
70  * rune_t is declared to be an ``int'' instead of the more natural
71  * ``unsigned long'' or ``long''.  Two things are happening here.  It is not
72  * unsigned so that EOF (-1) can be naturally assigned to it and used.  Also,
73  * it looks like 10646 will be a 31 bit standard.  This means that if your
74  * ints cannot hold 32 bits, you will be in trouble.  The reason an int was
75  * chosen over a long is that the is*() and to*() routines take ints (says
76  * ANSI C), but they use __ct_rune_t instead of int.
77  *
78  * NOTE: rune_t is not covered by ANSI nor other standards, and should not
79  * be instantiated outside of lib/libc/locale.  Use wchar_t.
80  */
81 #ifndef ___CT_RUNE_T_DECLARED
82 typedef   int       __ct_rune_t;                            /* Arg type for ctype funcs */
83 #define   ___CT_RUNE_T_DECLARED
84 #endif
85 
86 __BEGIN_DECLS
87 unsigned long       ___runetype(__ct_rune_t) __pure;
88 __ct_rune_t         ___tolower(__ct_rune_t) __pure;
89 __ct_rune_t         ___toupper(__ct_rune_t) __pure;
90 __END_DECLS
91 
92 /*
93  * _EXTERNALIZE_CTYPE_INLINES_ is defined in locale/nomacros.c to tell us
94  * to generate code for extern versions of all our inline functions.
95  */
96 #ifdef _EXTERNALIZE_CTYPE_INLINES_
97 #define   _USE_CTYPE_INLINE_
98 #define   static
99 #undef    __inline
100 #define   __inline
101 #undef    __always_inline
102 #define   __always_inline
103 #endif
104 
105 extern int __mb_sb_limit;
106 
107 /*
108  * Use inline functions if we are allowed to and the compiler supports them.
109  */
110 #if !defined(_DONT_USE_CTYPE_INLINE_) && \
111     (defined(_USE_CTYPE_INLINE_) || defined(__GNUC__) || defined(__cplusplus))
112 
113 #include <runetype.h>
114 
115 static __inline __always_inline int
__maskrune(__ct_rune_t _c,unsigned long _f)116 __maskrune(__ct_rune_t _c, unsigned long _f)
117 {
118           return ((_c < 0 || _c >= _CACHED_RUNES) ? ___runetype(_c) :
119                     _CurrentRuneLocale->__runetype[_c]) & _f;
120 }
121 
122 static __inline __always_inline int
__sbmaskrune(__ct_rune_t _c,unsigned long _f)123 __sbmaskrune(__ct_rune_t _c, unsigned long _f)
124 {
125           return (_c < 0 || _c >= __mb_sb_limit) ? 0 :
126                  _CurrentRuneLocale->__runetype[_c] & _f;
127 }
128 
129 static __inline __always_inline int
__istype(__ct_rune_t _c,unsigned long _f)130 __istype(__ct_rune_t _c, unsigned long _f)
131 {
132           return (!!__maskrune(_c, _f));
133 }
134 
135 static __inline __always_inline int
__sbistype(__ct_rune_t _c,unsigned long _f)136 __sbistype(__ct_rune_t _c, unsigned long _f)
137 {
138           return (!!__sbmaskrune(_c, _f));
139 }
140 
141 static __inline __always_inline int
__isctype(__ct_rune_t _c,unsigned long _f)142 __isctype(__ct_rune_t _c, unsigned long _f)
143 {
144           return (_c < 0 || _c >= 128) ? 0 :
145                  !!(_DefaultRuneLocale.__runetype[_c] & _f);
146 }
147 
148 static __inline __always_inline __ct_rune_t
__toupper(__ct_rune_t _c)149 __toupper(__ct_rune_t _c)
150 {
151           return (_c < 0 || _c >= _CACHED_RUNES) ? ___toupper(_c) :
152                  _CurrentRuneLocale->__mapupper[_c];
153 }
154 
155 static __inline __always_inline __ct_rune_t
__sbtoupper(__ct_rune_t _c)156 __sbtoupper(__ct_rune_t _c)
157 {
158           return (_c < 0 || _c >= __mb_sb_limit) ? _c :
159                  _CurrentRuneLocale->__mapupper[_c];
160 }
161 
162 static __inline __always_inline __ct_rune_t
__tolower(__ct_rune_t _c)163 __tolower(__ct_rune_t _c)
164 {
165           return (_c < 0 || _c >= _CACHED_RUNES) ? ___tolower(_c) :
166                  _CurrentRuneLocale->__maplower[_c];
167 }
168 
169 static __inline __always_inline __ct_rune_t
__sbtolower(__ct_rune_t _c)170 __sbtolower(__ct_rune_t _c)
171 {
172           return (_c < 0 || _c >= __mb_sb_limit) ? _c :
173                  _CurrentRuneLocale->__maplower[_c];
174 }
175 
176 static __inline __always_inline int
__wcwidth(__ct_rune_t _c)177 __wcwidth(__ct_rune_t _c)
178 {
179           unsigned int _x;
180 
181           if (_c == 0)
182                     return (0);
183           _x = (unsigned int)__maskrune(_c, _CTYPE_SWM|_CTYPE_R);
184           if ((_x & _CTYPE_SWM) != 0)
185                     return ((_x & _CTYPE_SWM) >> _CTYPE_SWS);
186           return ((_x & _CTYPE_R) != 0 ? 1 : -1);
187 }
188 
189 #else /* not using inlines */
190 
191 __BEGIN_DECLS
192 int                 __maskrune(__ct_rune_t, unsigned long);
193 int                 __sbmaskrune(__ct_rune_t, unsigned long);
194 int                 __istype(__ct_rune_t, unsigned long);
195 int                 __sbistype(__ct_rune_t, unsigned long);
196 int                 __isctype(__ct_rune_t, unsigned long);
197 __ct_rune_t         __toupper(__ct_rune_t);
198 __ct_rune_t         __sbtoupper(__ct_rune_t);
199 __ct_rune_t         __tolower(__ct_rune_t);
200 __ct_rune_t         __sbtolower(__ct_rune_t);
201 int                 __wcwidth(__ct_rune_t);
202 __END_DECLS
203 #endif /* using inlines */
204 
205 
206 __BEGIN_DECLS
207 int       isalnum(int);
208 int       isalpha(int);
209 int       iscntrl(int);
210 int       isdigit(int);
211 int       isgraph(int);
212 int       islower(int);
213 int       isprint(int);
214 int       ispunct(int);
215 int       isspace(int);
216 int       isupper(int);
217 int       isxdigit(int);
218 int       tolower(int);
219 int       toupper(int);
220 
221 #if __XSI_VISIBLE
222 int       isascii(int);
223 int       toascii(int);
224 #endif
225 
226 #if __ISO_C_VISIBLE >= 1999
227 int       isblank(int);
228 #endif
229 
230 #if __BSD_VISIBLE
231 int       digittoint(int);
232 int       ishexnumber(int);
233 int       isideogram(int);
234 int       isnumber(int);
235 int       isphonogram(int);
236 int       isrune(int);
237 int       isspecial(int);
238 #endif
239 
240 #if __POSIX_VISIBLE >= 200809 || defined(_XLOCALE_H_)
241 #include <xlocale/_ctype.h>
242 #endif
243 __END_DECLS
244 
245 #ifndef __cplusplus
246 #define   isalnum(c)          __sbistype((c), _CTYPE_A|_CTYPE_D|_CTYPE_N)
247 #define   isalpha(c)          __sbistype((c), _CTYPE_A)
248 #define   iscntrl(c)          __sbistype((c), _CTYPE_C)
249 #define   isdigit(c)          __sbistype((c), _CTYPE_D)
250 #define   isgraph(c)          __sbistype((c), _CTYPE_G)
251 #define   islower(c)          __sbistype((c), _CTYPE_L)
252 #define   isprint(c)          __sbistype((c), _CTYPE_R)
253 #define   ispunct(c)          __sbistype((c), _CTYPE_P)
254 #define   isspace(c)          __sbistype((c), _CTYPE_S)
255 #define   isupper(c)          __sbistype((c), _CTYPE_U)
256 #define   isxdigit(c)         __sbistype((c), _CTYPE_X)
257 #define   tolower(c)          __sbtolower(c)
258 #define   toupper(c)          __sbtoupper(c)
259 #endif /* !__cplusplus */
260 
261 #if __XSI_VISIBLE
262 /*
263  * POSIX.1-2001 specifies _tolower() and _toupper() to be macros equivalent to
264  * tolower() and toupper() respectively, minus extra checking to ensure that
265  * the argument is a lower or uppercase letter respectively.  We've chosen to
266  * implement these macros with the same error checking as tolower() and
267  * toupper() since this doesn't violate the specification itself, only its
268  * intent.  We purposely leave _tolower() and _toupper() undocumented to
269  * discourage their use.
270  *
271  * XXX isascii() and toascii() should similarly be undocumented.
272  */
273 #define   _tolower(c)         __sbtolower(c)
274 #define   _toupper(c)         __sbtoupper(c)
275 #define   isascii(c)          (((c) & ~0x7F) == 0)
276 #define   toascii(c)          ((c) & 0x7F)
277 #endif
278 
279 #if __ISO_C_VISIBLE >= 1999 && !defined(__cplusplus)
280 #define   isblank(c)          __sbistype((c), _CTYPE_B)
281 #endif
282 
283 #if __BSD_VISIBLE
284 #define   digittoint(c)       __sbmaskrune((c), 0xFF)
285 #define   ishexnumber(c)      __sbistype((c), _CTYPE_X)
286 #define   isideogram(c)       __sbistype((c), _CTYPE_I)
287 #define   isnumber(c)         __sbistype((c), _CTYPE_D|_CTYPE_N)
288 #define   isphonogram(c)      __sbistype((c), _CTYPE_Q)
289 #define   isrune(c) __sbistype((c), 0xFFFFFF00L)
290 #define   isspecial(c)        __sbistype((c), _CTYPE_T)
291 #endif
292 
293 #endif /* !_CTYPE_H_ */
294