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 #include <sys/cdefs.h>
32 #include <xlocale.h>
33 
34 #if !defined(__BIONIC_CTYPE_INLINE)
35 #define __BIONIC_CTYPE_INLINE static __inline
36 #endif
37 
38 /** Internal implementation detail. Do not use. */
39 #define _CTYPE_U 0x01
40 /** Internal implementation detail. Do not use. */
41 #define _CTYPE_L 0x02
42 /** Internal implementation detail. Do not use. */
43 #define _CTYPE_D 0x04
44 /** Internal implementation detail. Do not use. */
45 #define _CTYPE_S 0x08
46 /** Internal implementation detail. Do not use. */
47 #define _CTYPE_P 0x10
48 /** Internal implementation detail. Do not use. */
49 #define _CTYPE_C 0x20
50 /** Internal implementation detail. Do not use. */
51 #define _CTYPE_X 0x40
52 /** Internal implementation detail. Do not use. */
53 #define _CTYPE_B 0x80
54 /** Internal implementation detail. Do not use. */
55 #define _CTYPE_R (_CTYPE_P|_CTYPE_U|_CTYPE_L|_CTYPE_D|_CTYPE_B)
56 /** Internal implementation detail. Do not use. */
57 #define _CTYPE_A (_CTYPE_L|_CTYPE_U)
58 /** Internal implementation detail. Do not use. */
59 #define _CTYPE_N _CTYPE_D
60 
61 __BEGIN_DECLS
62 
63 /** Internal implementation detail. Do not use. */
64 extern const char* _ctype_;
65 
66 /** Returns true if `ch` is in `[A-Za-z0-9]`. */
isalnum(int __ch)67 __BIONIC_CTYPE_INLINE int isalnum(int __ch) {
68   // `isalnum(c)` is `isalpha(c) || isdigit(c)`, but there's no obvious way
69   // to simplify that, and the table lookup is just slightly faster...
70   // Note that this is unsafe for inputs less than -1 (EOF) or greater than
71   // 0xff. This is true of other C libraries too.
72   return (_ctype_[__ch + 1] & (_CTYPE_U|_CTYPE_L|_CTYPE_N));
73 }
74 
75 /** Returns true if `ch` is in `[A-Za-z]`. */
isalpha(int __ch)76 __BIONIC_CTYPE_INLINE int isalpha(int __ch) {
77   return (__ch >= 'A' && __ch <= 'Z') || (__ch >= 'a' && __ch <= 'z');
78 }
79 
80 /** Returns true if `ch` is a space or tab. */
isblank(int __ch)81 __BIONIC_CTYPE_INLINE int isblank(int __ch) {
82   return __ch == ' ' || __ch == '\t';
83 }
84 
85 /** Returns true if `ch` is a control character (any character before space, plus DEL). */
iscntrl(int __ch)86 __BIONIC_CTYPE_INLINE int iscntrl(int __ch) {
87   return (__BIONIC_CAST(static_cast, unsigned, __ch) < ' ') || __ch == 0x7f;
88 }
89 
90 /** Returns true if `ch` is in `[0-9]`. */
isdigit(int __ch)91 __BIONIC_CTYPE_INLINE int isdigit(int __ch) {
92   return (__ch >= '0' && __ch <= '9');
93 }
94 
95 /** Returns true if `ch` is `[A-Za-z0-9]` or punctuation. */
isgraph(int __ch)96 __BIONIC_CTYPE_INLINE int isgraph(int __ch) {
97   return (__ch >= '!' && __ch <= '~');
98 }
99 
100 /** Returns true if `ch` is in `[a-z]`. */
islower(int __ch)101 __BIONIC_CTYPE_INLINE int islower(int __ch) {
102   return (__ch >= 'a' && __ch <= 'z');
103 }
104 
105 /** Returns true if `ch` is `[A-Za-z0-9]` or punctuation or space. */
isprint(int __ch)106 __BIONIC_CTYPE_INLINE int isprint(int __ch) {
107   return (__ch >= ' ' && __ch <= '~');
108 }
109 
110 /** Returns true if `ch` is punctuation. */
ispunct(int __ch)111 __BIONIC_CTYPE_INLINE int ispunct(int __ch) {
112   // `ispunct(c)` is `isgraph(c) && !isalnum(c)`, but there's no obvious way
113   // to simplify that, and the table lookup is just slightly faster...
114   // Note that this is unsafe for inputs less than -1 (EOF) or greater than
115   // 0xff. This is true of other C libraries too.
116   return (_ctype_[__ch + 1] & _CTYPE_P);
117 }
118 
119 /** Returns true if `ch` is in `[ \f\n\r\t\v]`. */
isspace(int __ch)120 __BIONIC_CTYPE_INLINE int isspace(int __ch) {
121   return __ch == ' ' || (__ch >= '\t' && __ch <= '\r');
122 }
123 
124 /** Returns true if `ch` is in `[A-Z]`. */
isupper(int __ch)125 __BIONIC_CTYPE_INLINE int isupper(int __ch) {
126   return (__ch >= 'A' && __ch <= 'Z');
127 }
128 
129 /** Returns true if `ch` is in `[0-9A-Fa-f]`. */
isxdigit(int __ch)130 __BIONIC_CTYPE_INLINE int isxdigit(int __ch) {
131   return (__ch >= '0' && __ch <= '9') || (__ch >= 'a' && __ch <= 'f') || (__ch >= 'A' && __ch <= 'F');
132 }
133 
134 /**
135  * Returns the corresponding lower-case character if `ch` is upper-case, or undefined otherwise.
136  *
137  * Prefer tolower() instead.
138  */
_tolower(int __ch)139 __BIONIC_CTYPE_INLINE int _tolower(int __ch) {
140   return __ch | 0x20;
141 }
142 
143 /** Returns the corresponding lower-case character if `ch` is upper-case, or `ch` otherwise. */
tolower(int __ch)144 __BIONIC_CTYPE_INLINE int tolower(int __ch) {
145   if (__ch >= 'A' && __ch <= 'Z') return _tolower(__ch);
146   return __ch;
147 }
148 
149 /**
150  * Returns the corresponding upper-case character if `ch` is lower-case, or undefined otherwise.
151  *
152  * Prefer toupper() instead.
153  */
_toupper(int __ch)154 __BIONIC_CTYPE_INLINE int _toupper(int __ch) {
155   // Using EOR rather than AND makes no difference on arm, but saves an
156   // instruction on arm64.
157   return __ch ^ 0x20;
158 }
159 
160 /** Returns the corresponding upper-case character if `ch` is lower-case, or `ch` otherwise. */
toupper(int __ch)161 __BIONIC_CTYPE_INLINE int toupper(int __ch) {
162   if (__ch >= 'a' && __ch <= 'z') return _toupper(__ch);
163   return __ch;
164 }
165 
166 /** Returns true if `ch` is less than 0x80. */
isascii(int __ch)167 __BIONIC_CTYPE_INLINE int isascii(int __ch) {
168   return __BIONIC_CAST(static_cast, unsigned, __ch) < 0x80;
169 }
170 
171 /** Returns `ch & 0x7f`. */
toascii(int __ch)172 __BIONIC_CTYPE_INLINE int toascii(int __ch) {
173   return __ch & 0x7f;
174 }
175 
176 #if __ANDROID_API__ >= 21
177 /** Like isalnum but with an ignored `locale_t`. */
178 int isalnum_l(int __ch, locale_t __l) __INTRODUCED_IN(21);
179 /** Like isalpha but with an ignored `locale_t`. */
180 int isalpha_l(int __ch, locale_t __l) __INTRODUCED_IN(21);
181 /** Like isblank but with an ignored `locale_t`. */
182 int isblank_l(int __ch, locale_t __l) __INTRODUCED_IN(21);
183 /** Like iscntrl but with an ignored `locale_t`. */
184 int iscntrl_l(int __ch, locale_t __l) __INTRODUCED_IN(21);
185 /** Like isdigit but with an ignored `locale_t`. */
186 int isdigit_l(int __ch, locale_t __l) __INTRODUCED_IN(21);
187 /** Like isgraph but with an ignored `locale_t`. */
188 int isgraph_l(int __ch, locale_t __l) __INTRODUCED_IN(21);
189 /** Like islower but with an ignored `locale_t`. */
190 int islower_l(int __ch, locale_t __l) __INTRODUCED_IN(21);
191 /** Like isprint but with an ignored `locale_t`. */
192 int isprint_l(int __ch, locale_t __l) __INTRODUCED_IN(21);
193 /** Like ispunct but with an ignored `locale_t`. */
194 int ispunct_l(int __ch, locale_t __l) __INTRODUCED_IN(21);
195 /** Like isspace but with an ignored `locale_t`. */
196 int isspace_l(int __ch, locale_t __l) __INTRODUCED_IN(21);
197 /** Like isupper but with an ignored `locale_t`. */
198 int isupper_l(int __ch, locale_t __l) __INTRODUCED_IN(21);
199 /** Like isxdigit but with an ignored `locale_t`. */
200 int isxdigit_l(int __ch, locale_t __l) __INTRODUCED_IN(21);
201 /** Like tolower but with an ignored `locale_t`. */
202 int tolower_l(int __ch, locale_t __l) __INTRODUCED_IN(21);
203 /** Like toupper but with an ignored `locale_t`. */
204 int toupper_l(int __ch, locale_t __l) __INTRODUCED_IN(21);
205 #else
206 // Implemented as static inlines in libc++ before 21.
207 #endif
208 
209 __END_DECLS
210