}
size_t
-mbfl_strlen(mbfl_string *string)
+mbfl_strlen(const mbfl_string *string)
{
size_t len, n, k;
unsigned char *p;
needle_u8 = needle;
}
- if (needle_u8->len < 1) {
- result = (size_t) -8;
+ result = (size_t) -1;
+ if (haystack_u8->len < needle_u8->len) {
goto out;
}
- result = (size_t) -1;
- if (haystack_u8->len < needle_u8->len) {
+ if (needle_u8->len == 0) {
+ size_t haystack_length = mbfl_strlen(haystack_u8);
+ /* Check if offset is out of bound */
+ if (
+ (offset > 0 && offset > haystack_length)
+ || (offset < 0 && -offset > haystack_length)
+ ) {
+ result = -16;
+ goto out;
+ }
+
+ if (offset < 0) {
+ result = haystack_length + offset;
+ } else if (reverse) {
+ result = haystack_length;
+ } else {
+ result = (size_t) offset;
+ }
goto out;
}
* strlen
*/
MBFLAPI extern size_t
-mbfl_strlen(mbfl_string *string);
+mbfl_strlen(const mbfl_string *string);
/*
* oddlen
}
}
- if (needle.len == 0) {
- php_error_docref(NULL, E_WARNING, "Empty delimiter");
- RETURN_FALSE;
- }
-
n = mbfl_strpos(&haystack, &needle, offset, reverse);
if (!mbfl_is_error(n)) {
RETVAL_LONG(n);
RETURN_THROWS();
}
- if (needle.len == 0) {
- php_error_docref(NULL, E_WARNING, "Empty delimiter");
- RETURN_FALSE;
- }
-
n = php_mb_stripos(0, (char *)haystack.val, haystack.len, (char *)needle.val, needle.len, offset, from_encoding);
if (!mbfl_is_error(n)) {
RETURN_FALSE;
}
- if (needle.len == 0) {
- php_error_docref(NULL, E_WARNING, "Empty delimiter");
- RETURN_FALSE;
- }
-
n = mbfl_strpos(&haystack, &needle, 0, 0);
if (!mbfl_is_error(n)) {
if (part) {
RETURN_FALSE;
}
- if (!needle.len) {
- php_error_docref(NULL, E_WARNING, "Empty delimiter");
- RETURN_FALSE;
- }
-
n = php_mb_stripos(0, (char *)haystack.val, haystack.len, (char *)needle.val, needle.len, 0, from_encoding);
if (mbfl_is_error(n)) {
RETURN_FALSE;
break;
}
- if (needle.len == 0) {
- break;
- }
-
if (offset != 0) {
size_t haystack_char_len = mbfl_strlen(&haystack);
--- /dev/null
+--TEST--
+Test mb_stripos() function : with empty needle
+--SKIPIF--
+<?php
+extension_loaded('mbstring') or die('skip');
+function_exists('mb_stripos') or die("skip mb_stripos() is not available in this build");
+?>
+--FILE--
+<?php
+
+mb_internal_encoding('UTF-8');
+
+$string_ascii = 'abc def';
+// Japanese string in UTF-8
+$string_mb = "日本語テキストです。0123456789。";
+
+echo "\n-- ASCII string without offset --\n";
+var_dump(mb_stripos($string_ascii, ''));
+
+echo "\n-- ASCII string with in range positive offset --\n";
+var_dump(mb_stripos($string_ascii, '', 2));
+
+echo "\n-- ASCII string with in range negative offset --\n";
+var_dump(mb_stripos($string_ascii, '', -2));
+
+echo "\n-- ASCII string with out of bound positive offset --\n";
+var_dump(mb_stripos($string_ascii, '', 150));
+
+echo "\n-- ASCII string with out of bound negative offset --\n";
+var_dump(mb_stripos($string_ascii, '', -150));
+
+
+echo "\n-- Multi-byte string without offset --\n";
+var_dump(mb_stripos($string_mb, ''));
+
+echo "\n-- Multi-byte string with in range positive offset --\n";
+var_dump(mb_stripos($string_mb, '', 2));
+
+echo "\n-- Multi-byte string with in range negative offset --\n";
+var_dump(mb_stripos($string_mb, '', -2));
+
+echo "\n-- Multi-byte string with out of bound positive offset --\n";
+var_dump(mb_stripos($string_mb, '', 150));
+
+echo "\n-- Multi-byte string with out of bound negative offset --\n";
+var_dump(mb_stripos($string_mb, '', -150));
+
+?>
+--EXPECTF--
+-- ASCII string without offset --
+int(0)
+
+-- ASCII string with in range positive offset --
+int(2)
+
+-- ASCII string with in range negative offset --
+int(5)
+
+-- ASCII string with out of bound positive offset --
+
+Warning: mb_stripos(): Offset not contained in string in %s on line %d
+bool(false)
+
+-- ASCII string with out of bound negative offset --
+
+Warning: mb_stripos(): Offset not contained in string in %s on line %d
+bool(false)
+
+-- Multi-byte string without offset --
+int(0)
+
+-- Multi-byte string with in range positive offset --
+int(2)
+
+-- Multi-byte string with in range negative offset --
+int(19)
+
+-- Multi-byte string with out of bound positive offset --
+
+Warning: mb_stripos(): Offset not contained in string in %s on line %d
+bool(false)
+
+-- Multi-byte string with out of bound negative offset --
+
+Warning: mb_stripos(): Offset not contained in string in %s on line %d
+bool(false)
--- /dev/null
+--TEST--
+Test mb_stristr() function : with empty needle
+--SKIPIF--
+<?php
+extension_loaded('mbstring') or die('skip');
+function_exists('mb_stristr') or die("skip mb_stristr() is not available in this build");
+?>
+--FILE--
+<?php
+
+mb_internal_encoding('UTF-8');
+
+$string_ascii = 'abc def';
+// Japanese string in UTF-8
+$string_mb = "日本語テキストです。0123456789。";
+
+echo "\n-- ASCII string --\n";
+var_dump(mb_stristr($string_ascii, '', false, 'ISO-8859-1'));
+var_dump(mb_stristr($string_ascii, ''));
+var_dump(mb_stristr($string_ascii, '', true));
+
+echo "\n-- Multibyte string --\n";
+var_dump(mb_stristr($string_mb, ''));
+var_dump(mb_stristr($string_mb, '', false, 'utf-8'));
+var_dump(mb_stristr($string_mb, '', true));
+
+?>
+--EXPECT--
+-- ASCII string --
+string(7) "abc def"
+string(7) "abc def"
+string(0) ""
+
+-- Multibyte string --
+string(53) "日本語テキストです。0123456789。"
+string(53) "日本語テキストです。0123456789。"
+string(0) ""
--- /dev/null
+--TEST--
+Test mb_strpos() function : with empty needle
+--SKIPIF--
+<?php
+extension_loaded('mbstring') or die('skip');
+function_exists('mb_strpos') or die("skip mb_strpos() is not available in this build");
+?>
+--FILE--
+<?php
+
+mb_internal_encoding('UTF-8');
+
+$string_ascii = 'abc def';
+// Japanese string in UTF-8
+$string_mb = "日本語テキストです。0123456789。";
+
+echo "\n-- ASCII string without offset --\n";
+var_dump(mb_strpos($string_ascii, ''));
+
+echo "\n-- ASCII string with in range positive offset --\n";
+var_dump(mb_strpos($string_ascii, '', 2));
+
+echo "\n-- ASCII string with in range negative offset --\n";
+var_dump(mb_strpos($string_ascii, '', -2));
+
+echo "\n-- ASCII string with out of bound positive offset --\n";
+var_dump(mb_strpos($string_ascii, '', 15));
+
+echo "\n-- ASCII string with out of bound negative offset --\n";
+var_dump(mb_strpos($string_ascii, '', -15));
+
+
+echo "\n-- Multi-byte string without offset --\n";
+var_dump(mb_strpos($string_mb, ''));
+
+echo "\n-- Multi-byte string with in range positive offset --\n";
+var_dump(mb_strpos($string_mb, '', 2));
+
+echo "\n-- Multi-byte string with in range negative offset --\n";
+var_dump(mb_strpos($string_mb, '', -2));
+
+echo "\n-- Multi-byte string with out of bound positive offset --\n";
+var_dump(mb_strpos($string_mb, '', 150));
+
+echo "\n-- Multi-byte string with out of bound negative offset --\n";
+var_dump(mb_strpos($string_mb, '', -150));
+
+?>
+--EXPECTF--
+-- ASCII string without offset --
+int(0)
+
+-- ASCII string with in range positive offset --
+int(2)
+
+-- ASCII string with in range negative offset --
+int(5)
+
+-- ASCII string with out of bound positive offset --
+
+Warning: mb_strpos(): Offset not contained in string in %s on line %d
+bool(false)
+
+-- ASCII string with out of bound negative offset --
+
+Warning: mb_strpos(): Offset not contained in string in %s on line %d
+bool(false)
+
+-- Multi-byte string without offset --
+int(0)
+
+-- Multi-byte string with in range positive offset --
+int(2)
+
+-- Multi-byte string with in range negative offset --
+int(19)
+
+-- Multi-byte string with out of bound positive offset --
+
+Warning: mb_strpos(): Offset not contained in string in %s on line %d
+bool(false)
+
+-- Multi-byte string with out of bound negative offset --
+
+Warning: mb_strpos(): Offset not contained in string in %s on line %d
+bool(false)
--- /dev/null
+--TEST--
+Test mb_strrchr() function : with empty needle
+--SKIPIF--
+<?php
+extension_loaded('mbstring') or die('skip');
+function_exists('mb_strrchr') or die("skip mb_strrchr() is not available in this build");
+?>
+--FILE--
+<?php
+
+mb_internal_encoding('UTF-8');
+
+$string_ascii = 'abc def';
+// Japanese string in UTF-8
+$string_mb = "日本語テキストです。0123456789。";
+
+echo "\n-- ASCII string --\n";
+var_dump(bin2hex(mb_strrchr($string_ascii, '', false, 'ISO-8859-1')));
+var_dump(bin2hex(mb_strrchr($string_ascii, '')));
+var_dump(bin2hex(mb_strrchr($string_ascii, '', true)));
+
+echo "\n-- Multibyte string --\n";
+var_dump(bin2hex(mb_strrchr($string_mb, '')));
+var_dump(bin2hex(mb_strrchr($string_mb, '', false, 'utf-8')));
+var_dump(bin2hex(mb_strrchr($string_mb, '', true)));
+
+?>
+--EXPECT--
+-- ASCII string --
+string(0) ""
+string(0) ""
+string(0) ""
+
+-- Multibyte string --
+string(0) ""
+string(0) ""
+string(0) ""
--- /dev/null
+--TEST--
+Test mb_strripos() function : with empty needle
+--SKIPIF--
+<?php
+extension_loaded('mbstring') or die('skip');
+function_exists('mb_strripos') or die("skip mb_strripos() is not available in this build");
+?>
+--FILE--
+<?php
+
+mb_internal_encoding('UTF-8');
+
+$string_ascii = 'abc def';
+// Japanese string in UTF-8
+$string_mb = "日本語テキストです。0123456789。";
+
+echo "\n-- ASCII string without offset --\n";
+var_dump(mb_strripos($string_ascii, ''));
+
+echo "\n-- ASCII string with in range positive offset --\n";
+var_dump(mb_strripos($string_ascii, '', 2));
+
+echo "\n-- ASCII string with in range negative offset --\n";
+var_dump(mb_strripos($string_ascii, '', -2));
+
+echo "\n-- ASCII string with out of bound positive offset --\n";
+var_dump(mb_strripos($string_ascii, '', 15));
+
+echo "\n-- ASCII string with out of bound negative offset --\n";
+var_dump(mb_strripos($string_ascii, '', -15));
+
+
+echo "\n-- Multi-byte string without offset --\n";
+var_dump(mb_strripos($string_mb, ''));
+
+echo "\n-- Multi-byte string with in range positive offset --\n";
+var_dump(mb_strripos($string_mb, '', 2));
+
+echo "\n-- Multi-byte string with in range negative offset --\n";
+var_dump(mb_strripos($string_mb, '', -2));
+
+echo "\n-- Multi-byte string with out of bound positive offset --\n";
+var_dump(mb_strripos($string_mb, '', 150));
+
+echo "\n-- Multi-byte string with out of bound negative offset --\n";
+var_dump(mb_strripos($string_mb, '', -150));
+
+?>
+--EXPECTF--
+-- ASCII string without offset --
+int(7)
+
+-- ASCII string with in range positive offset --
+int(7)
+
+-- ASCII string with in range negative offset --
+int(5)
+
+-- ASCII string with out of bound positive offset --
+
+Warning: mb_strripos(): Offset is greater than the length of haystack string in %s on line %d
+bool(false)
+
+-- ASCII string with out of bound negative offset --
+
+Warning: mb_strripos(): Offset is greater than the length of haystack string in %s on line %d
+bool(false)
+
+-- Multi-byte string without offset --
+int(21)
+
+-- Multi-byte string with in range positive offset --
+int(21)
+
+-- Multi-byte string with in range negative offset --
+int(19)
+
+-- Multi-byte string with out of bound positive offset --
+
+Warning: mb_strripos(): Offset is greater than the length of haystack string in %s on line %d
+bool(false)
+
+-- Multi-byte string with out of bound negative offset --
+
+Warning: mb_strripos(): Offset is greater than the length of haystack string in %s on line %d
+bool(false)
--- /dev/null
+--TEST--
+Test mb_strrpos() function : with empty needle
+--SKIPIF--
+<?php
+extension_loaded('mbstring') or die('skip');
+function_exists('mb_strrpos') or die("skip mb_strrpos() is not available in this build");
+?>
+--FILE--
+<?php
+
+mb_internal_encoding('UTF-8');
+
+$string_ascii = 'abc def';
+// Japanese string in UTF-8
+$string_mb = "日本語テキストです。0123456789。";
+
+echo "\n-- ASCII string without offset --\n";
+var_dump(mb_strrpos($string_ascii, ''));
+
+echo "\n-- ASCII string with in range positive offset --\n";
+var_dump(mb_strrpos($string_ascii, '', 2));
+
+echo "\n-- ASCII string with in range negative offset --\n";
+var_dump(mb_strrpos($string_ascii, '', -2));
+
+echo "\n-- ASCII string with out of bound positive offset --\n";
+var_dump(mb_strrpos($string_ascii, '', 15));
+
+echo "\n-- ASCII string with out of bound negative offset --\n";
+var_dump(mb_strrpos($string_ascii, '', -15));
+
+
+echo "\n-- Multi-byte string without offset --\n";
+var_dump(mb_strrpos($string_mb, ''));
+
+echo "\n-- Multi-byte string with in range positive offset --\n";
+var_dump(mb_strrpos($string_mb, '', 2));
+
+echo "\n-- Multi-byte string with in range negative offset --\n";
+var_dump(mb_strrpos($string_mb, '', -2));
+
+echo "\n-- Multi-byte string with out of bound positive offset --\n";
+var_dump(mb_strrpos($string_mb, '', 150));
+
+echo "\n-- Multi-byte string with out of bound negative offset --\n";
+var_dump(mb_strrpos($string_mb, '', -150));
+
+?>
+--EXPECTF--
+-- ASCII string without offset --
+int(7)
+
+-- ASCII string with in range positive offset --
+int(7)
+
+-- ASCII string with in range negative offset --
+int(5)
+
+-- ASCII string with out of bound positive offset --
+
+Warning: mb_strrpos(): Offset is greater than the length of haystack string in %s on line %d
+bool(false)
+
+-- ASCII string with out of bound negative offset --
+
+Warning: mb_strrpos(): Offset is greater than the length of haystack string in %s on line %d
+bool(false)
+
+-- Multi-byte string without offset --
+int(21)
+
+-- Multi-byte string with in range positive offset --
+int(21)
+
+-- Multi-byte string with in range negative offset --
+int(19)
+
+-- Multi-byte string with out of bound positive offset --
+
+Warning: mb_strrpos(): Offset is greater than the length of haystack string in %s on line %d
+bool(false)
+
+-- Multi-byte string with out of bound negative offset --
+
+Warning: mb_strrpos(): Offset is greater than the length of haystack string in %s on line %d
+bool(false)
--- /dev/null
+--TEST--
+Test mb_strstr() function : with empty needle
+--SKIPIF--
+<?php
+extension_loaded('mbstring') or die('skip');
+function_exists('mb_strstr') or die("skip mb_strstr() is not available in this build");
+?>
+--FILE--
+<?php
+
+mb_internal_encoding('UTF-8');
+
+$string_ascii = 'abc def';
+// Japanese string in UTF-8
+$string_mb = "日本語テキストです。0123456789。";
+
+echo "\n-- ASCII string --\n";
+var_dump(mb_strstr($string_ascii, '', false, 'ISO-8859-1'));
+var_dump(mb_strstr($string_ascii, ''));
+var_dump(mb_strstr($string_ascii, '', true));
+
+echo "\n-- Multibyte string --\n";
+var_dump(mb_strstr($string_mb, ''));
+var_dump(mb_strstr($string_mb, '', false, 'utf-8'));
+var_dump(mb_strstr($string_mb, '', true));
+
+?>
+--EXPECT--
+-- ASCII string --
+string(7) "abc def"
+string(7) "abc def"
+string(0) ""
+
+-- Multibyte string --
+string(53) "日本語テキストです。0123456789。"
+string(53) "日本語テキストです。0123456789。"
+string(0) ""