xref: /PHP-7.4/ext/ctype/ctype.c (revision 92ac598a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Hartmut Holzgraefe <hholzgra@php.net>                        |
16    +----------------------------------------------------------------------+
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include "php.h"
24 #include "php_ini.h"
25 #include "php_ctype.h"
26 #include "SAPI.h"
27 #include "ext/standard/info.h"
28 
29 #include <ctype.h>
30 
31 #if HAVE_CTYPE
32 
33 static PHP_MINFO_FUNCTION(ctype);
34 
35 static PHP_FUNCTION(ctype_alnum);
36 static PHP_FUNCTION(ctype_alpha);
37 static PHP_FUNCTION(ctype_cntrl);
38 static PHP_FUNCTION(ctype_digit);
39 static PHP_FUNCTION(ctype_lower);
40 static PHP_FUNCTION(ctype_graph);
41 static PHP_FUNCTION(ctype_print);
42 static PHP_FUNCTION(ctype_punct);
43 static PHP_FUNCTION(ctype_space);
44 static PHP_FUNCTION(ctype_upper);
45 static PHP_FUNCTION(ctype_xdigit);
46 
47 /* {{{ arginfo */
48 ZEND_BEGIN_ARG_INFO(arginfo_ctype_alnum, 0)
49 	ZEND_ARG_INFO(0, text)
50 ZEND_END_ARG_INFO()
51 
52 ZEND_BEGIN_ARG_INFO(arginfo_ctype_alpha, 0)
53 	ZEND_ARG_INFO(0, text)
54 ZEND_END_ARG_INFO()
55 
56 ZEND_BEGIN_ARG_INFO(arginfo_ctype_cntrl, 0)
57 	ZEND_ARG_INFO(0, text)
58 ZEND_END_ARG_INFO()
59 
60 ZEND_BEGIN_ARG_INFO(arginfo_ctype_digit, 0)
61 	ZEND_ARG_INFO(0, text)
62 ZEND_END_ARG_INFO()
63 
64 ZEND_BEGIN_ARG_INFO(arginfo_ctype_lower, 0)
65 	ZEND_ARG_INFO(0, text)
66 ZEND_END_ARG_INFO()
67 
68 ZEND_BEGIN_ARG_INFO(arginfo_ctype_graph, 0)
69 	ZEND_ARG_INFO(0, text)
70 ZEND_END_ARG_INFO()
71 
72 ZEND_BEGIN_ARG_INFO(arginfo_ctype_print, 0)
73 	ZEND_ARG_INFO(0, text)
74 ZEND_END_ARG_INFO()
75 
76 ZEND_BEGIN_ARG_INFO(arginfo_ctype_punct, 0)
77 	ZEND_ARG_INFO(0, text)
78 ZEND_END_ARG_INFO()
79 
80 ZEND_BEGIN_ARG_INFO(arginfo_ctype_space, 0)
81 	ZEND_ARG_INFO(0, text)
82 ZEND_END_ARG_INFO()
83 
84 ZEND_BEGIN_ARG_INFO(arginfo_ctype_upper, 0)
85 	ZEND_ARG_INFO(0, text)
86 ZEND_END_ARG_INFO()
87 
88 ZEND_BEGIN_ARG_INFO(arginfo_ctype_xdigit, 0)
89 	ZEND_ARG_INFO(0, text)
90 ZEND_END_ARG_INFO()
91 
92 /* }}} */
93 
94 /* {{{ ctype_functions[]
95  * Every user visible function must have an entry in ctype_functions[].
96  */
97 static const zend_function_entry ctype_functions[] = {
98 	PHP_FE(ctype_alnum,	arginfo_ctype_alnum)
99 	PHP_FE(ctype_alpha,	arginfo_ctype_alpha)
100 	PHP_FE(ctype_cntrl,	arginfo_ctype_cntrl)
101 	PHP_FE(ctype_digit,	arginfo_ctype_digit)
102 	PHP_FE(ctype_lower,	arginfo_ctype_lower)
103 	PHP_FE(ctype_graph,	arginfo_ctype_graph)
104 	PHP_FE(ctype_print,	arginfo_ctype_print)
105 	PHP_FE(ctype_punct,	arginfo_ctype_punct)
106 	PHP_FE(ctype_space,	arginfo_ctype_space)
107 	PHP_FE(ctype_upper,	arginfo_ctype_upper)
108 	PHP_FE(ctype_xdigit,	arginfo_ctype_xdigit)
109 	PHP_FE_END
110 };
111 /* }}} */
112 
113 /* {{{ ctype_module_entry
114  */
115 zend_module_entry ctype_module_entry = {
116 	STANDARD_MODULE_HEADER,
117 	"ctype",
118 	ctype_functions,
119 	NULL,
120 	NULL,
121 	NULL,
122 	NULL,
123 	PHP_MINFO(ctype),
124     PHP_CTYPE_VERSION,
125 	STANDARD_MODULE_PROPERTIES
126 };
127 /* }}} */
128 
129 #ifdef COMPILE_DL_CTYPE
130 ZEND_GET_MODULE(ctype)
131 #endif
132 
133 /* {{{ PHP_MINFO_FUNCTION
134  */
PHP_MINFO_FUNCTION(ctype)135 static PHP_MINFO_FUNCTION(ctype)
136 {
137 	php_info_print_table_start();
138 	php_info_print_table_row(2, "ctype functions", "enabled");
139 	php_info_print_table_end();
140 }
141 /* }}} */
142 
143 /* {{{ ctype
144  */
145 #define CTYPE(iswhat, allow_digits, allow_minus) \
146 	zval *c; \
147 	ZEND_PARSE_PARAMETERS_START(1, 1); \
148 		Z_PARAM_ZVAL(c) \
149 	ZEND_PARSE_PARAMETERS_END(); \
150 	if (Z_TYPE_P(c) == IS_LONG) { \
151 		if (Z_LVAL_P(c) <= 255 && Z_LVAL_P(c) >= 0) { \
152 			RETURN_BOOL(iswhat((int)Z_LVAL_P(c))); \
153 		} else if (Z_LVAL_P(c) >= -128 && Z_LVAL_P(c) < 0) { \
154 			RETURN_BOOL(iswhat((int)Z_LVAL_P(c) + 256)); \
155 		} else if (Z_LVAL_P(c) >= 0) { \
156 			RETURN_BOOL(allow_digits); \
157 		} else { \
158 			RETURN_BOOL(allow_minus); \
159 		} \
160 	} else if (Z_TYPE_P(c) == IS_STRING) { \
161 		char *p = Z_STRVAL_P(c), *e = Z_STRVAL_P(c) + Z_STRLEN_P(c); \
162 		if (e == p) {	\
163 			RETURN_FALSE;	\
164 		}	\
165 		while (p < e) { \
166 			if(!iswhat((int)*(unsigned char *)(p++))) { \
167 				RETURN_FALSE; \
168 			} \
169 		} \
170 		RETURN_TRUE; \
171 	} else { \
172 		RETURN_FALSE; \
173 	} \
174 
175 /* }}} */
176 
177 /* {{{ proto bool ctype_alnum(mixed c)
178    Checks for alphanumeric character(s) */
PHP_FUNCTION(ctype_alnum)179 static PHP_FUNCTION(ctype_alnum)
180 {
181 	CTYPE(isalnum, 1, 0);
182 }
183 /* }}} */
184 
185 /* {{{ proto bool ctype_alpha(mixed c)
186    Checks for alphabetic character(s) */
PHP_FUNCTION(ctype_alpha)187 static PHP_FUNCTION(ctype_alpha)
188 {
189 	CTYPE(isalpha, 0, 0);
190 }
191 /* }}} */
192 
193 /* {{{ proto bool ctype_cntrl(mixed c)
194    Checks for control character(s) */
PHP_FUNCTION(ctype_cntrl)195 static PHP_FUNCTION(ctype_cntrl)
196 {
197 	CTYPE(iscntrl, 0, 0);
198 }
199 /* }}} */
200 
201 /* {{{ proto bool ctype_digit(mixed c)
202    Checks for numeric character(s) */
PHP_FUNCTION(ctype_digit)203 static PHP_FUNCTION(ctype_digit)
204 {
205 	CTYPE(isdigit, 1, 0);
206 }
207 /* }}} */
208 
209 /* {{{ proto bool ctype_lower(mixed c)
210    Checks for lowercase character(s)  */
PHP_FUNCTION(ctype_lower)211 static PHP_FUNCTION(ctype_lower)
212 {
213 	CTYPE(islower, 0, 0);
214 }
215 /* }}} */
216 
217 /* {{{ proto bool ctype_graph(mixed c)
218    Checks for any printable character(s) except space */
PHP_FUNCTION(ctype_graph)219 static PHP_FUNCTION(ctype_graph)
220 {
221 	CTYPE(isgraph, 1, 1);
222 }
223 /* }}} */
224 
225 /* {{{ proto bool ctype_print(mixed c)
226    Checks for printable character(s) */
PHP_FUNCTION(ctype_print)227 static PHP_FUNCTION(ctype_print)
228 {
229 	CTYPE(isprint, 1, 1);
230 }
231 /* }}} */
232 
233 /* {{{ proto bool ctype_punct(mixed c)
234    Checks for any printable character which is not whitespace or an alphanumeric character */
PHP_FUNCTION(ctype_punct)235 static PHP_FUNCTION(ctype_punct)
236 {
237 	CTYPE(ispunct, 0, 0);
238 }
239 /* }}} */
240 
241 /* {{{ proto bool ctype_space(mixed c)
242    Checks for whitespace character(s)*/
PHP_FUNCTION(ctype_space)243 static PHP_FUNCTION(ctype_space)
244 {
245 	CTYPE(isspace, 0, 0);
246 }
247 /* }}} */
248 
249 /* {{{ proto bool ctype_upper(mixed c)
250    Checks for uppercase character(s) */
PHP_FUNCTION(ctype_upper)251 static PHP_FUNCTION(ctype_upper)
252 {
253 	CTYPE(isupper, 0, 0);
254 }
255 /* }}} */
256 
257 /* {{{ proto bool ctype_xdigit(mixed c)
258    Checks for character(s) representing a hexadecimal digit */
PHP_FUNCTION(ctype_xdigit)259 static PHP_FUNCTION(ctype_xdigit)
260 {
261 	CTYPE(isxdigit, 1, 0);
262 }
263 /* }}} */
264 
265 #endif	/* HAVE_CTYPE */
266