xref: /php-src/ext/ctype/ctype.c (revision 11accb5c)
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_ctype.h"
23 #include "ctype_arginfo.h"
24 #include "ext/standard/info.h"
25 
26 #include <ctype.h>
27 
28 #ifdef HAVE_CTYPE
29 
30 static PHP_MINFO_FUNCTION(ctype);
31 
32 /* }}} */
33 
34 /* {{{ ctype_module_entry */
35 zend_module_entry ctype_module_entry = {
36 	STANDARD_MODULE_HEADER,
37 	"ctype",
38 	ext_functions,
39 	NULL,
40 	NULL,
41 	NULL,
42 	NULL,
43 	PHP_MINFO(ctype),
44 	PHP_CTYPE_VERSION,
45 	STANDARD_MODULE_PROPERTIES
46 };
47 /* }}} */
48 
49 #ifdef COMPILE_DL_CTYPE
50 ZEND_GET_MODULE(ctype)
51 #endif
52 
53 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(ctype)54 static PHP_MINFO_FUNCTION(ctype)
55 {
56 	php_info_print_table_start();
57 	php_info_print_table_row(2, "ctype functions", "enabled");
58 	php_info_print_table_end();
59 }
60 /* }}} */
61 
62 /* 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)63 static zend_never_inline void ctype_fallback(zval *c, zval *return_value, int (*iswhat)(int), bool allow_digits, bool allow_minus)
64 {
65 	php_error_docref(NULL, E_DEPRECATED,
66 		"Argument of type %s will be interpreted as string in the future", zend_zval_type_name(c));
67 	if (Z_TYPE_P(c) == IS_LONG) {
68 		if (Z_LVAL_P(c) <= 255 && Z_LVAL_P(c) >= 0) {
69 			RETURN_BOOL(iswhat((int)Z_LVAL_P(c)));
70 		} else if (Z_LVAL_P(c) >= -128 && Z_LVAL_P(c) < 0) {
71 			RETURN_BOOL(iswhat((int)Z_LVAL_P(c) + 256));
72 		} else if (Z_LVAL_P(c) >= 0) {
73 			RETURN_BOOL(allow_digits);
74 		} else {
75 			RETURN_BOOL(allow_minus);
76 		}
77 	} else {
78 		RETURN_FALSE;
79 	}
80 }
81 
82 /* Define as a macro such that iswhat can use the macro version instead of the function version.
83  * This heavily reduces the overhead. (GH-11997) */
84 #define ctype_impl(iswhat, allow_digits, allow_minus) do { 					\
85 		zval *c;															\
86 																			\
87 		ZEND_PARSE_PARAMETERS_START(1, 1);									\
88 			Z_PARAM_ZVAL(c)													\
89 		ZEND_PARSE_PARAMETERS_END();										\
90 																			\
91 		if (Z_TYPE_P(c) == IS_STRING) {										\
92 			char *p = Z_STRVAL_P(c), *e = Z_STRVAL_P(c) + Z_STRLEN_P(c);	\
93 			if (e == p) {													\
94 				RETURN_FALSE;												\
95 			}																\
96 			while (p < e) {													\
97 				if (!iswhat((int)*(unsigned char *)(p++))) {				\
98 					RETURN_FALSE;											\
99 				}															\
100 			}																\
101 			RETURN_TRUE;													\
102 		}																	\
103 																			\
104 		ctype_fallback(c, return_value, iswhat, allow_digits, allow_minus);	\
105 	} while (0);
106 
107 /* {{{ Checks for alphanumeric character(s) */
PHP_FUNCTION(ctype_alnum)108 PHP_FUNCTION(ctype_alnum)
109 {
110 	ctype_impl(isalnum, 1, 0);
111 }
112 /* }}} */
113 
114 /* {{{ Checks for alphabetic character(s) */
PHP_FUNCTION(ctype_alpha)115 PHP_FUNCTION(ctype_alpha)
116 {
117 	ctype_impl(isalpha, 0, 0);
118 }
119 /* }}} */
120 
121 /* {{{ Checks for control character(s) */
PHP_FUNCTION(ctype_cntrl)122 PHP_FUNCTION(ctype_cntrl)
123 {
124 	ctype_impl(iscntrl, 0, 0);
125 }
126 /* }}} */
127 
128 /* {{{ Checks for numeric character(s) */
PHP_FUNCTION(ctype_digit)129 PHP_FUNCTION(ctype_digit)
130 {
131 	ctype_impl(isdigit, 1, 0);
132 }
133 /* }}} */
134 
135 /* {{{ Checks for lowercase character(s)  */
PHP_FUNCTION(ctype_lower)136 PHP_FUNCTION(ctype_lower)
137 {
138 	ctype_impl(islower, 0, 0);
139 }
140 /* }}} */
141 
142 /* {{{ Checks for any printable character(s) except space */
PHP_FUNCTION(ctype_graph)143 PHP_FUNCTION(ctype_graph)
144 {
145 	ctype_impl(isgraph, 1, 1);
146 }
147 /* }}} */
148 
149 /* {{{ Checks for printable character(s) */
PHP_FUNCTION(ctype_print)150 PHP_FUNCTION(ctype_print)
151 {
152 	ctype_impl(isprint, 1, 1);
153 }
154 /* }}} */
155 
156 /* {{{ Checks for any printable character which is not whitespace or an alphanumeric character */
PHP_FUNCTION(ctype_punct)157 PHP_FUNCTION(ctype_punct)
158 {
159 	ctype_impl(ispunct, 0, 0);
160 }
161 /* }}} */
162 
163 /* {{{ Checks for whitespace character(s)*/
PHP_FUNCTION(ctype_space)164 PHP_FUNCTION(ctype_space)
165 {
166 	ctype_impl(isspace, 0, 0);
167 }
168 /* }}} */
169 
170 /* {{{ Checks for uppercase character(s) */
PHP_FUNCTION(ctype_upper)171 PHP_FUNCTION(ctype_upper)
172 {
173 	ctype_impl(isupper, 0, 0);
174 }
175 /* }}} */
176 
177 /* {{{ Checks for character(s) representing a hexadecimal digit */
PHP_FUNCTION(ctype_xdigit)178 PHP_FUNCTION(ctype_xdigit)
179 {
180 	ctype_impl(isxdigit, 1, 0);
181 }
182 /* }}} */
183 
184 #endif	/* HAVE_CTYPE */
185