mirror of
https://github.com/php/php-src.git
synced 2024-11-24 18:34:21 +08:00
mb_ereg_search_setpos(): Add support for negative position
Also add missing test for this function
This commit is contained in:
parent
70187267b4
commit
882f97042b
@ -1400,6 +1400,11 @@ PHP_FUNCTION(mb_ereg_search_setpos)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Accept negative position if length of search string can be determined */
|
||||||
|
if ((position < 0) && (!Z_ISUNDEF(MBREX(search_str))) && (Z_TYPE(MBREX(search_str)) == IS_STRING)) {
|
||||||
|
position += Z_STRLEN(MBREX(search_str));
|
||||||
|
}
|
||||||
|
|
||||||
if (position < 0 || (!Z_ISUNDEF(MBREX(search_str)) && Z_TYPE(MBREX(search_str)) == IS_STRING && (size_t)position >= Z_STRLEN(MBREX(search_str)))) {
|
if (position < 0 || (!Z_ISUNDEF(MBREX(search_str)) && Z_TYPE(MBREX(search_str)) == IS_STRING && (size_t)position >= Z_STRLEN(MBREX(search_str)))) {
|
||||||
php_error_docref(NULL, E_WARNING, "Position is out of range");
|
php_error_docref(NULL, E_WARNING, "Position is out of range");
|
||||||
MBREX(search_pos) = 0;
|
MBREX(search_pos) = 0;
|
||||||
|
70
ext/mbstring/tests/mb_ereg_search_setpos.phpt
Normal file
70
ext/mbstring/tests/mb_ereg_search_setpos.phpt
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
--TEST--
|
||||||
|
mb_ereg_search_setpos() function
|
||||||
|
--SKIPIF--
|
||||||
|
<?php
|
||||||
|
if (!extension_loaded('mbstring')) die('skip mbstring not enabled');
|
||||||
|
?>
|
||||||
|
--FILE--
|
||||||
|
<?php
|
||||||
|
mb_regex_encoding('iso-8859-1');
|
||||||
|
$test_str = 'Iñtërnâtiônàlizætiøn'; // Length = 20
|
||||||
|
|
||||||
|
var_dump(mb_ereg_search_setpos(50)); // OK
|
||||||
|
var_dump(mb_ereg_search_setpos(-1)); // Error
|
||||||
|
|
||||||
|
mb_ereg_search_init($test_str);
|
||||||
|
|
||||||
|
$positions = array( 5, 19, 20, 25, 0, -5, -20, -30);
|
||||||
|
foreach($positions as $pos) {
|
||||||
|
echo("\n* Position: $pos :\n");
|
||||||
|
var_dump(mb_ereg_search_setpos($pos));
|
||||||
|
var_dump(mb_ereg_search_getpos());
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
==DONE==
|
||||||
|
--EXPECTF--
|
||||||
|
bool(true)
|
||||||
|
|
||||||
|
Warning: mb_ereg_search_setpos(): Position is out of range in %s on line %d
|
||||||
|
bool(false)
|
||||||
|
|
||||||
|
* Position: 5 :
|
||||||
|
bool(true)
|
||||||
|
int(5)
|
||||||
|
|
||||||
|
* Position: 19 :
|
||||||
|
bool(true)
|
||||||
|
int(19)
|
||||||
|
|
||||||
|
* Position: 20 :
|
||||||
|
|
||||||
|
Warning: mb_ereg_search_setpos(): Position is out of range in %s on line %d
|
||||||
|
bool(false)
|
||||||
|
int(0)
|
||||||
|
|
||||||
|
* Position: 25 :
|
||||||
|
|
||||||
|
Warning: mb_ereg_search_setpos(): Position is out of range in %s on line %d
|
||||||
|
bool(false)
|
||||||
|
int(0)
|
||||||
|
|
||||||
|
* Position: 0 :
|
||||||
|
bool(true)
|
||||||
|
int(0)
|
||||||
|
|
||||||
|
* Position: -5 :
|
||||||
|
bool(true)
|
||||||
|
int(15)
|
||||||
|
|
||||||
|
* Position: -20 :
|
||||||
|
bool(true)
|
||||||
|
int(0)
|
||||||
|
|
||||||
|
* Position: -30 :
|
||||||
|
|
||||||
|
Warning: mb_ereg_search_setpos(): Position is out of range in %s on line %d
|
||||||
|
bool(false)
|
||||||
|
int(0)
|
||||||
|
==DONE==
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user