Skip to content

Commit 133f610

Browse files
committed
Allow null as a default value for length in mb_substr() and mb_strcut()
1 parent 89948c7 commit 133f610

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ PHP NEWS
1414
. Bug #62987 (Assigning to ArrayObject[null][something] overrides all
1515
undefined variables). (Laruence)
1616

17+
- mbstring:
18+
. Allow passing null as a default value to mb_substr() and mb_strcut(). Patch
19+
by Alexander Moskaliov via GitHub PR #133. (Lars)
20+
1721
?? ??? 2012, PHP 5.4.7
1822

1923
- Core:

ext/mbstring/mbstring.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,9 +2715,10 @@ PHP_FUNCTION(mb_substr)
27152715
char *str, *encoding;
27162716
long from, len;
27172717
int mblen, str_len, encoding_len;
2718+
zval **z_len = NULL;
27182719
mbfl_string string, result, *ret;
27192720

2720-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|ls", &str, &str_len, &from, &len, &encoding, &encoding_len) == FAILURE) {
2721+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|Zs", &str, &str_len, &from, &z_len, &encoding, &encoding_len) == FAILURE) {
27212722
return;
27222723
}
27232724

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

2739-
if (argc < 3) {
2740+
if (argc < 3 || Z_TYPE_PP(z_len) == IS_NULL) {
27402741
len = str_len;
2742+
} else {
2743+
convert_to_long_ex(z_len);
2744+
len = Z_LVAL_PP(z_len);
27412745
}
27422746

27432747
/* measures length */
@@ -2788,13 +2792,14 @@ PHP_FUNCTION(mb_strcut)
27882792
char *encoding;
27892793
long from, len;
27902794
int encoding_len;
2795+
zval **z_len = NULL;
27912796
mbfl_string string, result, *ret;
27922797

27932798
mbfl_string_init(&string);
27942799
string.no_language = MBSTRG(language);
27952800
string.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
27962801

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

@@ -2806,8 +2811,11 @@ PHP_FUNCTION(mb_strcut)
28062811
}
28072812
}
28082813

2809-
if (argc < 3) {
2814+
if (argc < 3 || Z_TYPE_PP(z_len) == IS_NULL) {
28102815
len = string.len;
2816+
} else {
2817+
convert_to_long_ex(z_len);
2818+
len = Z_LVAL_PP(z_len);
28112819
}
28122820

28132821
/* if "from" position is negative, count start position from the end

ext/mbstring/tests/mb_str_functions_opt-parameter.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,3 @@ baz
2828
baz
2929
foo
3030
==DONE==
31-
--XFAIL--
32-
mb functions fail to allow null instead of actual value

0 commit comments

Comments
 (0)