]> git.saurik.com Git - apple/libc.git/blob - include/ctype.h
d7f85cf34202e34ba1e629e4dedd4a9597ac38ac
[apple/libc.git] / include / ctype.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1989, 1993
27 * The Regents of the University of California. All rights reserved.
28 * (c) UNIX System Laboratories, Inc.
29 * All or some portions of this file are derived from material licensed
30 * to the University of California by American Telephone and Telegraph
31 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
32 * the permission of UNIX System Laboratories, Inc.
33 *
34 * This code is derived from software contributed to Berkeley by
35 * Paul Borman at Krystal Technologies.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)ctype.h 8.4 (Berkeley) 1/21/94
66 */
67
68 #ifndef _CTYPE_H_
69 #define _CTYPE_H_
70
71 #include <runetype.h>
72
73 #define _CTYPE_A 0x00000100L /* Alpha */
74 #define _CTYPE_C 0x00000200L /* Control */
75 #define _CTYPE_D 0x00000400L /* Digit */
76 #define _CTYPE_G 0x00000800L /* Graph */
77 #define _CTYPE_L 0x00001000L /* Lower */
78 #define _CTYPE_P 0x00002000L /* Punct */
79 #define _CTYPE_S 0x00004000L /* Space */
80 #define _CTYPE_U 0x00008000L /* Upper */
81 #define _CTYPE_X 0x00010000L /* X digit */
82 #define _CTYPE_B 0x00020000L /* Blank */
83 #define _CTYPE_R 0x00040000L /* Print */
84 #define _CTYPE_I 0x00080000L /* Ideogram */
85 #define _CTYPE_T 0x00100000L /* Special */
86 #define _CTYPE_Q 0x00200000L /* Phonogram */
87 #define _CTYPE_SW0 0x20000000L /* 0 width character */
88 #define _CTYPE_SW1 0x40000000L /* 1 width character */
89 #define _CTYPE_SW2 0x80000000L /* 2 width character */
90 #define _CTYPE_SW3 0xc0000000L /* 3 width character */
91
92 /*
93 * Backward compatibility
94 */
95 #define _A _CTYPE_A /* Alpha */
96 #define _C _CTYPE_C /* Control */
97 #define _D _CTYPE_D /* Digit */
98 #define _G _CTYPE_G /* Graph */
99 #define _L _CTYPE_L /* Lower */
100 #define _P _CTYPE_P /* Punct */
101 #define _S _CTYPE_S /* Space */
102 #define _U _CTYPE_U /* Upper */
103 #define _X _CTYPE_X /* X digit */
104 #define _B _CTYPE_B /* Blank */
105 #define _R _CTYPE_R /* Print */
106 #define _I _CTYPE_I /* Ideogram */
107 #define _T _CTYPE_T /* Special */
108 #define _Q _CTYPE_Q /* Phonogram */
109 #define _SW0 _CTYPE_SW0 /* 0 width character */
110 #define _SW1 _CTYPE_SW1 /* 1 width character */
111 #define _SW2 _CTYPE_SW2 /* 2 width character */
112 #define _SW3 _CTYPE_SW3 /* 3 width character */
113
114 __BEGIN_DECLS
115 int isalnum(int);
116 int isalpha(int);
117 int isblank(int);
118 int iscntrl(int);
119 int isdigit(int);
120 int isgraph(int);
121 int islower(int);
122 int isprint(int);
123 int ispunct(int);
124 int isspace(int);
125 int isupper(int);
126 int isxdigit(int);
127 int tolower(int);
128 int toupper(int);
129
130 #if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
131 int _tolower(int);
132 int _toupper(int);
133 int digittoint(int);
134 int isascii(int);
135 int ishexnumber(int);
136 int isideogram(int);
137 int isnumber(int);
138 int isphonogram(int);
139 int isrune(int);
140 int isspecial(int);
141 int toascii(int);
142 #endif
143 __END_DECLS
144
145
146 #define isalnum(c) __istype((c), (_CTYPE_A|_CTYPE_D))
147 #define isalpha(c) __istype((c), _CTYPE_A)
148 #define isblank(c) __istype((c), _CTYPE_B)
149 #define iscntrl(c) __istype((c), _CTYPE_C)
150 #define isdigit(c) __isctype((c), _CTYPE_D) /* ANSI -- locale independent */
151 #define isgraph(c) __istype((c), _CTYPE_G)
152 #define islower(c) __istype((c), _CTYPE_L)
153 #define isprint(c) __istype((c), _CTYPE_R)
154 #define ispunct(c) __istype((c), _CTYPE_P)
155 #define isspace(c) __istype((c), _CTYPE_S)
156 #define isupper(c) __istype((c), _CTYPE_U)
157 #define isxdigit(c) __isctype((c), _CTYPE_X) /* ANSI -- locale independent */
158 #define tolower(c) __tolower(c)
159 #define toupper(c) __toupper(c)
160
161 #if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
162 #define _tolower(c) __tolower(c)
163 #define _toupper(c) __toupper(c)
164 #define digittoint(c) __maskrune((c), 0xFF)
165 #define isascii(c) (((c) & ~0x7F) == 0)
166 #define ishexnumber(c) __istype((c), _CTYPE_X)
167 #define isideogram(c) __istype((c), _CTYPE_I)
168 #define isnumber(c) __istype((c), _CTYPE_D)
169 #define isphonogram(c) __istype((c), _CTYPE_Q)
170 #define isrune(c) __istype((c), 0xFFFFFF00L)
171 #define isspecial(c) __istype((c), _CTYPE_T)
172 #define toascii(c) ((c) & 0x7F)
173 #endif
174
175 /* See comments in <machine/ansi.h> about _BSD_RUNE_T_. */
176 __BEGIN_DECLS
177 unsigned long ___runetype(_BSD_CT_RUNE_T_);
178 _BSD_CT_RUNE_T_ ___tolower(_BSD_CT_RUNE_T_);
179 _BSD_CT_RUNE_T_ ___toupper(_BSD_CT_RUNE_T_);
180 __END_DECLS
181
182 /*
183 * _EXTERNALIZE_CTYPE_INLINES_ is defined in locale/nomacros.c to tell us
184 * to generate code for extern versions of all our inline functions.
185 */
186 #ifdef _EXTERNALIZE_CTYPE_INLINES_
187 #define _USE_CTYPE_INLINE_
188 #define static
189 #define __inline
190 #endif
191
192 /*
193 * Use inline functions if we are allowed to and the compiler supports them.
194 */
195 #if !defined(_DONT_USE_CTYPE_INLINE_) && \
196 (defined(_USE_CTYPE_INLINE_) || defined(__GNUC__) || defined(__cplusplus))
197
198 static __inline int
199 __maskrune(_BSD_CT_RUNE_T_ _c, unsigned long _f)
200 {
201 #ifdef USE_ASCII
202 return _CurrentRuneLocale->runetype[_c && 0xff] & _f;
203 #else /* USE_ASCII */
204 return ((_c < 0 || _c >= _CACHED_RUNES) ? ___runetype(_c) :
205 _CurrentRuneLocale->runetype[_c]) & _f;
206 #endif /* USE_ASCII */
207 }
208
209 static __inline int
210 __istype(_BSD_CT_RUNE_T_ c, unsigned long f)
211 {
212 return !!(__maskrune(c, f));
213 }
214
215 static __inline _BSD_CT_RUNE_T_
216 __isctype(_BSD_CT_RUNE_T_ _c, unsigned long _f)
217 {
218 #ifdef USE_ASCII
219 return !!(_DefaultRuneLocale.runetype[_c & 0xff] & _f);
220 #else /* USE_ASCII */
221 return (_c < 0 || _c >= _CACHED_RUNES) ? 0 :
222 !!(_DefaultRuneLocale.runetype[_c] & _f);
223 #endif /* USE_ASCII */
224 }
225
226 static __inline _BSD_CT_RUNE_T_
227 __toupper(_BSD_CT_RUNE_T_ _c)
228 {
229 #ifdef USE_ASCII
230 return _CurrentRuneLocale->mapupper[_c & 0xff];
231 #else /* USE_ASCII */
232 return (_c < 0 || _c >= _CACHED_RUNES) ? ___toupper(_c) :
233 _CurrentRuneLocale->mapupper[_c];
234 #endif /* USE_ASCII */
235 }
236
237 static __inline _BSD_CT_RUNE_T_
238 __tolower(_BSD_CT_RUNE_T_ _c)
239 {
240 #ifdef USE_ASCII
241 return _CurrentRuneLocale->maplower[_c & 0xff];
242 #else /* USE_ASCII */
243 return (_c < 0 || _c >= _CACHED_RUNES) ? ___tolower(_c) :
244 _CurrentRuneLocale->maplower[_c];
245 #endif /* USE_ASCII */
246 }
247
248 #else /* not using inlines */
249
250 __BEGIN_DECLS
251 int __maskrune(_BSD_CT_RUNE_T_, unsigned long);
252 int __istype (_BSD_CT_RUNE_T_, unsigned long);
253 int __isctype(_BSD_CT_RUNE_T_, unsigned long);
254 _BSD_CT_RUNE_T_ __toupper(_BSD_CT_RUNE_T_);
255 _BSD_CT_RUNE_T_ __tolower(_BSD_CT_RUNE_T_);
256 __END_DECLS
257 #endif /* using inlines */
258
259 #endif /* !_CTYPE_H_ */