Convert if blocks to assertions and adapt stubs accordingly

This commit is contained in:
George Peter Banyard 2020-04-08 21:05:59 +02:00
parent 73455778d4
commit 12ec7a2730
3 changed files with 68 additions and 91 deletions

View File

@ -1336,12 +1336,8 @@ PHP_FUNCTION(mb_internal_encoding)
RETURN_THROWS();
}
if (name == NULL) {
name = MBSTRG(current_internal_encoding) ? MBSTRG(current_internal_encoding)->name: NULL;
if (name != NULL) {
RETURN_STRING(name);
} else {
RETURN_FALSE;
}
ZEND_ASSERT(MBSTRG(current_internal_encoding));
RETURN_STRING(MBSTRG(current_internal_encoding)->name);
} else {
encoding = mbfl_name2encoding(name);
if (!encoding) {
@ -1350,6 +1346,7 @@ PHP_FUNCTION(mb_internal_encoding)
} else {
MBSTRG(current_internal_encoding) = encoding;
MBSTRG(internal_encoding_set) = 1;
/* TODO Return old encoding */
RETURN_TRUE;
}
}
@ -1426,6 +1423,7 @@ PHP_FUNCTION(mb_http_input)
}
}
if (!list) {
// TODO should return empty string?
RETURN_FALSE;
}
RETVAL_STRING(list);
@ -1433,11 +1431,13 @@ PHP_FUNCTION(mb_http_input)
retname = 0;
break;
default:
// TODO ValueError
result = MBSTRG(http_input_identify);
break;
}
}
// FIXME this bloc seems useless except for default switch case
if (retname) {
if (result) {
RETVAL_STRING(result->name);
@ -1461,12 +1461,8 @@ PHP_FUNCTION(mb_http_output)
}
if (name == NULL) {
name = MBSTRG(current_http_output_encoding) ? MBSTRG(current_http_output_encoding)->name: NULL;
if (name != NULL) {
RETURN_STRING(name);
} else {
RETURN_FALSE;
}
ZEND_ASSERT(MBSTRG(current_http_output_encoding));
RETURN_STRING(MBSTRG(current_http_output_encoding)->name);
} else {
encoding = mbfl_name2encoding(name);
if (!encoding) {
@ -1475,6 +1471,7 @@ PHP_FUNCTION(mb_http_output)
} else {
MBSTRG(http_output_set) = 1;
MBSTRG(current_http_output_encoding) = encoding;
/* TODO Return previous encoding? */
RETURN_TRUE;
}
}
@ -2153,24 +2150,19 @@ static void php_mb_strstr_variants(INTERNAL_FUNCTION_PARAMETERS, unsigned int va
if (!mbfl_is_error(n)) {
if (part) {
ret = mbfl_substr(&haystack, &result, 0, n);
if (ret != NULL) {
// TODO: avoid reallocation ???
RETVAL_STRINGL((char *)ret->val, ret->len);
efree(ret->val);
} else {
RETVAL_FALSE;
}
ZEND_ASSERT(ret != NULL);
// TODO: avoid reallocation ???
RETVAL_STRINGL((char *)ret->val, ret->len);
efree(ret->val);
} else {
ret = mbfl_substr(&haystack, &result, n, MBFL_SUBSTR_UNTIL_END);
if (ret != NULL) {
// TODO: avoid reallocation ???
RETVAL_STRINGL((char *)ret->val, ret->len);
efree(ret->val);
} else {
RETVAL_FALSE;
}
ZEND_ASSERT(ret != NULL);
// TODO: avoid reallocation ???
RETVAL_STRINGL((char *)ret->val, ret->len);
efree(ret->val);
}
} else {
// FIXME use handle_strpos_error(n)
RETVAL_FALSE;
}
}
@ -2301,9 +2293,7 @@ PHP_FUNCTION(mb_substr)
}
ret = mbfl_substr(&string, &result, real_from, real_len);
if (NULL == ret) {
RETURN_FALSE;
}
ZEND_ASSERT(ret != NULL);
// TODO: avoid reallocation ???
RETVAL_STRINGL((char *)ret->val, ret->len); /* the string is already strdup()'ed */
@ -2355,13 +2345,12 @@ PHP_FUNCTION(mb_strcut)
}
if (from > string.len) {
// TODO Out of bounds ValueError
RETURN_FALSE;
}
ret = mbfl_strcut(&string, &result, from, len);
if (ret == NULL) {
RETURN_FALSE;
}
ZEND_ASSERT(ret != NULL);
// TODO: avoid reallocation ???
RETVAL_STRINGL((char *)ret->val, ret->len); /* the string is already strdup()'ed */
@ -2388,11 +2377,8 @@ PHP_FUNCTION(mb_strwidth)
}
n = mbfl_strwidth(&string);
if (!mbfl_is_error(n)) {
RETVAL_LONG(n);
} else {
RETVAL_FALSE;
}
ZEND_ASSERT(n >= 0);
RETVAL_LONG(n);
}
/* }}} */
@ -2449,10 +2435,7 @@ PHP_FUNCTION(mb_strimwidth)
}
ret = mbfl_strimwidth(&string, &marker, &result, from, width);
if (ret == NULL) {
RETURN_FALSE;
}
ZEND_ASSERT(ret != NULL);
// TODO: avoid reallocation ???
RETVAL_STRINGL((char *)ret->val, ret->len); /* the string is already strdup()'ed */
efree(ret->val);
@ -2968,13 +2951,10 @@ PHP_FUNCTION(mb_encode_mimeheader)
mbfl_string_init(&result);
ret = mbfl_mime_header_encode(&string, &result, charset, transenc, linefeed, indent);
if (ret != NULL) {
// TODO: avoid reallocation ???
RETVAL_STRINGL((char *)ret->val, ret->len); /* the string is already strdup()'ed */
efree(ret->val);
} else {
RETVAL_FALSE;
}
ZEND_ASSERT(ret != NULL);
// TODO: avoid reallocation ???
RETVAL_STRINGL((char *)ret->val, ret->len); /* the string is already strdup()'ed */
efree(ret->val);
}
/* }}} */
@ -2993,13 +2973,10 @@ PHP_FUNCTION(mb_decode_mimeheader)
mbfl_string_init(&result);
ret = mbfl_mime_header_decode(&string, &result, MBSTRG(current_internal_encoding));
if (ret != NULL) {
// TODO: avoid reallocation ???
RETVAL_STRINGL((char *)ret->val, ret->len); /* the string is already strdup()'ed */
efree(ret->val);
} else {
RETVAL_FALSE;
}
ZEND_ASSERT(ret != NULL);
// TODO: avoid reallocation ???
RETVAL_STRINGL((char *)ret->val, ret->len); /* the string is already strdup()'ed */
efree(ret->val);
}
/* }}} */
@ -3090,13 +3067,10 @@ PHP_FUNCTION(mb_convert_kana)
}
ret = mbfl_ja_jp_hantozen(&string, &result, opt);
if (ret != NULL) {
// TODO: avoid reallocation ???
RETVAL_STRINGL((char *)ret->val, ret->len); /* the string is already strdup()'ed */
efree(ret->val);
} else {
RETVAL_FALSE;
}
ZEND_ASSERT(ret != NULL);
// TODO: avoid reallocation ???
RETVAL_STRINGL((char *)ret->val, ret->len); /* the string is already strdup()'ed */
efree(ret->val);
}
/* }}} */
@ -3372,13 +3346,10 @@ php_mb_numericentity_exec(INTERNAL_FUNCTION_PARAMETERS, int type)
mapsize /= 4;
ret = mbfl_html_numeric_entity(&string, &result, convmap, mapsize, type);
if (ret != NULL) {
// TODO: avoid reallocation ???
RETVAL_STRINGL((char *)ret->val, ret->len);
efree(ret->val);
} else {
RETVAL_FALSE;
}
ZEND_ASSERT(ret != NULL);
// TODO: avoid reallocation ???
RETVAL_STRINGL((char *)ret->val, ret->len);
efree(ret->val);
efree((void *)convmap);
}
/* }}} */
@ -4016,6 +3987,7 @@ PHP_FUNCTION(mb_get_info)
RETVAL_STRING("Off");
}
} else {
// TODO Convert to ValueError
RETURN_FALSE;
}
}

View File

@ -43,13 +43,13 @@ function mb_strrichr(string $haystack, string $needle, bool $part = false, strin
function mb_substr_count(string $haystack, string $needle, string $encoding = UNKNOWN): int {}
function mb_substr(string $str, int $start, ?int $length = null, string $encoding = UNKNOWN): string|false {}
function mb_substr(string $str, int $start, ?int $length = null, string $encoding = UNKNOWN): string {}
function mb_strcut(string $str, int $start, ?int $length = null, string $encoding = UNKNOWN): string|false {}
function mb_strwidth(string $str, string $encoding = UNKNOWN): int|false {}
function mb_strwidth(string $str, string $encoding = UNKNOWN): int {}
function mb_strimwidth(string $str, int $start, int $width, string $trimmarker = UNKNOWN, string $encoding = UNKNOWN): string|false {}
function mb_strimwidth(string $str, int $start, int $width, string $trim_marker = UNKNOWN, string $encoding = UNKNOWN): string {}
function mb_convert_encoding(array|string $str, string $to, array|string $from = UNKNOWN): array|string|false {}
@ -63,19 +63,19 @@ function mb_detect_encoding(string $str, array|string|null $encoding_list = null
function mb_list_encodings(): array {}
function mb_encoding_aliases(string $encoding): array|false {}
function mb_encoding_aliases(string $encoding): array {}
function mb_encode_mimeheader(string $str, string $charset = UNKNOWN, string $transfer = UNKNOWN, string $linefeed = UNKNOWN, int $indent = 0): string|false {}
function mb_encode_mimeheader(string $str, string $charset = UNKNOWN, string $transfer = UNKNOWN, string $linefeed = UNKNOWN, int $indent = 0): string {}
function mb_decode_mimeheader(string $string): string|false {}
function mb_decode_mimeheader(string $string): string {}
function mb_convert_kana(string $str, string $option = UNKNOWN, string $encoding = UNKNOWN): string|false {}
function mb_convert_kana(string $str, string $option = UNKNOWN, string $encoding = UNKNOWN): string {}
function mb_convert_variables(string $to, array|string $from, &$var, &...$vars): string|false {}
function mb_encode_numericentity(string $string, array $convmap, string $encoding = UNKNOWN, bool $is_hex = false): string|false {}
function mb_encode_numericentity(string $string, array $convmap, string $encoding = UNKNOWN, bool $is_hex = false): string {}
function mb_decode_numericentity(string $string, array $convmap, string $encoding = UNKNOWN): string|false {}
function mb_decode_numericentity(string $string, array $convmap, string $encoding = UNKNOWN): string {}
function mb_send_mail(string $to, string $subject, string $message, $additional_headers = UNKNOWN, string $additional_parameters = UNKNOWN): bool {}

View File

@ -79,25 +79,27 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mb_substr_count, 0, 2, IS_LONG,
ZEND_ARG_TYPE_INFO(0, encoding, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_substr, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mb_substr, 0, 2, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, start, IS_LONG, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, length, IS_LONG, 1, "null")
ZEND_ARG_TYPE_INFO(0, encoding, IS_STRING, 0)
ZEND_END_ARG_INFO()
#define arginfo_mb_strcut arginfo_mb_substr
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_strwidth, 0, 1, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_strcut, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, start, IS_LONG, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, length, IS_LONG, 1, "null")
ZEND_ARG_TYPE_INFO(0, encoding, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_strimwidth, 0, 3, MAY_BE_STRING|MAY_BE_FALSE)
#define arginfo_mb_strwidth arginfo_mb_strlen
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mb_strimwidth, 0, 3, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, start, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, width, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, trimmarker, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, trim_marker, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, encoding, IS_STRING, 0)
ZEND_END_ARG_INFO()
@ -129,11 +131,11 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mb_list_encodings, 0, 0, IS_ARRAY, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_encoding_aliases, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mb_encoding_aliases, 0, 1, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, encoding, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_encode_mimeheader, 0, 1, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mb_encode_mimeheader, 0, 1, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, charset, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, transfer, IS_STRING, 0)
@ -141,11 +143,11 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_encode_mimeheader, 0, 1, MAY_
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, indent, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_decode_mimeheader, 0, 1, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mb_decode_mimeheader, 0, 1, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, string, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_convert_kana, 0, 1, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mb_convert_kana, 0, 1, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, option, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, encoding, IS_STRING, 0)
@ -158,14 +160,14 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_convert_variables, 0, 3, MAY_
ZEND_ARG_VARIADIC_INFO(1, vars)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_encode_numericentity, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mb_encode_numericentity, 0, 2, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, string, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, convmap, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, encoding, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, is_hex, _IS_BOOL, 0, "false")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_decode_numericentity, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mb_decode_numericentity, 0, 2, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, string, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, convmap, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, encoding, IS_STRING, 0)
@ -193,7 +195,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mb_scrub, 0, 1, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, encoding, IS_STRING, 0)
ZEND_END_ARG_INFO()
#define arginfo_mb_ord arginfo_mb_strwidth
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_ord, 0, 1, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, encoding, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_chr, 0, 1, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, cp, IS_LONG, 0)