Skip to content
Browse files

Allow null as a default value for length in mb_substr() and mb_strcut()

  • Loading branch information...
1 parent 32a97df commit 352a1956b60059f9792cac840d57b184c7305667 @lstrojny lstrojny committed
Showing with 42 additions and 4 deletions.
  1. +12 −4 ext/mbstring/mbstring.c
  2. +30 −0 ext/mbstring/tests/mb_str_functions_opt-parameter.phpt
View
16 ext/mbstring/mbstring.c
@@ -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;
}
@@ -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 */
@@ -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;
}
@@ -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
View
30 ext/mbstring/tests/mb_str_functions_opt-parameter.phpt
@@ -0,0 +1,30 @@
+--TEST--
+Optional long parameter might be null
+--FILE--
+<?php
+echo mb_strpos('abb', 'b', null, 'UTF-8') . "\n";
+echo mb_strrpos('abb', 'b', null, 'UTF-8') . "\n";
+echo mb_stripos('abb', 'B', null, 'UTF-8') . "\n";
+echo mb_strripos('abb', 'B', null, 'UTF-8') . "\n";
+echo mb_strstr('foobarbaz', 'ba', null, 'UTF-8') . "\n";
+echo mb_strrchr('foobarbaz', 'ba', null, 'UTF-8') . "\n";
+echo mb_stristr('foobarbaz', 'BA', null, 'UTF-8') . "\n";
+echo mb_strrichr('foobarbaz', 'BA', null, 'UTF-8') . "\n";
+echo mb_substr('foobarbaz', 6, null, 'UTF-8') . "\n";
+echo mb_strcut('foobarbaz', 6, null, 'UTF-8') . "\n";
+echo mb_strimwidth('foobar', 0, 3, null, 'UTF-8') . "\n";
+?>
+==DONE==
+--EXPECT--
+1
+2
+1
+2
+barbaz
+baz
+barbaz
+baz
+baz
+baz
+foo
+==DONE==

0 comments on commit 352a195

Please sign in to comment.
Something went wrong with that request. Please try again.