Skip to content

Commit

Permalink
Allow null as a default value for length in mb_substr() and mb_strcut()
Browse files Browse the repository at this point in the history
  • Loading branch information
lstrojny committed Sep 2, 2012
1 parent 89948c7 commit 133f610
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
4 changes: 4 additions & 0 deletions NEWS
Expand Up @@ -14,6 +14,10 @@ PHP NEWS
. Bug #62987 (Assigning to ArrayObject[null][something] overrides all
undefined variables). (Laruence)

- mbstring:
. Allow passing null as a default value to mb_substr() and mb_strcut(). Patch
by Alexander Moskaliov via GitHub PR #133. (Lars)

?? ??? 2012, PHP 5.4.7

- Core:
Expand Down
16 changes: 12 additions & 4 deletions ext/mbstring/mbstring.c
Expand Up @@ -2715,9 +2715,10 @@ PHP_FUNCTION(mb_substr)
char *str, *encoding;
long from, len;
int mblen, str_len, encoding_len;
zval **z_len = NULL;
mbfl_string string, result, *ret;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|ls", &str, &str_len, &from, &len, &encoding, &encoding_len) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|Zs", &str, &str_len, &from, &z_len, &encoding, &encoding_len) == FAILURE) {
return;
}

Expand All @@ -2736,8 +2737,11 @@ PHP_FUNCTION(mb_substr)
string.val = (unsigned char *)str;
string.len = str_len;

if (argc < 3) {
if (argc < 3 || Z_TYPE_PP(z_len) == IS_NULL) {
len = str_len;
} else {
convert_to_long_ex(z_len);
len = Z_LVAL_PP(z_len);
}

/* measures length */
Expand Down Expand Up @@ -2788,13 +2792,14 @@ PHP_FUNCTION(mb_strcut)
char *encoding;
long from, len;
int encoding_len;
zval **z_len = NULL;
mbfl_string string, result, *ret;

mbfl_string_init(&string);
string.no_language = MBSTRG(language);
string.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|ls", (char **)&string.val, (int **)&string.len, &from, &len, &encoding, &encoding_len) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|Zs", (char **)&string.val, (int **)&string.len, &from, &z_len, &encoding, &encoding_len) == FAILURE) {
return;
}

Expand All @@ -2806,8 +2811,11 @@ PHP_FUNCTION(mb_strcut)
}
}

if (argc < 3) {
if (argc < 3 || Z_TYPE_PP(z_len) == IS_NULL) {
len = string.len;
} else {
convert_to_long_ex(z_len);
len = Z_LVAL_PP(z_len);
}

/* if "from" position is negative, count start position from the end
Expand Down
2 changes: 0 additions & 2 deletions ext/mbstring/tests/mb_str_functions_opt-parameter.phpt
Expand Up @@ -28,5 +28,3 @@ baz
baz
foo
==DONE==
--XFAIL--
mb functions fail to allow null instead of actual value

0 comments on commit 133f610

Please sign in to comment.