1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 /**
32  * @file ctype.h
33  * @brief ASCII character classification.
34  */
35 
36 #include <sys/cdefs.h>
37 #include <xlocale.h>
38 
39 /* All the functions in this file are trivial, being but a single
40  * instruction on most architectures. For that reason, we inline them by
41  * default. This macro is meant for internal use only, so that we can
42  * also provide actual symbols for any caller that needs them.
43  */
44 #if !defined(__BIONIC_CTYPE_INLINE)
45 #define __BIONIC_CTYPE_INLINE static __inline
46 #endif
47 
48 /** Internal implementation detail. Do not use. */
49 #define _CTYPE_U 0x01
50 /** Internal implementation detail. Do not use. */
51 #define _CTYPE_L 0x02
52 /** Internal implementation detail. Do not use. */
53 #define _CTYPE_D 0x04
54 /** Internal implementation detail. Do not use. */
55 #define _CTYPE_S 0x08
56 /** Internal implementation detail. Do not use. */
57 #define _CTYPE_P 0x10
58 /** Internal implementation detail. Do not use. */
59 #define _CTYPE_C 0x20
60 /** Internal implementation detail. Do not use. */
61 #define _CTYPE_X 0x40
62 /** Internal implementation detail. Do not use. */
63 #define _CTYPE_B 0x80
64 /** Internal implementation detail. Do not use. */
65 #define _CTYPE_R (_CTYPE_P|_CTYPE_U|_CTYPE_L|_CTYPE_D|_CTYPE_B)
66 /** Internal implementation detail. Do not use. */
67 #define _CTYPE_A (_CTYPE_L|_CTYPE_U)
68 /** Internal implementation detail. Do not use. */
69 #define _CTYPE_N _CTYPE_D
70 
71 __BEGIN_DECLS
72 
73 /** Internal implementation detail. Do not use. */
74 extern const char* _ctype_;
75 
76 /** Returns true if `ch` is in `[A-Za-z]`. */
isalpha(int __ch)77 __BIONIC_CTYPE_INLINE int isalpha(int __ch) {
78   return (__ch >= 'A' && __ch <= 'Z') || (__ch >= 'a' && __ch <= 'z');
79 }
80 
81 /** Returns true if `ch` is a space or tab. */
isblank(int __ch)82 __BIONIC_CTYPE_INLINE int isblank(int __ch) {
83   return __ch == ' ' || __ch == '\t';
84 }
85 
86 /** Returns true if `ch` is a control character (any character before space, plus DEL). */
iscntrl(int __ch)87 __BIONIC_CTYPE_INLINE int iscntrl(int __ch) {
88   return (__BIONIC_CAST(static_cast, unsigned, __ch) < ' ') || __ch == 0x7f;
89 }
90 
91 /** Returns true if `ch` is in `[0-9]`. */
isdigit(int __ch)92 __BIONIC_CTYPE_INLINE int isdigit(int __ch) {
93   return (__ch >= '0' && __ch <= '9');
94 }
95 
96 /** Returns true if `ch` is `[A-Za-z0-9]` or punctuation. */
isgraph(int __ch)97 __BIONIC_CTYPE_INLINE int isgraph(int __ch) {
98   return (__ch >= '!' && __ch <= '~');
99 }
100 
101 /** Returns true if `ch` is in `[a-z]`. */
islower(int __ch)102 __BIONIC_CTYPE_INLINE int islower(int __ch) {
103   return (__ch >= 'a' && __ch <= 'z');
104 }
105 
106 /** Returns true if `ch` is `[A-Za-z0-9]` or punctuation or space. */
isprint(int __ch)107 __BIONIC_CTYPE_INLINE int isprint(int __ch) {
108   return (__ch >= ' ' && __ch <= '~');
109 }
110 
111 /** Returns true if `ch` is in `[ \f\n\r\t\v]`. */
isspace(int __ch)112 __BIONIC_CTYPE_INLINE int isspace(int __ch) {
113   return __ch == ' ' || (__ch >= '\t' && __ch <= '\r');
114 }
115 
116 /** Returns true if `ch` is in `[A-Z]`. */
isupper(int __ch)117 __BIONIC_CTYPE_INLINE int isupper(int __ch) {
118   return (__ch >= 'A' && __ch <= 'Z');
119 }
120 
121 /** Returns true if `ch` is in `[0-9A-Fa-f]`. */
isxdigit(int __ch)122 __BIONIC_CTYPE_INLINE int isxdigit(int __ch) {
123   return (__ch >= '0' && __ch <= '9') || (__ch >= 'a' && __ch <= 'f') || (__ch >= 'A' && __ch <= 'F');
124 }
125 
126 /** Returns true if `ch` is in `[A-Za-z0-9]`. */
isalnum(int __ch)127 __BIONIC_CTYPE_INLINE int isalnum(int __ch) {
128   return isalpha(__ch) || isdigit(__ch);
129 }
130 
131 /** Returns true if `ch` is punctuation. */
ispunct(int __ch)132 __BIONIC_CTYPE_INLINE int ispunct(int __ch) {
133   return isgraph(__ch) && !isalnum(__ch);
134 }
135 
136 /**
137  * Returns the corresponding lower-case character if `ch` is upper-case, or undefined otherwise.
138  *
139  * Prefer tolower() instead.
140  */
_tolower(int __ch)141 __BIONIC_CTYPE_INLINE int _tolower(int __ch) {
142   return __ch | 0x20;
143 }
144 
145 /** Returns the corresponding lower-case character if `ch` is upper-case, or `ch` otherwise. */
tolower(int __ch)146 __BIONIC_CTYPE_INLINE int tolower(int __ch) {
147   if (__ch >= 'A' && __ch <= 'Z') return _tolower(__ch);
148   return __ch;
149 }
150 
151 /**
152  * Returns the corresponding upper-case character if `ch` is lower-case, or undefined otherwise.
153  *
154  * Prefer toupper() instead.
155  */
_toupper(int __ch)156 __BIONIC_CTYPE_INLINE int _toupper(int __ch) {
157   // Using EOR rather than AND makes no difference on arm, but saves an
158   // instruction on arm64.
159   return __ch ^ 0x20;
160 }
161 
162 /** Returns the corresponding upper-case character if `ch` is lower-case, or `ch` otherwise. */
toupper(int __ch)163 __BIONIC_CTYPE_INLINE int toupper(int __ch) {
164   if (__ch >= 'a' && __ch <= 'z') return _toupper(__ch);
165   return __ch;
166 }
167 
168 /** Returns true if `ch` is less than 0x80. */
isascii(int __ch)169 __BIONIC_CTYPE_INLINE int isascii(int __ch) {
170   return __BIONIC_CAST(static_cast, unsigned, __ch) < 0x80;
171 }
172 
173 /** Returns `ch & 0x7f`. */
toascii(int __ch)174 __BIONIC_CTYPE_INLINE int toascii(int __ch) {
175   return __ch & 0x7f;
176 }
177 
178 /** Like isalnum() but with an ignored `locale_t`. */
isalnum_l(int __ch,locale_t __l)179 __BIONIC_CTYPE_INLINE int isalnum_l(int __ch, locale_t __l) {
180   return isalnum(__ch);
181 }
182 
183 /** Like isalpha() but with an ignored `locale_t`. */
isalpha_l(int __ch,locale_t __l)184 __BIONIC_CTYPE_INLINE int isalpha_l(int __ch, locale_t __l) {
185   return isalpha(__ch);
186 }
187 
188 /** Like isblank() but with an ignored `locale_t`. */
isblank_l(int __ch,locale_t __l)189 __BIONIC_CTYPE_INLINE int isblank_l(int __ch, locale_t __l) {
190   return isblank(__ch);
191 }
192 
193 /** Like iscntrl() but with an ignored `locale_t`. */
iscntrl_l(int __ch,locale_t __l)194 __BIONIC_CTYPE_INLINE int iscntrl_l(int __ch, locale_t __l) {
195   return iscntrl(__ch);
196 }
197 
198 /** Like isdigit() but with an ignored `locale_t`. */
isdigit_l(int __ch,locale_t __l)199 __BIONIC_CTYPE_INLINE int isdigit_l(int __ch, locale_t __l) {
200   return isdigit(__ch);
201 }
202 
203 /** Like isgraph() but with an ignored `locale_t`. */
isgraph_l(int __ch,locale_t __l)204 __BIONIC_CTYPE_INLINE int isgraph_l(int __ch, locale_t __l) {
205   return isgraph(__ch);
206 }
207 
208 /** Like islower() but with an ignored `locale_t`. */
islower_l(int __ch,locale_t __l)209 __BIONIC_CTYPE_INLINE int islower_l(int __ch, locale_t __l) {
210   return islower(__ch);
211 }
212 
213 /** Like isprint() but with an ignored `locale_t`. */
isprint_l(int __ch,locale_t __l)214 __BIONIC_CTYPE_INLINE int isprint_l(int __ch, locale_t __l) {
215   return isprint(__ch);
216 }
217 
218 /** Like ispunct() but with an ignored `locale_t`. */
ispunct_l(int __ch,locale_t __l)219 __BIONIC_CTYPE_INLINE int ispunct_l(int __ch, locale_t __l) {
220   return ispunct(__ch);
221 }
222 
223 /** Like isspace() but with an ignored `locale_t`. */
isspace_l(int __ch,locale_t __l)224 __BIONIC_CTYPE_INLINE int isspace_l(int __ch, locale_t __l) {
225   return isspace(__ch);
226 }
227 
228 /** Like isupper() but with an ignored `locale_t`. */
isupper_l(int __ch,locale_t __l)229 __BIONIC_CTYPE_INLINE int isupper_l(int __ch, locale_t __l) {
230   return isupper(__ch);
231 }
232 
233 /** Like isxdigit() but with an ignored `locale_t`. */
isxdigit_l(int __ch,locale_t __l)234 __BIONIC_CTYPE_INLINE int isxdigit_l(int __ch, locale_t __l) {
235   return isxdigit(__ch);
236 }
237 
238 /** Like tolower() but with an ignored `locale_t`. */
tolower_l(int __ch,locale_t __l)239 __BIONIC_CTYPE_INLINE int tolower_l(int __ch, locale_t __l) {
240   return tolower(__ch);
241 }
242 
243 /** Like toupper() but with an ignored `locale_t`. */
toupper_l(int __ch,locale_t __l)244 __BIONIC_CTYPE_INLINE int toupper_l(int __ch, locale_t __l) {
245   return toupper(__ch);
246 }
247 
248 __END_DECLS
249