Use function for ctype implementation

Instead of having the whole logic inside a macro.
This commit is contained in:
Nikita Popov 2021-07-09 10:30:45 +02:00
parent ab353a50e9
commit dc80ea7e38

View file

@ -61,113 +61,114 @@ static PHP_MINFO_FUNCTION(ctype)
} }
/* }}} */ /* }}} */
/* {{{ ctype */ static void ctype_impl(
#define CTYPE(iswhat, allow_digits, allow_minus) \ INTERNAL_FUNCTION_PARAMETERS, int (*iswhat)(int), bool allow_digits, bool allow_minus) {
zval *c; \ zval *c;
ZEND_PARSE_PARAMETERS_START(1, 1); \
Z_PARAM_ZVAL(c) \
ZEND_PARSE_PARAMETERS_END(); \
if (Z_TYPE_P(c) == IS_LONG) { \
if (Z_LVAL_P(c) <= 255 && Z_LVAL_P(c) >= 0) { \
RETURN_BOOL(iswhat((int)Z_LVAL_P(c))); \
} else if (Z_LVAL_P(c) >= -128 && Z_LVAL_P(c) < 0) { \
RETURN_BOOL(iswhat((int)Z_LVAL_P(c) + 256)); \
} else if (Z_LVAL_P(c) >= 0) { \
RETURN_BOOL(allow_digits); \
} else { \
RETURN_BOOL(allow_minus); \
} \
} else if (Z_TYPE_P(c) == IS_STRING) { \
char *p = Z_STRVAL_P(c), *e = Z_STRVAL_P(c) + Z_STRLEN_P(c); \
if (e == p) { \
RETURN_FALSE; \
} \
while (p < e) { \
if(!iswhat((int)*(unsigned char *)(p++))) { \
RETURN_FALSE; \
} \
} \
RETURN_TRUE; \
} else { \
RETURN_FALSE; \
} \
/* }}} */ ZEND_PARSE_PARAMETERS_START(1, 1);
Z_PARAM_ZVAL(c)
ZEND_PARSE_PARAMETERS_END();
if (Z_TYPE_P(c) == IS_LONG) {
if (Z_LVAL_P(c) <= 255 && Z_LVAL_P(c) >= 0) {
RETURN_BOOL(iswhat((int)Z_LVAL_P(c)));
} else if (Z_LVAL_P(c) >= -128 && Z_LVAL_P(c) < 0) {
RETURN_BOOL(iswhat((int)Z_LVAL_P(c) + 256));
} else if (Z_LVAL_P(c) >= 0) {
RETURN_BOOL(allow_digits);
} else {
RETURN_BOOL(allow_minus);
}
} else if (Z_TYPE_P(c) == IS_STRING) {
char *p = Z_STRVAL_P(c), *e = Z_STRVAL_P(c) + Z_STRLEN_P(c);
if (e == p) {
RETURN_FALSE;
}
while (p < e) {
if(!iswhat((int)*(unsigned char *)(p++))) {
RETURN_FALSE;
}
}
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* {{{ Checks for alphanumeric character(s) */ /* {{{ Checks for alphanumeric character(s) */
PHP_FUNCTION(ctype_alnum) PHP_FUNCTION(ctype_alnum)
{ {
CTYPE(isalnum, 1, 0); ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isalnum, 1, 0);
} }
/* }}} */ /* }}} */
/* {{{ Checks for alphabetic character(s) */ /* {{{ Checks for alphabetic character(s) */
PHP_FUNCTION(ctype_alpha) PHP_FUNCTION(ctype_alpha)
{ {
CTYPE(isalpha, 0, 0); ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isalpha, 0, 0);
} }
/* }}} */ /* }}} */
/* {{{ Checks for control character(s) */ /* {{{ Checks for control character(s) */
PHP_FUNCTION(ctype_cntrl) PHP_FUNCTION(ctype_cntrl)
{ {
CTYPE(iscntrl, 0, 0); ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, iscntrl, 0, 0);
} }
/* }}} */ /* }}} */
/* {{{ Checks for numeric character(s) */ /* {{{ Checks for numeric character(s) */
PHP_FUNCTION(ctype_digit) PHP_FUNCTION(ctype_digit)
{ {
CTYPE(isdigit, 1, 0); ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isdigit, 1, 0);
} }
/* }}} */ /* }}} */
/* {{{ Checks for lowercase character(s) */ /* {{{ Checks for lowercase character(s) */
PHP_FUNCTION(ctype_lower) PHP_FUNCTION(ctype_lower)
{ {
CTYPE(islower, 0, 0); ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, islower, 0, 0);
} }
/* }}} */ /* }}} */
/* {{{ Checks for any printable character(s) except space */ /* {{{ Checks for any printable character(s) except space */
PHP_FUNCTION(ctype_graph) PHP_FUNCTION(ctype_graph)
{ {
CTYPE(isgraph, 1, 1); ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isgraph, 1, 1);
} }
/* }}} */ /* }}} */
/* {{{ Checks for printable character(s) */ /* {{{ Checks for printable character(s) */
PHP_FUNCTION(ctype_print) PHP_FUNCTION(ctype_print)
{ {
CTYPE(isprint, 1, 1); ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isprint, 1, 1);
} }
/* }}} */ /* }}} */
/* {{{ Checks for any printable character which is not whitespace or an alphanumeric character */ /* {{{ Checks for any printable character which is not whitespace or an alphanumeric character */
PHP_FUNCTION(ctype_punct) PHP_FUNCTION(ctype_punct)
{ {
CTYPE(ispunct, 0, 0); ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ispunct, 0, 0);
} }
/* }}} */ /* }}} */
/* {{{ Checks for whitespace character(s)*/ /* {{{ Checks for whitespace character(s)*/
PHP_FUNCTION(ctype_space) PHP_FUNCTION(ctype_space)
{ {
CTYPE(isspace, 0, 0); ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isspace, 0, 0);
} }
/* }}} */ /* }}} */
/* {{{ Checks for uppercase character(s) */ /* {{{ Checks for uppercase character(s) */
PHP_FUNCTION(ctype_upper) PHP_FUNCTION(ctype_upper)
{ {
CTYPE(isupper, 0, 0); ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isupper, 0, 0);
} }
/* }}} */ /* }}} */
/* {{{ Checks for character(s) representing a hexadecimal digit */ /* {{{ Checks for character(s) representing a hexadecimal digit */
PHP_FUNCTION(ctype_xdigit) PHP_FUNCTION(ctype_xdigit)
{ {
CTYPE(isxdigit, 1, 0); ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isxdigit, 1, 0);
} }
/* }}} */ /* }}} */