com php-src: Add negative offset support to iconv_strpos( ): ext/iconv/iconv.c ext/iconv/tests/iconv_strpos .phpt
ext/iconv/tests/iconv_strpos_variation3.phpt ext/iconv/tests/iconv_strpos_variation3_64bit. phpt ext/iconv/tests/iconv_strpos_variation5.phpt

From: Date: Wed, 09 Mar 2016 13:41:38 +0000
Subject: com php-src: Add negative offset support to iconv_strpos( ): ext/iconv/iconv.c ext/iconv/tests/iconv_strpos .phpt
ext/iconv/tests/iconv_strpos_variation3.phpt ext/iconv/tests/iconv_strpos_variation3_64bit. phpt ext/iconv/tests/iconv_strpos_variation5.phpt
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-91298@lists.php.net to get a copy of this message
Commit: 570bbed98e54aaf132b97befb92689c96bdee687 Author: Francois Laupretre <francois@tekwire.net> Thu, 24 Dec 2015 19:03:10 +0100 Committer: Nikita Popov <nikic@php.net> Wed, 9 Mar 2016 14:41:38 +0100 Parents: 4a8c51c7d7f28653203ed6e5d6c89e5e829c57a3 Branches: master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=570bbed98e54aaf132b97befb92689c96bdee687 Log: Add negative offset support to iconv_strpos() Changed paths: M ext/iconv/iconv.c M ext/iconv/tests/iconv_strpos.phpt M ext/iconv/tests/iconv_strpos_variation3.phpt M ext/iconv/tests/iconv_strpos_variation3_64bit.phpt M ext/iconv/tests/iconv_strpos_variation5.phpt

diff --git a/ext/iconv/iconv.c b/ext/iconv/iconv.c index d4a7f6e..0d44936 100644 --- a/ext/iconv/iconv.c +++ b/ext/iconv/iconv.c @@ -2121,7 +2121,7 @@ PHP_FUNCTION(iconv_substr) PHP_FUNCTION(iconv_strpos) { char *charset = get_internal_encoding(); - size_t charset_len = 0; + size_t charset_len = 0, haystk_len; zend_string *haystk; zend_string *ndl; zend_long offset = 0; @@ -2142,8 +2142,17 @@ PHP_FUNCTION(iconv_strpos) } if (offset < 0) { - php_error_docref(NULL, E_WARNING, "Offset not contained in string."); - RETURN_FALSE; + /* Convert negative offset (counted from the end of string) */ + err = _php_iconv_strlen(&haystk_len, ZSTR_VAL(haystk), ZSTR_LEN(haystk), charset); + if (err != PHP_ICONV_ERR_SUCCESS) { + _php_iconv_show_error(err, GENERIC_SUPERSET_NAME, charset); + RETURN_FALSE; + } + offset += haystk_len; + if (offset < 0) { /* If offset before start */ + php_error_docref(NULL, E_WARNING, "Offset not contained in string."); + RETURN_FALSE; + } } if (ZSTR_LEN(ndl) < 1) { diff --git a/ext/iconv/tests/iconv_strpos.phpt b/ext/iconv/tests/iconv_strpos.phpt index f0f1c48..28e3fe2 100644 --- a/ext/iconv/tests/iconv_strpos.phpt +++ b/ext/iconv/tests/iconv_strpos.phpt @@ -24,6 +24,7 @@ function foo($haystk, $needle, $offset, $to_charset = false, $from_charset = fal } } foo("abecdbcdabef", "bcd", -1); +foo("abecdbcdabef", "bcd", -7); foo("abecdbcdabef", "bcd", 100000); foo("abcabcabcdabcababcdabc", "bcd", 0); foo("abcabcabcdabcababcdabc", "bcd", 10); @@ -38,8 +39,9 @@ var_dump(iconv_strpos("", "string")); ?> --EXPECTF-- bool(false) -2: %s bool(false) +int(5) +int(5) 2: %s bool(false) bool(false) diff --git a/ext/iconv/tests/iconv_strpos_variation3.phpt b/ext/iconv/tests/iconv_strpos_variation3.phpt index aa9bc1a..6f27b74 100644 --- a/ext/iconv/tests/iconv_strpos_variation3.phpt +++ b/ext/iconv/tests/iconv_strpos_variation3.phpt @@ -48,56 +48,58 @@ $fp = fopen(__FILE__, "r"); $inputs = array( // int data -/*1*/ 0, + 0, 1, 12345, + -5, -2345, // float data -/*5*/ 10.5, - -10.5, + 10.5, + -9.5, + -100.3, 12.3456789000e10, 12.3456789000E-10, .5, // null data -/*10*/ NULL, + NULL, null, // boolean data -/*12*/ true, + true, false, TRUE, FALSE, // empty data -/*16*/ "", + "", '', // string data -/*18*/ "string", + "string", 'string', $heredoc, // object data -/*21*/ new classA(), + new classA(), // undefined data -/*22*/ @$undefined_var, + @$undefined_var, // unset data -/*23*/ @$unset_var, + @$unset_var, // resource variable -/*24*/ $fp + $fp ); // loop through each element of $inputs to check the behavior of iconv_strpos() -$iterator = 1; + foreach($inputs as $input) { - echo "\n-- Iteration $iterator --\n"; + echo "--\n"; + var_dump($input); var_dump( iconv_strpos($haystack, $needle, $input, $encoding)); - $iterator++; }; fclose($fp); @@ -106,96 +108,103 @@ echo "Done"; ?> --EXPECTF-- *** Testing iconv_strpos() : usage variations *** - --- Iteration 1 -- +-- +int(0) int(8) - --- Iteration 2 -- +-- +int(1) int(8) - --- Iteration 3 -- +-- +int(12345) bool(false) - --- Iteration 4 -- +-- +int(-5) +int(8) +-- +int(-2345) Warning: iconv_strpos(): Offset not contained in string. in %s on line %d bool(false) - --- Iteration 5 -- +-- +float(10.5) bool(false) - --- Iteration 6 -- +-- +float(-9.5) +int(8) +-- +float(-100.3) Warning: iconv_strpos(): Offset not contained in string. in %s on line %d bool(false) - --- Iteration 7 -- +-- +float(123456789000) Warning: iconv_strpos() expects parameter 3 to be integer, float given in %s on line %d bool(false) - --- Iteration 8 -- +-- +float(1.23456789E-9) int(8) - --- Iteration 9 -- +-- +float(0.5) int(8) - --- Iteration 10 -- +-- +NULL int(8) - --- Iteration 11 -- +-- +NULL int(8) - --- Iteration 12 -- +-- +bool(true) int(8) - --- Iteration 13 -- +-- +bool(false) int(8) - --- Iteration 14 -- +-- +bool(true) int(8) - --- Iteration 15 -- +-- +bool(false) int(8) - --- Iteration 16 -- +-- +string(0) "" Warning: iconv_strpos() expects parameter 3 to be integer, string given in %s on line %d bool(false) - --- Iteration 17 -- +-- +string(0) "" Warning: iconv_strpos() expects parameter 3 to be integer, string given in %s on line %d bool(false) - --- Iteration 18 -- +-- +string(6) "string" Warning: iconv_strpos() expects parameter 3 to be integer, string given in %s on line %d bool(false) - --- Iteration 19 -- +-- +string(6) "string" Warning: iconv_strpos() expects parameter 3 to be integer, string given in %s on line %d bool(false) - --- Iteration 20 -- +-- +string(11) "hello world" Warning: iconv_strpos() expects parameter 3 to be integer, string given in %s on line %d bool(false) - --- Iteration 21 -- +-- +object(classA)#%d (%d) { +} Warning: iconv_strpos() expects parameter 3 to be integer, object given in %s on line %d bool(false) - --- Iteration 22 -- +-- +NULL int(8) - --- Iteration 23 -- +-- +NULL int(8) - --- Iteration 24 -- +-- +resource(%d) of type (stream) Warning: iconv_strpos() expects parameter 3 to be integer, resource given in %s on line %d bool(false) -Done \ No newline at end of file +Done diff --git a/ext/iconv/tests/iconv_strpos_variation3_64bit.phpt b/ext/iconv/tests/iconv_strpos_variation3_64bit.phpt index d915339..2704493 100644 --- a/ext/iconv/tests/iconv_strpos_variation3_64bit.phpt +++ b/ext/iconv/tests/iconv_strpos_variation3_64bit.phpt @@ -48,56 +48,58 @@ $fp = fopen(__FILE__, "r"); $inputs = array( // int data -/*1*/ 0, + 0, 1, 12345, + -5, -2345, // float data -/*5*/ 10.5, - -10.5, + 10.5, + -9.5, + -100.3, 12.3456789000e10, 12.3456789000E-10, .5, // null data -/*10*/ NULL, + NULL, null, // boolean data -/*12*/ true, + true, false, TRUE, FALSE, // empty data -/*16*/ "", + "", '', // string data -/*18*/ "string", + "string", 'string', $heredoc, // object data -/*21*/ new classA(), + new classA(), // undefined data -/*22*/ @$undefined_var, + @$undefined_var, // unset data -/*23*/ @$unset_var, + @$unset_var, // resource variable -/*24*/ $fp + $fp ); // loop through each element of $inputs to check the behavior of iconv_strpos() -$iterator = 1; + foreach($inputs as $input) { - echo "\n-- Iteration $iterator --\n"; + echo "--\n"; + var_dump($input); var_dump( iconv_strpos($haystack, $needle, $input, $encoding)); - $iterator++; }; fclose($fp); @@ -106,93 +108,100 @@ echo "Done"; ?> --EXPECTF-- *** Testing iconv_strpos() : usage variations *** - --- Iteration 1 -- +-- +int(0) int(8) - --- Iteration 2 -- +-- +int(1) int(8) - --- Iteration 3 -- +-- +int(12345) bool(false) - --- Iteration 4 -- +-- +int(-5) +int(8) +-- +int(-2345) Warning: iconv_strpos(): Offset not contained in string. in %s on line %d bool(false) - --- Iteration 5 -- +-- +float(10.5) bool(false) - --- Iteration 6 -- +-- +float(-9.5) +int(8) +-- +float(-100.3) Warning: iconv_strpos(): Offset not contained in string. in %s on line %d bool(false) - --- Iteration 7 -- +-- +float(123456789000) bool(false) - --- Iteration 8 -- +-- +float(1.23456789E-9) int(8) - --- Iteration 9 -- +-- +float(0.5) int(8) - --- Iteration 10 -- +-- +NULL int(8) - --- Iteration 11 -- +-- +NULL int(8) - --- Iteration 12 -- +-- +bool(true) int(8) - --- Iteration 13 -- +-- +bool(false) int(8) - --- Iteration 14 -- +-- +bool(true) int(8) - --- Iteration 15 -- +-- +bool(false) int(8) - --- Iteration 16 -- +-- +string(0) "" Warning: iconv_strpos() expects parameter 3 to be integer, string given in %s on line %d bool(false) - --- Iteration 17 -- +-- +string(0) "" Warning: iconv_strpos() expects parameter 3 to be integer, string given in %s on line %d bool(false) - --- Iteration 18 -- +-- +string(6) "string" Warning: iconv_strpos() expects parameter 3 to be integer, string given in %s on line %d bool(false) - --- Iteration 19 -- +-- +string(6) "string" Warning: iconv_strpos() expects parameter 3 to be integer, string given in %s on line %d bool(false) - --- Iteration 20 -- +-- +string(11) "hello world" Warning: iconv_strpos() expects parameter 3 to be integer, string given in %s on line %d bool(false) - --- Iteration 21 -- +-- +object(classA)#%d (%d) { +} Warning: iconv_strpos() expects parameter 3 to be integer, object given in %s on line %d bool(false) - --- Iteration 22 -- +-- +NULL int(8) - --- Iteration 23 -- +-- +NULL int(8) - --- Iteration 24 -- +-- +resource(%d) of type (stream) Warning: iconv_strpos() expects parameter 3 to be integer, resource given in %s on line %d bool(false) diff --git a/ext/iconv/tests/iconv_strpos_variation5.phpt b/ext/iconv/tests/iconv_strpos_variation5.phpt index 3db0634..fcd5aae 100644 --- a/ext/iconv/tests/iconv_strpos_variation5.phpt +++ b/ext/iconv/tests/iconv_strpos_variation5.phpt @@ -32,10 +32,9 @@ $needle_mb = base64_decode(b'44CC'); /* * Loop through integers as multiples of ten for $offset argument - * iconv_strpos should not be able to accept negative values as $offset. * 60 is larger than *BYTE* count for $string_mb */ -for ($i = -10; $i <= 60; $i += 10) { +for ($i = -30; $i <= 60; $i += 10) { echo "\n**-- Offset is: $i --**\n"; echo "-- ASCII String --\n"; var_dump(iconv_strpos($string_ascii, $needle_ascii, $i)); @@ -49,7 +48,7 @@ echo "Done"; --EXPECTF-- *** Testing iconv_strpos() : usage variations *** -**-- Offset is: -10 --** +**-- Offset is: -30 --** -- ASCII String -- Warning: iconv_strpos(): Offset not contained in string. in %s on line %d @@ -59,6 +58,18 @@ bool(false) Warning: iconv_strpos(): Offset not contained in string. in %s on line %d bool(false) +**-- Offset is: -20 --** +-- ASCII String -- +int(9) +--Multibyte String -- +int(9) + +**-- Offset is: -10 --** +-- ASCII String -- +int(20) +--Multibyte String -- +int(20) + **-- Offset is: 0 --** -- ASCII String -- int(9)
« previous php.cvs (#91298) next »