]> granicus.if.org Git - php/commitdiff
Improve strpos and strstr function family implementation
authorGeorge Peter Banyard <girgias@php.net>
Sun, 18 Aug 2019 13:33:10 +0000 (15:33 +0200)
committerGeorge Peter Banyard <girgias@php.net>
Mon, 26 Aug 2019 15:11:37 +0000 (17:11 +0200)
31 files changed:
UPGRADING
Zend/zend_operators.h
ext/standard/string.c
ext/standard/tests/strings/bug63943.phpt [deleted file]
ext/standard/tests/strings/stripos.phpt
ext/standard/tests/strings/stripos_variation1.phpt
ext/standard/tests/strings/stripos_variation10.phpt
ext/standard/tests/strings/stripos_variation11.phpt
ext/standard/tests/strings/stripos_variation2.phpt
ext/standard/tests/strings/stripos_variation3.phpt
ext/standard/tests/strings/stripos_variation7.phpt
ext/standard/tests/strings/stristr.phpt
ext/standard/tests/strings/stristr_error.phpt [deleted file]
ext/standard/tests/strings/stristr_variation2.phpt
ext/standard/tests/strings/strpos.phpt
ext/standard/tests/strings/strpos_variation1.phpt [new file with mode: 0644]
ext/standard/tests/strings/strripos.phpt
ext/standard/tests/strings/strripos_variation1.phpt
ext/standard/tests/strings/strripos_variation2.phpt
ext/standard/tests/strings/strripos_variation3.phpt
ext/standard/tests/strings/strripos_variation6.phpt [new file with mode: 0644]
ext/standard/tests/strings/strrpos.phpt
ext/standard/tests/strings/strrpos_variation1.phpt
ext/standard/tests/strings/strrpos_variation10.phpt
ext/standard/tests/strings/strrpos_variation11.phpt
ext/standard/tests/strings/strrpos_variation14.phpt [new file with mode: 0644]
ext/standard/tests/strings/strrpos_variation2.phpt
ext/standard/tests/strings/strrpos_variation3.phpt
ext/standard/tests/strings/strrpos_variation7.phpt
ext/standard/tests/strings/strstr.phpt
ext/standard/tests/strings/strstr_variation1.phpt [new file with mode: 0644]

index a398a61cc6fd8c33a9facfb4a529e42411422dfa..e4f2feccf231bdf9a818fef681610013a5a07ea9 100644 (file)
--- a/UPGRADING
+++ b/UPGRADING
@@ -202,6 +202,7 @@ PHP 8.0 UPGRADE NOTES
     string. Previously non-string needles were interpreted as an ASCII code
     point. An explicit call to chr() can be used to restore the previous
     behavior.
+  . The needle argument for strpos(), strrpos(), stripos(), strripos(), strstr() and stristr() can now be empty.
   . The 'salt' option of password_hash() is no longer supported. If the 'salt'
     option is used a warning is generated, the provided salt is ignored, and a
     generated salt is used instead.
index 882ec2b66e18b522571ba7b89d835d2278e786dc..b42ad7f39575e54ca14505529d3c5fb60868da75 100644 (file)
@@ -152,6 +152,10 @@ zend_memnstr(const char *haystack, const char *needle, size_t needle_len, const
        ptrdiff_t off_p;
        size_t off_s;
 
+       if (needle_len == 0) {
+               return p;
+       }
+
        if (needle_len == 1) {
                return (const char *)memchr(p, *needle, (end-p));
        }
@@ -210,6 +214,10 @@ zend_memnrstr(const char *haystack, const char *needle, size_t needle_len, const
     ptrdiff_t off_p;
     size_t off_s;
 
+       if (needle_len == 0) {
+               return p;
+       }
+
     if (needle_len == 1) {
         return (const char *)zend_memrchr(haystack, *needle, (p - haystack));
     }
index 7d7d0faaa0cffc04e93be7946951730ea9ff6fc8..526eb1353a55c733823b6f8e29363132d4d02d8e 100644 (file)
@@ -1812,11 +1812,6 @@ PHP_FUNCTION(stristr)
                Z_PARAM_BOOL(part)
        ZEND_PARSE_PARAMETERS_END();
 
-       if (!ZSTR_LEN(needle)) {
-               php_error_docref(NULL, E_WARNING, "Empty needle");
-               RETURN_FALSE;
-       }
-
        haystack_dup = estrndup(ZSTR_VAL(haystack), ZSTR_LEN(haystack));
        orig_needle = estrndup(ZSTR_VAL(needle), ZSTR_LEN(needle));
        found = php_stristr(haystack_dup, orig_needle, ZSTR_LEN(haystack), ZSTR_LEN(needle));
@@ -1853,11 +1848,6 @@ PHP_FUNCTION(strstr)
                Z_PARAM_BOOL(part)
        ZEND_PARSE_PARAMETERS_END();
 
-       if (!ZSTR_LEN(needle)) {
-               php_error_docref(NULL, E_WARNING, "Empty needle");
-               RETURN_FALSE;
-       }
-
        found = php_memnstr(ZSTR_VAL(haystack), ZSTR_VAL(needle), ZSTR_LEN(needle), ZSTR_VAL(haystack) + ZSTR_LEN(haystack));
 
        if (found) {
@@ -1899,11 +1889,6 @@ PHP_FUNCTION(strpos)
                RETURN_FALSE;
        }
 
-       if (!ZSTR_LEN(needle)) {
-               php_error_docref(NULL, E_WARNING, "Empty needle");
-               RETURN_FALSE;
-       }
-
        found = (char*)php_memnstr(ZSTR_VAL(haystack) + offset,
                                                ZSTR_VAL(needle), ZSTR_LEN(needle),
                                                ZSTR_VAL(haystack) + ZSTR_LEN(haystack));
@@ -1940,7 +1925,7 @@ PHP_FUNCTION(stripos)
                RETURN_FALSE;
        }
 
-       if (ZSTR_LEN(needle) == 0 || ZSTR_LEN(needle) > ZSTR_LEN(haystack)) {
+       if (ZSTR_LEN(needle) > ZSTR_LEN(haystack)) {
                RETURN_FALSE;
        }
 
@@ -1964,23 +1949,18 @@ PHP_FUNCTION(stripos)
    Finds position of last occurrence of a string within another string */
 PHP_FUNCTION(strrpos)
 {
+       zend_string *needle;
        zend_string *haystack;
-       char *needle;
-       size_t needle_len;
        zend_long offset = 0;
        const char *p, *e, *found;
 
        ZEND_PARSE_PARAMETERS_START(2, 3)
                Z_PARAM_STR(haystack)
-               Z_PARAM_STRING(needle, needle_len)
+               Z_PARAM_STR(needle)
                Z_PARAM_OPTIONAL
                Z_PARAM_LONG(offset)
        ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
 
-       if (ZSTR_LEN(haystack) == 0 || needle_len == 0) {
-               RETURN_FALSE;
-       }
-
        if (offset >= 0) {
                if ((size_t)offset > ZSTR_LEN(haystack)) {
                        php_error_docref(NULL, E_WARNING, "Offset not contained in string");
@@ -1993,15 +1973,16 @@ PHP_FUNCTION(strrpos)
                        php_error_docref(NULL, E_WARNING, "Offset not contained in string");
                        RETURN_FALSE;
                }
+
                p = ZSTR_VAL(haystack);
-               if ((size_t)-offset < needle_len) {
+               if ((size_t)-offset < ZSTR_LEN(needle)) {
                        e = ZSTR_VAL(haystack) + ZSTR_LEN(haystack);
                } else {
-                       e = ZSTR_VAL(haystack) + ZSTR_LEN(haystack) + offset + needle_len;
+                       e = ZSTR_VAL(haystack) + ZSTR_LEN(haystack) + offset + ZSTR_LEN(needle);
                }
        }
 
-       if ((found = zend_memnrstr(p, needle, needle_len, e))) {
+       if ((found = zend_memnrstr(p, ZSTR_VAL(needle), ZSTR_LEN(needle), e))) {
                RETURN_LONG(found - ZSTR_VAL(haystack));
        }
 
@@ -2026,10 +2007,6 @@ PHP_FUNCTION(strripos)
                Z_PARAM_LONG(offset)
        ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
 
-       if (ZSTR_LEN(haystack) == 0 || ZSTR_LEN(needle) == 0) {
-               RETURN_FALSE;
-       }
-
        if (ZSTR_LEN(needle) == 1) {
                /* Single character search can shortcut memcmps
                   Can also avoid tolower emallocs */
@@ -2075,6 +2052,7 @@ PHP_FUNCTION(strripos)
                        php_error_docref(NULL, E_WARNING, "Offset not contained in string");
                        RETURN_FALSE;
                }
+
                p = ZSTR_VAL(haystack_dup);
                if ((size_t)-offset < ZSTR_LEN(needle)) {
                        e = ZSTR_VAL(haystack_dup) + ZSTR_LEN(haystack);
diff --git a/ext/standard/tests/strings/bug63943.phpt b/ext/standard/tests/strings/bug63943.phpt
deleted file mode 100644 (file)
index 6018879..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
---TEST--
-Bug #63943 (Bad warning text from strpos() on empty needle)
---FILE--
-<?php
-strpos("lllllll", '');
-?>
---EXPECTF--
-Warning: strpos(): Empty needle in %sbug63943.php on line %d
index 4c3c5a7fccc219eeecd59c8bb5ab4e3f71341699..bc2a7bc8e5e83a7445122eb1cd5355356886ff27 100644 (file)
@@ -25,9 +25,9 @@ stripos() function test
        var_dump(stripos("0", false));
        var_dump(stripos("1", true));
        var_dump(stripos("\\\\a", "\\a"));
-
-       echo "Done\n";
 ?>
+
+DONE
 --EXPECT--
 int(0)
 int(5)
@@ -37,19 +37,20 @@ int(10)
 int(2)
 int(0)
 int(0)
-bool(false)
-bool(false)
-bool(false)
-bool(false)
+int(0)
 int(0)
 bool(false)
 bool(false)
+int(0)
 bool(false)
-bool(false)
+int(0)
 bool(false)
 bool(false)
 int(0)
 bool(false)
 int(0)
+int(0)
+int(0)
 int(1)
-Done
+
+DONE
index 53d6be65e7e0ca05b3cb30163bd63cdf80c31917..c34917ffce63c642c7845590772cb66047f493a1 100644 (file)
@@ -117,20 +117,20 @@ int(9)
 int(8)
 bool(false)
 -- Iteration 12 --
-bool(false)
-bool(false)
+int(0)
+int(11)
 -- Iteration 13 --
-bool(false)
-bool(false)
+int(0)
+int(12)
 -- Iteration 14 --
-bool(false)
-bool(false)
+int(0)
+int(13)
 -- Iteration 15 --
-bool(false)
-bool(false)
+int(0)
+int(14)
 -- Iteration 16 --
-bool(false)
-bool(false)
+int(0)
+int(15)
 -- Iteration 17 --
 int(10)
 int(47)
index 6792b4d0b2980c218b1d4db1fe466138ea2e1bf0..879da88aa7d4cb20fd1afa095d250fee45134744 100644 (file)
@@ -95,7 +95,7 @@ fclose($file_handle);  //closing the file handle
 
 echo "*** Done ***";
 ?>
---EXPECTF--
+--EXPECT--
 *** Testing stripos() function with unexpected values for needle ***
 
 -- Iteration 1 --
@@ -144,35 +144,35 @@ stripos() expects parameter 2 to be string, array given
 int(9)
 
 -- Iteration 16 --
-bool(false)
+int(0)
 
 -- Iteration 17 --
 int(9)
 
 -- Iteration 18 --
-bool(false)
+int(0)
 
 -- Iteration 19 --
 int(64)
 
 -- Iteration 20 --
-bool(false)
+int(0)
 
 -- Iteration 21 --
-bool(false)
+int(0)
 
 -- Iteration 22 --
-bool(false)
+int(0)
 
 -- Iteration 23 --
-bool(false)
+int(0)
 
 -- Iteration 24 --
 stripos() expects parameter 2 to be string, resource given
 
 -- Iteration 25 --
-bool(false)
+int(0)
 
 -- Iteration 26 --
-bool(false)
+int(0)
 *** Done ***
index aab3181df35b3965ab6e26353d6d28a295e489ad..4f002ed0ca367813fd5aba57da9d2e688dcda4d7 100644 (file)
@@ -144,7 +144,7 @@ stripos() expects parameter 1 to be string, array given
 int(0)
 bool(false)
 -- Iteration 16 --
-bool(false)
+int(0)
 
 Warning: stripos(): Offset not contained in string in %s on line %d
 bool(false)
@@ -152,7 +152,7 @@ bool(false)
 int(0)
 bool(false)
 -- Iteration 18 --
-bool(false)
+int(0)
 
 Warning: stripos(): Offset not contained in string in %s on line %d
 bool(false)
@@ -160,22 +160,22 @@ bool(false)
 int(0)
 bool(false)
 -- Iteration 20 --
-bool(false)
+int(0)
 
 Warning: stripos(): Offset not contained in string in %s on line %d
 bool(false)
 -- Iteration 21 --
-bool(false)
+int(0)
 
 Warning: stripos(): Offset not contained in string in %s on line %d
 bool(false)
 -- Iteration 22 --
-bool(false)
+int(0)
 
 Warning: stripos(): Offset not contained in string in %s on line %d
 bool(false)
 -- Iteration 23 --
-bool(false)
+int(0)
 
 Warning: stripos(): Offset not contained in string in %s on line %d
 bool(false)
@@ -183,12 +183,12 @@ bool(false)
 stripos() expects parameter 1 to be string, resource given
 stripos() expects parameter 1 to be string, resource given
 -- Iteration 25 --
-bool(false)
+int(0)
 
 Warning: stripos(): Offset not contained in string in %s on line %d
 bool(false)
 -- Iteration 26 --
-bool(false)
+int(0)
 
 Warning: stripos(): Offset not contained in string in %s on line %d
 bool(false)
index 0f4c887869856d23b0a24be45585e467220a94d1..294b088d02e0c61536861b1b3f83ca64e8953a07 100644 (file)
@@ -119,20 +119,20 @@ bool(false)
 int(10)
 int(10)
 -- Iteration 12 --
-bool(false)
-bool(false)
+int(0)
+int(11)
 -- Iteration 13 --
-bool(false)
-bool(false)
+int(0)
+int(12)
 -- Iteration 14 --
-bool(false)
-bool(false)
+int(0)
+int(13)
 -- Iteration 15 --
-bool(false)
-bool(false)
+int(0)
+int(14)
 -- Iteration 16 --
-bool(false)
-bool(false)
+int(0)
+int(15)
 -- Iteration 17 --
 int(14)
 int(51)
index 936f870579876ec61994ef4111c9a25d5a2e608d..32faf63871bd4472898fcb616e920b69cb94a63d 100644 (file)
@@ -32,6 +32,6 @@ echo "*** Done ***";
 int(14)
 int(23)
 int(23)
-bool(false)
+int(0)
 int(7)
 *** Done ***
index 04c636365fce607dac3f6af378299aa24f3d151f..a21686876d31d4da24aa2decf3d5c9055e338287 100644 (file)
@@ -25,10 +25,10 @@ echo "*** Done ***";
 --EXPECTF--
 *** Testing stripos() function: with heredoc strings ***
 -- With empty heredoc string --
-bool(false)
+int(0)
 
 Warning: stripos(): Offset not contained in string in %s on line %d
 bool(false)
-bool(false)
-bool(false)
+int(0)
+int(0)
 *** Done ***
index 92cfa09eb1e172f05c75e8460155c0a8726185eb..0d47e80bcbccb2ee0fbccec249281d8ddb37e4d9 100644 (file)
@@ -8,10 +8,10 @@ stristr() function
        var_dump(stristr("tEsT sTrInG", "t S"));
        var_dump(stristr("tEsT sTrInG", "g"));
        var_dump(md5(stristr("te".chr(0)."st", chr(0))));
-       var_dump(@stristr("", ""));
-       var_dump(@stristr("a", ""));
-       var_dump(@stristr("", "a"));
-       var_dump(md5(@stristr("\\\\a\\", "\\a")));
+       var_dump(stristr("", ""));
+       var_dump(stristr("a", ""));
+       var_dump(stristr("", "a"));
+       var_dump(md5(stristr("\\\\a\\", "\\a")));
        var_dump(stristr("tEsT sTrInG", " "));
 ?>
 --EXPECTF--
@@ -21,8 +21,8 @@ string(6) "sTrInG"
 string(8) "T sTrInG"
 string(1) "G"
 string(32) "7272696018bdeb2c9a3f8d01fc2a9273"
-bool(false)
-bool(false)
+string(0) ""
+string(1) "a"
 bool(false)
 string(32) "6ec19f52f0766c463f3bb240f4396913"
 string(7) " sTrInG"
diff --git a/ext/standard/tests/strings/stristr_error.phpt b/ext/standard/tests/strings/stristr_error.phpt
deleted file mode 100644 (file)
index a7b683d..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
---TEST--
-Test stristr() function : error conditions
---FILE--
-<?php
-
-/* Prototype: string stristr  ( string $haystack  , mixed $needle  [, bool $before_needle  ] )
-   Description: Case-insensitive strstr()
-*/
-echo "*** Testing stristr() : error conditions ***\n";
-
-echo "\n-- Testing stristr() function with empty haystack --\n";
-var_dump( stristr(NULL, "") );
-
-echo "\n-- Testing stristr() function with empty needle --\n";
-var_dump( stristr("Hello World", "") );
-
-?>
-===DONE===
---EXPECTF--
-*** Testing stristr() : error conditions ***
-
--- Testing stristr() function with empty haystack --
-
-Warning: stristr(): Empty needle in %s on line %d
-bool(false)
-
--- Testing stristr() function with empty needle --
-
-Warning: stristr(): Empty needle in %s on line %d
-bool(false)
-===DONE===
index fd842d11064417060c38f8ef6a1f0ee9954ab8e7..dd0b34f883b54c54e936616ee925418b2d04164e 100644 (file)
@@ -84,7 +84,7 @@ fclose($file_handle);  //closing the file handle
 
 ?>
 ===DONE===
---EXPECTF--
+--EXPECT--
 *** Testing stristr() function: with unexpected inputs for 'needle' argument ***
 -- Iteration 1 --
 bool(false)
@@ -109,33 +109,21 @@ stristr() expects parameter 2 to be string, array given
 -- Iteration 11 --
 bool(false)
 -- Iteration 12 --
-
-Warning: stristr(): Empty needle in %s on line %d
-bool(false)
+string(11) "Hello World"
 -- Iteration 13 --
 bool(false)
 -- Iteration 14 --
-
-Warning: stristr(): Empty needle in %s on line %d
-bool(false)
+string(11) "Hello World"
 -- Iteration 15 --
-
-Warning: stristr(): Empty needle in %s on line %d
-bool(false)
+string(11) "Hello World"
 -- Iteration 16 --
-
-Warning: stristr(): Empty needle in %s on line %d
-bool(false)
+string(11) "Hello World"
 -- Iteration 17 --
 bool(false)
 -- Iteration 18 --
 stristr() expects parameter 2 to be string, resource given
 -- Iteration 19 --
-
-Warning: stristr(): Empty needle in %s on line %d
-bool(false)
+string(11) "Hello World"
 -- Iteration 20 --
-
-Warning: stristr(): Empty needle in %s on line %d
-bool(false)
+string(11) "Hello World"
 ===DONE===
index 4640b670f316314e6a8720da849fa7cb8c84bc38..9fc521d8d87403ad59de409f4c66a707ccf90cb4 100644 (file)
Binary files a/ext/standard/tests/strings/strpos.phpt and b/ext/standard/tests/strings/strpos.phpt differ
diff --git a/ext/standard/tests/strings/strpos_variation1.phpt b/ext/standard/tests/strings/strpos_variation1.phpt
new file mode 100644 (file)
index 0000000..f285173
--- /dev/null
@@ -0,0 +1,23 @@
+--TEST--
+Test strpos() function : usage variations - complex strings containing other than 7-bit chars
+--FILE--
+<?php
+$string = chr(0).chr(128).chr(129).chr(234).chr(235).chr(254).chr(255);
+$stringAsHex = bin2hex($string);
+echo "-- Positions of some chars in the string '$stringAsHex' are as follows --\n";
+echo bin2hex( chr(128) ) ." => ";
+var_dump( strpos($string, chr(128)) );
+echo bin2hex( chr(255) ) ." => ";
+var_dump( strpos($string, chr(255), 3) );
+echo bin2hex( chr(256) ) ." => ";
+var_dump( strpos($string, chr(256)) );
+?>
+
+DONE
+--EXPECT--
+-- Positions of some chars in the string '008081eaebfeff' are as follows --
+80 => int(1)
+ff => int(6)
+00 => int(0)
+
+DONE
index fa15e2fcacae7fc83ecc3ae09d5247b86a30c5f7..c343e92731f309c6f413d3c710eac44ca930ffb1 100644 (file)
@@ -10,11 +10,11 @@ strripos() function
        var_dump(strripos("te".chr(0)."st", chr(0)));
        var_dump(strripos("tEst", "test"));
        var_dump(strripos("teSt", "test"));
-       var_dump(@strripos("foo", "f", 1));
-       var_dump(@strripos("", ""));
-       var_dump(@strripos("a", ""));
-       var_dump(@strripos("", "a"));
-       var_dump(@strripos("\\\\a", "\\a"));
+       var_dump(strripos("foo", "f", 1));
+       var_dump(strripos("", ""));
+       var_dump(strripos("a", ""));
+       var_dump(strripos("", "a"));
+       var_dump(strripos("\\\\a", "\\a"));
 ?>
 --EXPECT--
 int(5)
@@ -26,7 +26,7 @@ int(2)
 int(0)
 int(0)
 bool(false)
-bool(false)
-bool(false)
+int(0)
+int(1)
 bool(false)
 int(1)
index ce396695aff2cba0f98a6729f825a5d87b0e27dd..8b0c3a35ccfa4b8cdf86813683642556da3b9ee3 100644 (file)
@@ -132,30 +132,30 @@ int(8)
 bool(false)
 int(8)
 -- Iteration 12 --
-bool(false)
-bool(false)
-bool(false)
-bool(false)
+int(44)
+int(44)
+int(44)
+int(43)
 -- Iteration 13 --
-bool(false)
-bool(false)
-bool(false)
-bool(false)
+int(44)
+int(44)
+int(44)
+int(43)
 -- Iteration 14 --
-bool(false)
-bool(false)
-bool(false)
-bool(false)
+int(44)
+int(44)
+int(44)
+int(43)
 -- Iteration 15 --
-bool(false)
-bool(false)
-bool(false)
-bool(false)
+int(44)
+int(44)
+int(44)
+int(43)
 -- Iteration 16 --
-bool(false)
-bool(false)
-bool(false)
-bool(false)
+int(44)
+int(44)
+int(44)
+int(43)
 -- Iteration 17 --
 int(43)
 int(43)
index 3800c321551b9bb6f5d32bee1f230adf29be2586..affe72a50af004b89e1bfdff0aa68a9aa31e36f0 100644 (file)
@@ -133,30 +133,30 @@ int(10)
 bool(false)
 int(10)
 -- Iteration 12 --
-bool(false)
-bool(false)
-bool(false)
-bool(false)
+int(54)
+int(54)
+int(54)
+int(53)
 -- Iteration 13 --
-bool(false)
-bool(false)
-bool(false)
-bool(false)
+int(54)
+int(54)
+int(54)
+int(53)
 -- Iteration 14 --
-bool(false)
-bool(false)
-bool(false)
-bool(false)
+int(54)
+int(54)
+int(54)
+int(53)
 -- Iteration 15 --
-bool(false)
-bool(false)
-bool(false)
-bool(false)
+int(54)
+int(54)
+int(54)
+int(53)
 -- Iteration 16 --
-bool(false)
-bool(false)
-bool(false)
-bool(false)
+int(54)
+int(54)
+int(54)
+int(53)
 -- Iteration 17 --
 int(53)
 int(53)
index 4fccaeb47ee724f02fedc32b8ed1b85c403e17dd..0f531f03da92228f72be246e49ce2ce28b78d0b9 100644 (file)
@@ -54,6 +54,6 @@ bool(false)
 -- Multi line strings with no offset -- 
 int(18)
 int(31)
-bool(false)
+int(63)
 int(55)
 ===DONE===
diff --git a/ext/standard/tests/strings/strripos_variation6.phpt b/ext/standard/tests/strings/strripos_variation6.phpt
new file mode 100644 (file)
index 0000000..92bb704
--- /dev/null
@@ -0,0 +1,23 @@
+--TEST--
+Test strrpos() function : usage variations - negative offset with empty needle
+--FILE--
+<?php
+$haystack = "Hello,\t\n\0\n  $&!#%()*<=>?@hello123456he \x234 \101 ";
+
+var_dump(strlen($haystack));
+
+var_dump( strripos($haystack, "", -1) );
+var_dump( strripos($haystack, "", -10) );
+var_dump( strripos($haystack, "", -26) );
+var_dump( strripos($haystack, "", -44) );
+?>
+
+DONE
+--EXPECT--
+int(44)
+int(43)
+int(34)
+int(18)
+int(0)
+
+DONE
index 691f67e599e146eca60b2257bd497f009ec96503..f672d93a2a2cdc573925356e031334ebfff04aae 100644 (file)
@@ -10,11 +10,11 @@ strrpos() function
        var_dump(strrpos("te".chr(0)."st", chr(0)));
        var_dump(strrpos("tEst", "test"));
        var_dump(strrpos("teSt", "test"));
-       var_dump(@strrpos("foo", "f", 1));
-       var_dump(@strrpos("", ""));
-       var_dump(@strrpos("a", ""));
-       var_dump(@strrpos("", "a"));
-       var_dump(@strrpos("\\\\a", "\\a"));
+       var_dump(strrpos("foo", "f", 1));
+       var_dump(strrpos("", ""));
+       var_dump(strrpos("a", ""));
+       var_dump(strrpos("", "a"));
+       var_dump(strrpos("\\\\a", "\\a"));
 ?>
 --EXPECT--
 int(5)
@@ -26,7 +26,7 @@ int(2)
 bool(false)
 bool(false)
 bool(false)
-bool(false)
-bool(false)
+int(0)
+int(1)
 bool(false)
 int(1)
index 4fc480face59926d9b24b2d93d0e84dd356de7b5..d54adaec7383d65537dd9ac2e732ae18047f415d 100644 (file)
@@ -108,20 +108,20 @@ int(9)
 int(8)
 bool(false)
 -- Iteration 12 --
-bool(false)
-bool(false)
+int(44)
+int(44)
 -- Iteration 13 --
-bool(false)
-bool(false)
+int(44)
+int(44)
 -- Iteration 14 --
-bool(false)
-bool(false)
+int(44)
+int(44)
 -- Iteration 15 --
-bool(false)
-bool(false)
+int(44)
+int(44)
 -- Iteration 16 --
-bool(false)
-bool(false)
+int(44)
+int(44)
 -- Iteration 17 --
 int(43)
 int(43)
index 440605d43055acacdea4888184325af42f2cd278..2a6b84337cc776c8b4d6fa99e0e12cf3cebe2ed8 100644 (file)
@@ -128,25 +128,25 @@ strrpos() expects parameter 2 to be string, array given
 -- Iteration 15 --
 int(41)
 -- Iteration 16 --
-bool(false)
+int(87)
 -- Iteration 17 --
 int(41)
 -- Iteration 18 --
-bool(false)
+int(87)
 -- Iteration 19 --
 int(64)
 -- Iteration 20 --
-bool(false)
+int(87)
 -- Iteration 21 --
-bool(false)
+int(87)
 -- Iteration 22 --
-bool(false)
+int(87)
 -- Iteration 23 --
-bool(false)
+int(87)
 -- Iteration 24 --
 strrpos() expects parameter 2 to be string, resource given
 -- Iteration 25 --
-bool(false)
+int(87)
 -- Iteration 26 --
-bool(false)
+int(87)
 *** Done ***
index 71201a1c21f7ca8def138705db72b06ed201d4d5..2fbe78e30acd09ad5d0df37845287f2987771fc3 100644 (file)
@@ -96,7 +96,7 @@ for($index = 0; $index < count($values); $index ++) {
 
 echo "*** Done ***";
 ?>
---EXPECT--
+--EXPECTF--
 *** Testing strrpos() function with unexpected values for haystack and needle ***
 -- Iteration 1 --
 int(0)
@@ -144,36 +144,52 @@ strrpos() expects parameter 1 to be string, array given
 int(0)
 bool(false)
 -- Iteration 16 --
-bool(false)
+int(0)
+
+Warning: strrpos(): Offset not contained in string in %s on line %d
 bool(false)
 -- Iteration 17 --
 int(0)
 bool(false)
 -- Iteration 18 --
-bool(false)
+int(0)
+
+Warning: strrpos(): Offset not contained in string in %s on line %d
 bool(false)
 -- Iteration 19 --
 int(0)
 bool(false)
 -- Iteration 20 --
-bool(false)
+int(0)
+
+Warning: strrpos(): Offset not contained in string in %s on line %d
 bool(false)
 -- Iteration 21 --
-bool(false)
+int(0)
+
+Warning: strrpos(): Offset not contained in string in %s on line %d
 bool(false)
 -- Iteration 22 --
-bool(false)
+int(0)
+
+Warning: strrpos(): Offset not contained in string in %s on line %d
 bool(false)
 -- Iteration 23 --
-bool(false)
+int(0)
+
+Warning: strrpos(): Offset not contained in string in %s on line %d
 bool(false)
 -- Iteration 24 --
 strrpos() expects parameter 1 to be string, resource given
 strrpos() expects parameter 1 to be string, resource given
 -- Iteration 25 --
-bool(false)
+int(0)
+
+Warning: strrpos(): Offset not contained in string in %s on line %d
 bool(false)
 -- Iteration 26 --
-bool(false)
+int(0)
+
+Warning: strrpos(): Offset not contained in string in %s on line %d
 bool(false)
 *** Done ***
diff --git a/ext/standard/tests/strings/strrpos_variation14.phpt b/ext/standard/tests/strings/strrpos_variation14.phpt
new file mode 100644 (file)
index 0000000..d244a19
--- /dev/null
@@ -0,0 +1,23 @@
+--TEST--
+Test strrpos() function : usage variations - negative offset with empty needle
+--FILE--
+<?php
+$haystack = "Hello,\t\n\0\n  $&!#%()*<=>?@hello123456he \x234 \101 ";
+
+var_dump(strlen($haystack));
+
+var_dump( strrpos($haystack, "", -1) );
+var_dump( strrpos($haystack, "", -10) );
+var_dump( strrpos($haystack, "", -26) );
+var_dump( strrpos($haystack, "", -44) );
+?>
+
+DONE
+--EXPECT--
+int(44)
+int(43)
+int(34)
+int(18)
+int(0)
+
+DONE
index af5c9e349d71b83218709530c17add218dc1408a..3accd165bb8d5552f7524f46eb19a6f28d163445 100644 (file)
@@ -109,20 +109,20 @@ bool(false)
 int(10)
 int(10)
 -- Iteration 12 --
-bool(false)
-bool(false)
+int(54)
+int(54)
 -- Iteration 13 --
-bool(false)
-bool(false)
+int(54)
+int(54)
 -- Iteration 14 --
-bool(false)
-bool(false)
+int(54)
+int(54)
 -- Iteration 15 --
-bool(false)
-bool(false)
+int(54)
+int(54)
 -- Iteration 16 --
-bool(false)
-bool(false)
+int(54)
+int(54)
 -- Iteration 17 --
 int(53)
 int(53)
index 460f18cca53dcb4bd7a63e94cceee75f4401313e..1ebe9e7a21255f2d2037969f6561392a3efd1952 100644 (file)
@@ -32,6 +32,6 @@ echo "*** Done ***";
 int(44)
 int(44)
 int(44)
-bool(false)
+int(63)
 int(55)
 *** Done ***
index c0546c74f8ca42a77504fd975e286dd88eb450f5..d5aa8b561ccb5c59a9bb2089ba8d55a8c56f9bd5 100644 (file)
@@ -22,11 +22,13 @@ var_dump( strrpos($empty_string, NULL) );
 
 echo "*** Done ***";
 ?>
---EXPECT--
+--EXPECTF--
 *** Testing strrpos() function: with heredoc strings ***
 -- With empty heredoc string --
+int(0)
+
+Warning: strrpos(): Offset not contained in string in %s on line %d
 bool(false)
-bool(false)
-bool(false)
-bool(false)
+int(0)
+int(0)
 *** Done ***
index 6bdc3feab1a31c62f302f5fe2d92ba5e917439fe..81ddee0804cc2a00c83afda542bf7b49c92bcba9 100644 (file)
Binary files a/ext/standard/tests/strings/strstr.phpt and b/ext/standard/tests/strings/strstr.phpt differ
diff --git a/ext/standard/tests/strings/strstr_variation1.phpt b/ext/standard/tests/strings/strstr_variation1.phpt
new file mode 100644 (file)
index 0000000..aade1fd
--- /dev/null
@@ -0,0 +1,23 @@
+--TEST--
+Test strstr() function : usage variations - complex strings containing other than 7-bit chars
+--FILE--
+<?php
+$string = chr(0).chr(128).chr(129).chr(234).chr(235).chr(254).chr(255);
+$stringAsHex = bin2hex($string);
+echo "-- Positions of some chars in the string '$stringAsHex' are as follows --\n";
+echo bin2hex( chr(128) ) ." => ";
+var_dump( bin2hex( strstr($string, chr(128) ) ) );
+echo bin2hex( chr(255) ) ." => ";
+var_dump( bin2hex( strstr($string, chr(255) ) ) );
+echo bin2hex( chr(256) ) ." => ";
+var_dump( bin2hex( strstr($string, chr(256) ) ) );
+?>
+
+DONE
+--EXPECT--
+-- Positions of some chars in the string '008081eaebfeff' are as follows --
+80 => string(12) "8081eaebfeff"
+ff => string(2) "ff"
+00 => string(14) "008081eaebfeff"
+
+DONE