xref: /php-src/ext/ctype/ctype.c (revision 07811b63)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Hartmut Holzgraefe <hholzgra@php.net>                        |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "php.h"
22 #include "php_ini.h"
23 #include "php_ctype.h"
24 #include "ctype_arginfo.h"
25 #include "SAPI.h"
26 #include "ext/standard/info.h"
27 
28 #include <ctype.h>
29 
30 #ifdef HAVE_CTYPE
31 
32 static PHP_MINFO_FUNCTION(ctype);
33 
34 /* }}} */
35 
36 /* {{{ ctype_module_entry */
37 zend_module_entry ctype_module_entry = {
38 	STANDARD_MODULE_HEADER,
39 	"ctype",
40 	ext_functions,
41 	NULL,
42 	NULL,
43 	NULL,
44 	NULL,
45 	PHP_MINFO(ctype),
46 	PHP_CTYPE_VERSION,
47 	STANDARD_MODULE_PROPERTIES
48 };
49 /* }}} */
50 
51 #ifdef COMPILE_DL_CTYPE
52 ZEND_GET_MODULE(ctype)
53 #endif
54 
55 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(ctype)56 static PHP_MINFO_FUNCTION(ctype)
57 {
58 	php_info_print_table_start();
59 	php_info_print_table_row(2, "ctype functions", "enabled");
60 	php_info_print_table_end();
61 }
62 /* }}} */
63 
64 /* Slow fallback for deprecated cases defined in a no-inline function to not bloat code. */
ctype_fallback(zval * c,zval * return_value,int (* iswhat)(int),bool allow_digits,bool allow_minus)65 static zend_never_inline void ctype_fallback(zval *c, zval *return_value, int (*iswhat)(int), bool allow_digits, bool allow_minus)
66 {
67 	php_error_docref(NULL, E_DEPRECATED,
68 		"Argument of type %s will be interpreted as string in the future", zend_zval_type_name(c));
69 	if (Z_TYPE_P(c) == IS_LONG) {
70 		if (Z_LVAL_P(c) <= 255 && Z_LVAL_P(c) >= 0) {
71 			RETURN_BOOL(iswhat((int)Z_LVAL_P(c)));
72 		} else if (Z_LVAL_P(c) >= -128 && Z_LVAL_P(c) < 0) {
73 			RETURN_BOOL(iswhat((int)Z_LVAL_P(c) + 256));
74 		} else if (Z_LVAL_P(c) >= 0) {
75 			RETURN_BOOL(allow_digits);
76 		} else {
77 			RETURN_BOOL(allow_minus);
78 		}
79 	} else {
80 		RETURN_FALSE;
81 	}
82 }
83 
84 /* Define as a macro such that iswhat can use the macro version instead of the function version.
85  * This heavily reduces the overhead. (GH-11997) */
86 #define ctype_impl(iswhat, allow_digits, allow_minus) do { 					\
87 		zval *c;															\
88 																			\
89 		ZEND_PARSE_PARAMETERS_START(1, 1);									\
90 			Z_PARAM_ZVAL(c)													\
91 		ZEND_PARSE_PARAMETERS_END();										\
92 																			\
93 		if (Z_TYPE_P(c) == IS_STRING) {										\
94 			char *p = Z_STRVAL_P(c), *e = Z_STRVAL_P(c) + Z_STRLEN_P(c);	\
95 			if (e == p) {													\
96 				RETURN_FALSE;												\
97 			}																\
98 			while (p < e) {													\
99 				if (!iswhat((int)*(unsigned char *)(p++))) {				\
100 					RETURN_FALSE;											\
101 				}															\
102 			}																\
103 			RETURN_TRUE;													\
104 		}																	\
105 																			\
106 		ctype_fallback(c, return_value, iswhat, allow_digits, allow_minus);	\
107 	} while (0);
108 
109 /* {{{ Checks for alphanumeric character(s) */
PHP_FUNCTION(ctype_alnum)110 PHP_FUNCTION(ctype_alnum)
111 {
112 	ctype_impl(isalnum, 1, 0);
113 }
114 /* }}} */
115 
116 /* {{{ Checks for alphabetic character(s) */
PHP_FUNCTION(ctype_alpha)117 PHP_FUNCTION(ctype_alpha)
118 {
119 	ctype_impl(isalpha, 0, 0);
120 }
121 /* }}} */
122 
123 /* {{{ Checks for control character(s) */
PHP_FUNCTION(ctype_cntrl)124 PHP_FUNCTION(ctype_cntrl)
125 {
126 	ctype_impl(iscntrl, 0, 0);
127 }
128 /* }}} */
129 
130 /* {{{ Checks for numeric character(s) */
PHP_FUNCTION(ctype_digit)131 PHP_FUNCTION(ctype_digit)
132 {
133 	ctype_impl(isdigit, 1, 0);
134 }
135 /* }}} */
136 
137 /* {{{ Checks for lowercase character(s)  */
PHP_FUNCTION(ctype_lower)138 PHP_FUNCTION(ctype_lower)
139 {
140 	ctype_impl(islower, 0, 0);
141 }
142 /* }}} */
143 
144 /* {{{ Checks for any printable character(s) except space */
PHP_FUNCTION(ctype_graph)145 PHP_FUNCTION(ctype_graph)
146 {
147 	ctype_impl(isgraph, 1, 1);
148 }
149 /* }}} */
150 
151 /* {{{ Checks for printable character(s) */
PHP_FUNCTION(ctype_print)152 PHP_FUNCTION(ctype_print)
153 {
154 	ctype_impl(isprint, 1, 1);
155 }
156 /* }}} */
157 
158 /* {{{ Checks for any printable character which is not whitespace or an alphanumeric character */
PHP_FUNCTION(ctype_punct)159 PHP_FUNCTION(ctype_punct)
160 {
161 	ctype_impl(ispunct, 0, 0);
162 }
163 /* }}} */
164 
165 /* {{{ Checks for whitespace character(s)*/
PHP_FUNCTION(ctype_space)166 PHP_FUNCTION(ctype_space)
167 {
168 	ctype_impl(isspace, 0, 0);
169 }
170 /* }}} */
171 
172 /* {{{ Checks for uppercase character(s) */
PHP_FUNCTION(ctype_upper)173 PHP_FUNCTION(ctype_upper)
174 {
175 	ctype_impl(isupper, 0, 0);
176 }
177 /* }}} */
178 
179 /* {{{ Checks for character(s) representing a hexadecimal digit */
PHP_FUNCTION(ctype_xdigit)180 PHP_FUNCTION(ctype_xdigit)
181 {
182 	ctype_impl(isxdigit, 1, 0);
183 }
184 /* }}} */
185 
186 #endif	/* HAVE_CTYPE */
187