xref: /PHP-8.0/ext/ctype/ctype.c (revision 2b5de6f8)
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    | http://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 /* {{{ ctype */
65 #define CTYPE(iswhat, allow_digits, allow_minus) \
66 	zval *c; \
67 	ZEND_PARSE_PARAMETERS_START(1, 1); \
68 		Z_PARAM_ZVAL(c) \
69 	ZEND_PARSE_PARAMETERS_END(); \
70 	if (Z_TYPE_P(c) == IS_LONG) { \
71 		if (Z_LVAL_P(c) <= 255 && Z_LVAL_P(c) >= 0) { \
72 			RETURN_BOOL(iswhat((int)Z_LVAL_P(c))); \
73 		} else if (Z_LVAL_P(c) >= -128 && Z_LVAL_P(c) < 0) { \
74 			RETURN_BOOL(iswhat((int)Z_LVAL_P(c) + 256)); \
75 		} else if (Z_LVAL_P(c) >= 0) { \
76 			RETURN_BOOL(allow_digits); \
77 		} else { \
78 			RETURN_BOOL(allow_minus); \
79 		} \
80 	} else if (Z_TYPE_P(c) == IS_STRING) { \
81 		char *p = Z_STRVAL_P(c), *e = Z_STRVAL_P(c) + Z_STRLEN_P(c); \
82 		if (e == p) {	\
83 			RETURN_FALSE;	\
84 		}	\
85 		while (p < e) { \
86 			if(!iswhat((int)*(unsigned char *)(p++))) { \
87 				RETURN_FALSE; \
88 			} \
89 		} \
90 		RETURN_TRUE; \
91 	} else { \
92 		RETURN_FALSE; \
93 	} \
94 
95 /* }}} */
96 
97 /* {{{ Checks for alphanumeric character(s) */
PHP_FUNCTION(ctype_alnum)98 PHP_FUNCTION(ctype_alnum)
99 {
100 	CTYPE(isalnum, 1, 0);
101 }
102 /* }}} */
103 
104 /* {{{ Checks for alphabetic character(s) */
PHP_FUNCTION(ctype_alpha)105 PHP_FUNCTION(ctype_alpha)
106 {
107 	CTYPE(isalpha, 0, 0);
108 }
109 /* }}} */
110 
111 /* {{{ Checks for control character(s) */
PHP_FUNCTION(ctype_cntrl)112 PHP_FUNCTION(ctype_cntrl)
113 {
114 	CTYPE(iscntrl, 0, 0);
115 }
116 /* }}} */
117 
118 /* {{{ Checks for numeric character(s) */
PHP_FUNCTION(ctype_digit)119 PHP_FUNCTION(ctype_digit)
120 {
121 	CTYPE(isdigit, 1, 0);
122 }
123 /* }}} */
124 
125 /* {{{ Checks for lowercase character(s)  */
PHP_FUNCTION(ctype_lower)126 PHP_FUNCTION(ctype_lower)
127 {
128 	CTYPE(islower, 0, 0);
129 }
130 /* }}} */
131 
132 /* {{{ Checks for any printable character(s) except space */
PHP_FUNCTION(ctype_graph)133 PHP_FUNCTION(ctype_graph)
134 {
135 	CTYPE(isgraph, 1, 1);
136 }
137 /* }}} */
138 
139 /* {{{ Checks for printable character(s) */
PHP_FUNCTION(ctype_print)140 PHP_FUNCTION(ctype_print)
141 {
142 	CTYPE(isprint, 1, 1);
143 }
144 /* }}} */
145 
146 /* {{{ Checks for any printable character which is not whitespace or an alphanumeric character */
PHP_FUNCTION(ctype_punct)147 PHP_FUNCTION(ctype_punct)
148 {
149 	CTYPE(ispunct, 0, 0);
150 }
151 /* }}} */
152 
153 /* {{{ Checks for whitespace character(s)*/
PHP_FUNCTION(ctype_space)154 PHP_FUNCTION(ctype_space)
155 {
156 	CTYPE(isspace, 0, 0);
157 }
158 /* }}} */
159 
160 /* {{{ Checks for uppercase character(s) */
PHP_FUNCTION(ctype_upper)161 PHP_FUNCTION(ctype_upper)
162 {
163 	CTYPE(isupper, 0, 0);
164 }
165 /* }}} */
166 
167 /* {{{ Checks for character(s) representing a hexadecimal digit */
PHP_FUNCTION(ctype_xdigit)168 PHP_FUNCTION(ctype_xdigit)
169 {
170 	CTYPE(isxdigit, 1, 0);
171 }
172 /* }}} */
173 
174 #endif	/* HAVE_CTYPE */
175