Skip to content

Commit dc80ea7

Browse files
committed
Use function for ctype implementation
Instead of having the whole logic inside a macro.
1 parent ab353a5 commit dc80ea7

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

ext/ctype/ctype.c

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -61,113 +61,114 @@ static PHP_MINFO_FUNCTION(ctype)
6161
}
6262
/* }}} */
6363

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-
/* }}} */
64+
static void ctype_impl(
65+
INTERNAL_FUNCTION_PARAMETERS, int (*iswhat)(int), bool allow_digits, bool allow_minus) {
66+
zval *c;
67+
68+
ZEND_PARSE_PARAMETERS_START(1, 1);
69+
Z_PARAM_ZVAL(c)
70+
ZEND_PARSE_PARAMETERS_END();
71+
72+
if (Z_TYPE_P(c) == IS_LONG) {
73+
if (Z_LVAL_P(c) <= 255 && Z_LVAL_P(c) >= 0) {
74+
RETURN_BOOL(iswhat((int)Z_LVAL_P(c)));
75+
} else if (Z_LVAL_P(c) >= -128 && Z_LVAL_P(c) < 0) {
76+
RETURN_BOOL(iswhat((int)Z_LVAL_P(c) + 256));
77+
} else if (Z_LVAL_P(c) >= 0) {
78+
RETURN_BOOL(allow_digits);
79+
} else {
80+
RETURN_BOOL(allow_minus);
81+
}
82+
} else if (Z_TYPE_P(c) == IS_STRING) {
83+
char *p = Z_STRVAL_P(c), *e = Z_STRVAL_P(c) + Z_STRLEN_P(c);
84+
if (e == p) {
85+
RETURN_FALSE;
86+
}
87+
while (p < e) {
88+
if(!iswhat((int)*(unsigned char *)(p++))) {
89+
RETURN_FALSE;
90+
}
91+
}
92+
RETURN_TRUE;
93+
} else {
94+
RETURN_FALSE;
95+
}
96+
}
9697

9798
/* {{{ Checks for alphanumeric character(s) */
9899
PHP_FUNCTION(ctype_alnum)
99100
{
100-
CTYPE(isalnum, 1, 0);
101+
ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isalnum, 1, 0);
101102
}
102103
/* }}} */
103104

104105
/* {{{ Checks for alphabetic character(s) */
105106
PHP_FUNCTION(ctype_alpha)
106107
{
107-
CTYPE(isalpha, 0, 0);
108+
ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isalpha, 0, 0);
108109
}
109110
/* }}} */
110111

111112
/* {{{ Checks for control character(s) */
112113
PHP_FUNCTION(ctype_cntrl)
113114
{
114-
CTYPE(iscntrl, 0, 0);
115+
ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, iscntrl, 0, 0);
115116
}
116117
/* }}} */
117118

118119
/* {{{ Checks for numeric character(s) */
119120
PHP_FUNCTION(ctype_digit)
120121
{
121-
CTYPE(isdigit, 1, 0);
122+
ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isdigit, 1, 0);
122123
}
123124
/* }}} */
124125

125126
/* {{{ Checks for lowercase character(s) */
126127
PHP_FUNCTION(ctype_lower)
127128
{
128-
CTYPE(islower, 0, 0);
129+
ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, islower, 0, 0);
129130
}
130131
/* }}} */
131132

132133
/* {{{ Checks for any printable character(s) except space */
133134
PHP_FUNCTION(ctype_graph)
134135
{
135-
CTYPE(isgraph, 1, 1);
136+
ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isgraph, 1, 1);
136137
}
137138
/* }}} */
138139

139140
/* {{{ Checks for printable character(s) */
140141
PHP_FUNCTION(ctype_print)
141142
{
142-
CTYPE(isprint, 1, 1);
143+
ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isprint, 1, 1);
143144
}
144145
/* }}} */
145146

146147
/* {{{ Checks for any printable character which is not whitespace or an alphanumeric character */
147148
PHP_FUNCTION(ctype_punct)
148149
{
149-
CTYPE(ispunct, 0, 0);
150+
ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ispunct, 0, 0);
150151
}
151152
/* }}} */
152153

153154
/* {{{ Checks for whitespace character(s)*/
154155
PHP_FUNCTION(ctype_space)
155156
{
156-
CTYPE(isspace, 0, 0);
157+
ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isspace, 0, 0);
157158
}
158159
/* }}} */
159160

160161
/* {{{ Checks for uppercase character(s) */
161162
PHP_FUNCTION(ctype_upper)
162163
{
163-
CTYPE(isupper, 0, 0);
164+
ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isupper, 0, 0);
164165
}
165166
/* }}} */
166167

167168
/* {{{ Checks for character(s) representing a hexadecimal digit */
168169
PHP_FUNCTION(ctype_xdigit)
169170
{
170-
CTYPE(isxdigit, 1, 0);
171+
ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isxdigit, 1, 0);
171172
}
172173
/* }}} */
173174

0 commit comments

Comments
 (0)