xref: /freebsd/include/ctype.h (revision 2357939bc239bd5334a169b62313806178dd8f30)
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. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)ctype.h	8.4 (Berkeley) 1/21/94
42  *      $FreeBSD$
43  */
44 
45 #ifndef _CTYPE_H_
46 #define	_CTYPE_H_
47 
48 #include <sys/cdefs.h>
49 #include <sys/_types.h>
50 
51 #define	_CTYPE_A	0x00000100L		/* Alpha */
52 #define	_CTYPE_C	0x00000200L		/* Control */
53 #define	_CTYPE_D	0x00000400L		/* Digit */
54 #define	_CTYPE_G	0x00000800L		/* Graph */
55 #define	_CTYPE_L	0x00001000L		/* Lower */
56 #define	_CTYPE_P	0x00002000L		/* Punct */
57 #define	_CTYPE_S	0x00004000L		/* Space */
58 #define	_CTYPE_U	0x00008000L		/* Upper */
59 #define	_CTYPE_X	0x00010000L		/* X digit */
60 #define	_CTYPE_B	0x00020000L		/* Blank */
61 #define	_CTYPE_R	0x00040000L		/* Print */
62 #define	_CTYPE_I	0x00080000L		/* Ideogram */
63 #define	_CTYPE_T	0x00100000L		/* Special */
64 #define	_CTYPE_Q	0x00200000L		/* Phonogram */
65 #define	_CTYPE_SW0	0x20000000L		/* 0 width character */
66 #define	_CTYPE_SW1	0x40000000L		/* 1 width character */
67 #define	_CTYPE_SW2	0x80000000L		/* 2 width character */
68 #define	_CTYPE_SW3	0xc0000000L		/* 3 width character */
69 
70 __BEGIN_DECLS
71 int	isalnum(int);
72 int	isalpha(int);
73 int	iscntrl(int);
74 int	isdigit(int);
75 int	isgraph(int);
76 int	islower(int);
77 int	isprint(int);
78 int	ispunct(int);
79 int	isspace(int);
80 int	isupper(int);
81 int	isxdigit(int);
82 int	tolower(int);
83 int	toupper(int);
84 
85 #if __XSI_VISIBLE
86 int	_tolower(int);
87 int	_toupper(int);
88 int	isascii(int);
89 int	toascii(int);
90 #endif
91 
92 #if __ISO_C_VISIBLE >= 1999
93 int	isblank(int);
94 #endif
95 
96 #if __BSD_VISIBLE
97 int	digittoint(int);
98 int	ishexnumber(int);
99 int	isideogram(int);
100 int	isnumber(int);
101 int	isphonogram(int);
102 int	isrune(int);
103 int	isspecial(int);
104 #endif
105 __END_DECLS
106 
107 #define	isalnum(c)	__istype((c), _CTYPE_A|_CTYPE_D)
108 #define	isalpha(c)	__istype((c), _CTYPE_A)
109 #define	iscntrl(c)	__istype((c), _CTYPE_C)
110 #define	isdigit(c)	__isctype((c), _CTYPE_D) /* ANSI -- locale independent */
111 #define	isgraph(c)	__istype((c), _CTYPE_G)
112 #define	islower(c)	__istype((c), _CTYPE_L)
113 #define	isprint(c)	__istype((c), _CTYPE_R)
114 #define	ispunct(c)	__istype((c), _CTYPE_P)
115 #define	isspace(c)	__istype((c), _CTYPE_S)
116 #define	isupper(c)	__istype((c), _CTYPE_U)
117 #define	isxdigit(c)	__isctype((c), _CTYPE_X) /* ANSI -- locale independent */
118 #define	tolower(c)	__tolower(c)
119 #define	toupper(c)	__toupper(c)
120 
121 #if __XSI_VISIBLE
122 /*
123  * POSIX.1-2001 specifies _tolower() and _toupper() to be macros equivalent to
124  * tolower() and toupper() respectively, minus extra checking to ensure that
125  * the argument is a lower or uppercase letter respectively.  We've chosen to
126  * implement these macros with the same error checking as tolower() and
127  * toupper() since this doesn't violate the specification itself, only its
128  * intent.  We purposely leave _tolower() and _toupper() undocumented to
129  * discourage their use.
130  *
131  * XXX isascii() and toascii() should similarly be undocumented.
132  */
133 #define	_tolower(c)	__tolower(c)
134 #define	_toupper(c)	__toupper(c)
135 #define	isascii(c)	(((c) & ~0x7F) == 0)
136 #define	toascii(c)	((c) & 0x7F)
137 #endif
138 
139 #if __ISO_C_VISIBLE >= 1999
140 #define	isblank(c)	__istype((c), _CTYPE_B)
141 #endif
142 
143 #if __BSD_VISIBLE
144 #define	digittoint(c)	__maskrune((c), 0xFF)
145 #define	ishexnumber(c)	__istype((c), _CTYPE_X)
146 #define	isideogram(c)	__istype((c), _CTYPE_I)
147 #define	isnumber(c)	__istype((c), _CTYPE_D)
148 #define	isphonogram(c)	__istype((c), _CTYPE_Q)
149 #define	isrune(c)	__istype((c), 0xFFFFFF00L)
150 #define	isspecial(c)	__istype((c), _CTYPE_T)
151 #endif
152 
153 /* See comments in <sys/_types.h> about __ct_rune_t. */
154 __BEGIN_DECLS
155 unsigned long	___runetype(__ct_rune_t);
156 __ct_rune_t	___tolower(__ct_rune_t);
157 __ct_rune_t	___toupper(__ct_rune_t);
158 __END_DECLS
159 
160 /*
161  * _EXTERNALIZE_CTYPE_INLINES_ is defined in locale/nomacros.c to tell us
162  * to generate code for extern versions of all our inline functions.
163  */
164 #ifdef _EXTERNALIZE_CTYPE_INLINES_
165 #define	_USE_CTYPE_INLINE_
166 #define	static
167 #define	__inline
168 #endif
169 
170 /*
171  * <runetype.h> brings namespace pollution (struct member names).  This prevents
172  * us from using the inline optimizations in the more strict __POSIX_VISIBLE and
173  * __XSI_VISIBLE namespaces.  To fix this properly would require that we rename
174  * member names of long-standing structs, or something equally evil.
175  */
176 #if !__BSD_VISIBLE && !defined(_USE_CTYPE_INLINE_) && \
177     !defined(_DONT_USE_CTYPE_INLINE_)
178 #define	_DONT_USE_CTYPE_INLINE_
179 #endif
180 
181 /*
182  * Use inline functions if we are allowed to and the compiler supports them.
183  */
184 #if !defined(_DONT_USE_CTYPE_INLINE_) && \
185     (defined(_USE_CTYPE_INLINE_) || defined(__GNUC__) || defined(__cplusplus))
186 
187 #include <runetype.h>
188 
189 static __inline int
190 __maskrune(__ct_rune_t _c, unsigned long _f)
191 {
192 	return ((_c < 0 || _c >= _CACHED_RUNES) ? ___runetype(_c) :
193 		_CurrentRuneLocale->runetype[_c]) & _f;
194 }
195 
196 static __inline int
197 __istype(__ct_rune_t _c, unsigned long _f)
198 {
199 	return (!!__maskrune(_c, _f));
200 }
201 
202 static __inline int
203 __isctype(__ct_rune_t _c, unsigned long _f)
204 {
205 	return (_c < 0 || _c >= _CACHED_RUNES) ? 0 :
206 	       !!(_DefaultRuneLocale.runetype[_c] & _f);
207 }
208 
209 static __inline __ct_rune_t
210 __toupper(__ct_rune_t _c)
211 {
212 	return (_c < 0 || _c >= _CACHED_RUNES) ? ___toupper(_c) :
213 	       _CurrentRuneLocale->mapupper[_c];
214 }
215 
216 static __inline __ct_rune_t
217 __tolower(__ct_rune_t _c)
218 {
219 	return (_c < 0 || _c >= _CACHED_RUNES) ? ___tolower(_c) :
220 	       _CurrentRuneLocale->maplower[_c];
221 }
222 
223 #else /* not using inlines */
224 
225 __BEGIN_DECLS
226 int		__maskrune(__ct_rune_t, unsigned long);
227 int		__istype(__ct_rune_t, unsigned long);
228 int		__isctype(__ct_rune_t, unsigned long);
229 __ct_rune_t	__toupper(__ct_rune_t);
230 __ct_rune_t	__tolower(__ct_rune_t);
231 __END_DECLS
232 #endif /* using inlines */
233 
234 #endif /* !_CTYPE_H_ */
235