]> granicus.if.org Git - php/commitdiff
mb_strpos()/mb_stripos(): Add support for negative offset
authorFrancois Laupretre <francois@tekwire.net>
Tue, 5 Jan 2016 15:41:10 +0000 (16:41 +0100)
committerNikita Popov <nikic@php.net>
Wed, 9 Mar 2016 13:41:38 +0000 (14:41 +0100)
ext/mbstring/mbstring.c
ext/mbstring/tests/bug45923.phpt
ext/mbstring/tests/mb_stripos.phpt
ext/mbstring/tests/mb_stripos_variation3.phpt
ext/mbstring/tests/mb_stripos_variation5_Bug45923.phpt
ext/mbstring/tests/mb_strpos.phpt
ext/mbstring/tests/mb_strpos_variation3.phpt
ext/mbstring/tests/mb_strpos_variation5.phpt

index aba1607a6d253873cb12e4c107f9f0fa624dc07b..796c4828652703f9d8d9ee69f549d90286f317b4 100644 (file)
@@ -2262,7 +2262,7 @@ PHP_FUNCTION(mb_strlen)
 PHP_FUNCTION(mb_strpos)
 {
        int n, reverse = 0;
-       zend_long offset = 0;
+       zend_long offset = 0, slen;
        mbfl_string haystack, needle;
        char *enc_name = NULL;
        size_t enc_name_len, haystack_len, needle_len;
@@ -2297,7 +2297,11 @@ PHP_FUNCTION(mb_strpos)
                }
        }
 
-       if (offset < 0 || offset > mbfl_strlen(&haystack)) {
+       slen = mbfl_strlen(&haystack);
+       if (offset < 0) {
+               offset += slen;
+       }
+       if (offset < 0 || offset > slen) {
                php_error_docref(NULL, E_WARNING, "Offset not contained in string");
                RETURN_FALSE;
        }
@@ -4877,6 +4881,9 @@ MBSTRING_API int php_mb_stripos(int mode, const char *old_haystack, unsigned int
                                        break;
                                }
                        } else {
+                               if (offset < 0) {
+                                       offset += (long)haystack_char_len;
+                               }
                                if (offset < 0 || offset > haystack_char_len) {
                                        php_error_docref(NULL, E_WARNING, "Offset not contained in string");
                                        break;
index 7819b94e13c91e77e622a110e3e4f27d5af86578..41ffd70924ed72e6c2724ad1067f0e48cbc0d1ec 100644 (file)
@@ -73,17 +73,11 @@ bool(false)
 Warning: mb_strpos(): Offset not contained in string in %s on line %d
 bool(false)
 > Offset: -1
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
 bool(false)
 > Offset: -3
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+int(8)
 > Offset: -6
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+int(8)
 > Offset: -20
 
 Warning: mb_strpos(): Offset not contained in string in %s on line %d
@@ -133,17 +127,11 @@ bool(false)
 Warning: mb_stripos(): Offset not contained in string in %s on line %d
 bool(false)
 > Offset: -1
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
 bool(false)
 > Offset: -3
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+int(8)
 > Offset: -6
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+int(8)
 > Offset: -20
 
 Warning: mb_stripos(): Offset not contained in string in %s on line %d
index 4ea8cfa6d6c00d18d2a3277bec3d24f274c90076..82688722234148d4af32008f55eaaa28e1a88b41 100644 (file)
@@ -1,10 +1,7 @@
 --TEST--
 mb_stripos() 
 --SKIPIF--
-<?php
-extension_loaded('mbstring') or die('skip');
-function_exists('mb_stripos') or die("skip mb_stripos() is not available in this build");
-?>
+<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
 --FILE--
 <?php
 // TODO: Add more encodings
@@ -17,43 +14,62 @@ include_once('common.inc');
 // Test string
 $euc_jp = b'0123¤³¤Îʸ»úÎó¤ÏÆüËܸì¤Ç¤¹¡£EUC-JP¤ò»È¤Ã¤Æ¤¤¤Þ¤¹¡£0123ÆüËܸì¤ÏÌÌÅݽ­¤¤¡£';
 
+$slen = mb_strlen($euc_jp, 'EUC-JP');
+echo "String len: $slen\n";
+
 // EUC-JP - With encoding parameter
 mb_internal_encoding('UTF-8') or print("mb_internal_encoding() failed\n");
 
 echo  "== POSITIVE OFFSET ==\n";
-print  mb_stripos($euc_jp,b'ÆüËܸì', 0, 'EUC-JP') . "\n";
+
+print  mb_stripos($euc_jp, b'ÆüËܸì', 0, 'EUC-JP') . "\n";
 print  mb_stripos($euc_jp, b'0', 0,     'EUC-JP') . "\n";
 print  mb_stripos($euc_jp, 3, 0,       'EUC-JP') . "\n";
 print  mb_stripos($euc_jp, 0, 0,       'EUC-JP') . "\n";
-print  mb_stripos($euc_jp,b'ÆüËܸì', 15, 'EUC-JP') . "\n";
+print  mb_stripos($euc_jp, b'ÆüËܸì', 15, 'EUC-JP') . "\n";
 print  mb_stripos($euc_jp, b'0', 15,     'EUC-JP') . "\n";
 print  mb_stripos($euc_jp, 3, 15,       'EUC-JP') . "\n";
 print  mb_stripos($euc_jp, 0, 15,       'EUC-JP') . "\n";
 
+
 // Negative offset
-// Note: PHP Warning - offset is negative.
-// Note: For offset(-15). It does not return position of latter string. (ie the same result as -50)
 echo "== NEGATIVE OFFSET ==\n";
-$r = mb_stripos($euc_jp,b'ÆüËܸì', -15, 'EUC-JP');
-($r === FALSE) ? print "OK_NEGATIVE_OFFSET\n" : print "NG_NEGATIVE_OFFSET\n";
-$r = mb_stripos($euc_jp, b'0', -15,     'EUC-JP');
-($r === FALSE) ? print "OK_NEGATIVE_OFFSET\n" : print "NG_NEGATIVE_OFFSET\n";
-$r = mb_stripos($euc_jp, 3, -15,       'EUC-JP');
-($r === FALSE) ? print "OK_NEGATIVE_OFFSET\n" : print "NG_NEGATIVE_OFFSET\n";
-$r = mb_stripos($euc_jp, 0, -15,       'EUC-JP');
-($r === FALSE) ? print "OK_NEGATIVE_OFFSET\n" : print "NG_NEGATIVE_OFFSET\n";
-$r = mb_stripos($euc_jp,b'ÆüËܸì', -50, 'EUC-JP');
-($r === FALSE) ? print "OK_NEGATIVE_OFFSET\n" : print "NG_NEGATIVE_OFFSET\n";
+
+print mb_stripos($euc_jp, b'ÆüËܸì', -15, 'EUC-JP') . "\n";
+print mb_stripos($euc_jp, b'0', -15,     'EUC-JP') . "\n";
+print mb_stripos($euc_jp, 3, -15,       'EUC-JP') . "\n";
+print mb_stripos($euc_jp, 0, -15,       'EUC-JP') . "\n";
+print mb_stripos($euc_jp, 0, -43,       'EUC-JP') . "\n";
+
+
+// Invalid offset - should return false with warning
+print ("== INVALID OFFSET ==\n");
+
+$r =  mb_stripos($euc_jp, b'ÆüËܸì', 44, 'EUC-JP');
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
+$r =  mb_stripos($euc_jp, b'ÆüËܸì', 50, 'EUC-JP');
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
+$r =  mb_stripos($euc_jp, b'0', 50,     'EUC-JP');
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
+$r =  mb_stripos($euc_jp, 3, 50,       'EUC-JP');
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
+$r =   mb_stripos($euc_jp, 0, 50,       'EUC-JP');
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
+$r = mb_stripos($euc_jp, b'ÆüËܸì', -50, 'EUC-JP');
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
 $r = mb_stripos($euc_jp, b'0', -50,     'EUC-JP');
-($r === FALSE) ? print "OK_NEGATIVE_OFFSET\n" : print "NG_NEGATIVE_OFFSET\n";
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
 $r = mb_stripos($euc_jp, 3, -50,       'EUC-JP');
-($r === FALSE) ? print "OK_NEGATIVE_OFFSET\n" : print "NG_NEGATIVE_OFFSET\n";
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
 $r = mb_stripos($euc_jp, 0, -50,       'EUC-JP');
-($r === FALSE) ? print "OK_NEGATIVE_OFFSET\n" : print "NG_NEGATIVE_OFFSET\n";
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
+$r = mb_stripos($euc_jp, 0, -44,       'EUC-JP');
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
 
 // Out of range - should return false
 print ("== OUT OF RANGE ==\n");
-$r =  mb_stripos($euc_jp,b'ÆüËܸì', 40, 'EUC-JP');
+
+$r =  mb_stripos($euc_jp, b'ÆüËܸì', 40, 'EUC-JP');
 ($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
 $r =  mb_stripos($euc_jp, b'0', 40,     'EUC-JP');
 ($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
@@ -61,12 +77,19 @@ $r =  mb_stripos($euc_jp, 3, 40,       'EUC-JP');
 ($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
 $r =   mb_stripos($euc_jp, 0, 40,       'EUC-JP');
 ($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
-// Note: Returned NULL string
-// echo gettype($r). ' val '. $r ."\n"; 
+$r =  mb_stripos($euc_jp, b'ÆüËܸì', -3, 'EUC-JP');
+($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
+$r =  mb_stripos($euc_jp, b'0', -3,     'EUC-JP');
+($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
+$r =  mb_stripos($euc_jp, 3, -3,       'EUC-JP');
+($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
+$r =   mb_stripos($euc_jp, 0, -3,       'EUC-JP');
+($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
 
 
 // Non-existent
 echo "== NON-EXISTENT ==\n";
+
 $r = mb_stripos($euc_jp, b'´Ú¹ñ¸ì', 0, 'EUC-JP');
 ($r === FALSE) ? print "OK_STR\n"     : print "NG_STR\n";
 $r = mb_stripos($euc_jp, b"\n",     0, 'EUC-JP');
@@ -75,30 +98,32 @@ $r = mb_stripos($euc_jp, b"\n",     0, 'EUC-JP');
 
 // EUC-JP - No encoding parameter
 echo "== NO ENCODING PARAMETER ==\n";
+
 mb_internal_encoding('EUC-JP')  or print("mb_internal_encoding() failed\n");
 
-print  mb_stripos($euc_jp,b'ÆüËܸì', 0) . "\n";
+print  mb_stripos($euc_jp, b'ÆüËܸì', 0) . "\n";
 print  mb_stripos($euc_jp, b'0', 0) . "\n";
 print  mb_stripos($euc_jp, 3, 0) . "\n";
 print  mb_stripos($euc_jp, 0, 0) . "\n";
 
-$r = mb_stripos($euc_jp,b'´Ú¹ñ¸ì', 0);
+$r = mb_stripos($euc_jp, b'´Ú¹ñ¸ì', 0);
 ($r === FALSE) ? print "OK_STR\n"     : print "NG_STR\n";
-$r = mb_stripos($euc_jp,b"\n", 0);
+$r = mb_stripos($euc_jp, b"\n", 0);
 ($r === FALSE) ? print "OK_NEWLINE\n" : print "NG_NEWLINE\n";
 
 // EUC-JP - No offset and encoding parameter
 echo "== NO OFFSET AND ENCODING PARAMETER ==\n";
+
 mb_internal_encoding('EUC-JP')  or print("mb_internal_encoding() failed\n");
 
-print  mb_stripos($euc_jp,b'ÆüËܸì') . "\n";
+print  mb_stripos($euc_jp, b'ÆüËܸì') . "\n";
 print  mb_stripos($euc_jp, b'0') . "\n";
 print  mb_stripos($euc_jp, 3) . "\n";
 print  mb_stripos($euc_jp, 0) . "\n";
 
-$r = mb_stripos($euc_jp,b'´Ú¹ñ¸ì');
+$r = mb_stripos($euc_jp, b'´Ú¹ñ¸ì');
 ($r === FALSE) ? print "OK_STR\n"     : print "NG_STR\n";
-$r = mb_stripos($euc_jp,b"\n");
+$r = mb_stripos($euc_jp, b"\n");
 ($r === FALSE) ? print "OK_NEWLINE\n" : print "NG_NEWLINE\n";
 
 
@@ -113,11 +138,10 @@ $r = mb_stripos($euc_jp, $t_obj, 'EUC-JP');
 ($r === NULL) ? print("OK_OBJECT\n") : print("NG_OBJECT\n");
 $r = mb_stripos($euc_jp, $t_obj, 'BAD_ENCODING');
 ($r === NULL) ? print("OK_BAD_ENCODING\n") : print("NG_BAD_ENCODING\n");
-
-
 ?>
-
+==DONE==
 --EXPECT--
+String len: 43
 == POSITIVE OFFSET ==
 10
 0
@@ -128,27 +152,41 @@ $r = mb_stripos($euc_jp, $t_obj, 'BAD_ENCODING');
 33
 30
 == NEGATIVE OFFSET ==
+34
+30
+33
+30
+0
+== INVALID OFFSET ==
 ERR: Warning
-OK_NEGATIVE_OFFSET
+OK_INVALID_OFFSET
 ERR: Warning
-OK_NEGATIVE_OFFSET
+OK_INVALID_OFFSET
 ERR: Warning
-OK_NEGATIVE_OFFSET
+OK_INVALID_OFFSET
 ERR: Warning
-OK_NEGATIVE_OFFSET
+OK_INVALID_OFFSET
 ERR: Warning
-OK_NEGATIVE_OFFSET
+OK_INVALID_OFFSET
 ERR: Warning
-OK_NEGATIVE_OFFSET
+OK_INVALID_OFFSET
 ERR: Warning
-OK_NEGATIVE_OFFSET
+OK_INVALID_OFFSET
 ERR: Warning
-OK_NEGATIVE_OFFSET
+OK_INVALID_OFFSET
+ERR: Warning
+OK_INVALID_OFFSET
+ERR: Warning
+OK_INVALID_OFFSET
 == OUT OF RANGE ==
 OK_OUT_RANGE
 OK_OUT_RANGE
 OK_OUT_RANGE
 OK_OUT_RANGE
+OK_OUT_RANGE
+OK_OUT_RANGE
+OK_OUT_RANGE
+OK_OUT_RANGE
 == NON-EXISTENT ==
 OK_STR
 OK_NEWLINE
@@ -175,4 +213,4 @@ ERR: Warning
 OK_OBJECT
 ERR: Warning
 OK_BAD_ENCODING
-
+==DONE==
index 21a12937864fc2a148f012fb2ecb75a3cfe67fe2..69c4a9e53b0dddcde5e52239993d924f189b5046 100644 (file)
@@ -8,10 +8,9 @@ if (PHP_INT_SIZE != 8) die('skip 64-bit only');
 ?>
 --FILE--
 <?php
-/* Prototype  : int mb_stripos(string haystack, string needle [, int offset [, string encoding]])
- * Description: Finds position of first occurrence of a string within another, case insensitive 
+/* Prototype  : int mb_stripos(string $haystack, string $needle [, int $offset [, string $encoding]])
+ * Description: Find position of first occurrence of a string within another, case insensitive
  * Source code: ext/mbstring/mbstring.c
- * Alias to functions: 
  */
 
 /*
@@ -52,45 +51,47 @@ $inputs = array(
 /*1*/  0,
        1,
        12345,
+          -5,
        -2345,
 
        // float data
-/*5*/  10.5,
-       -10.5,
+/*6*/  10.5,
+       -5.5,
+          -100.5,
        12.3456789000e10,
        12.3456789000E-10,
        .5,
 
        // null data
-/*10*/ NULL,
+/*12*/ NULL,
        null,
 
        // boolean data
-/*12*/ true,
+/*14*/ true,
        false,
        TRUE,
        FALSE,
        
        // empty data
-/*16*/ "",
+/*18*/ "",
        '',
 
        // string data
-/*18*/ "string",
+/*20*/ "string",
        'string',
        $heredoc,
        
        // object data
-/*21*/ new classA(),
+/*23*/ new classA(),
 
        // undefined data
-/*22*/ @$undefined_var,
+/*24*/ @$undefined_var,
 
        // unset data
-/*23*/ @$unset_var,
+/*25*/ @$unset_var,
 
        // resource variable
-/*24*/ $fp
+/*26*/ $fp
 );
 
 // loop through each element of $inputs to check the behavior of mb_stripos()
@@ -120,28 +121,28 @@ Warning: mb_stripos(): Offset not contained in string in %s on line %d
 bool(false)
 
 -- Iteration 4 --
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+int(8)
 
 -- Iteration 5 --
+
+Warning: mb_stripos(): Offset not contained in string in %s on line %d
 bool(false)
 
 -- Iteration 6 --
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
 bool(false)
 
 -- Iteration 7 --
+int(8)
+
+-- Iteration 8 --
 
 Warning: mb_stripos(): Offset not contained in string in %s on line %d
 bool(false)
 
--- Iteration 8 --
-int(8)
-
 -- Iteration 9 --
-int(8)
+
+Warning: mb_stripos(): Offset not contained in string in %s on line %d
+bool(false)
 
 -- Iteration 10 --
 int(8)
@@ -162,42 +163,48 @@ int(8)
 int(8)
 
 -- Iteration 16 --
+int(8)
+
+-- Iteration 17 --
+int(8)
+
+-- Iteration 18 --
 
 Warning: mb_stripos() expects parameter 3 to be integer, string given in %s on line %d
 NULL
 
--- Iteration 17 --
+-- Iteration 19 --
 
 Warning: mb_stripos() expects parameter 3 to be integer, string given in %s on line %d
 NULL
 
--- Iteration 18 --
+-- Iteration 20 --
 
 Warning: mb_stripos() expects parameter 3 to be integer, string given in %s on line %d
 NULL
 
--- Iteration 19 --
+-- Iteration 21 --
 
 Warning: mb_stripos() expects parameter 3 to be integer, string given in %s on line %d
 NULL
 
--- Iteration 20 --
+-- Iteration 22 --
 
 Warning: mb_stripos() expects parameter 3 to be integer, string given in %s on line %d
 NULL
 
--- Iteration 21 --
+-- Iteration 23 --
 
 Warning: mb_stripos() expects parameter 3 to be integer, object given in %s on line %d
 NULL
 
--- Iteration 22 --
+-- Iteration 24 --
 int(8)
 
--- Iteration 23 --
+-- Iteration 25 --
 int(8)
 
--- Iteration 24 --
+-- Iteration 26 --
 
 Warning: mb_stripos() expects parameter 3 to be integer, resource given in %s on line %d
 NULL
index fbe4937ac2b172c0218625ab7e6a419ed1f73c8a..8ffcae5c0f9595f361b5d6b9c3d4175b8791fc6a 100644 (file)
@@ -7,10 +7,9 @@ function_exists('mb_stripos') or die("skip mb_stripos() is not available in this
 ?>
 --FILE--
 <?php
-/* Prototype  : int mb_stripos(string haystack, string needle [, int offset [, string encoding]])
- * Description: Finds position of first occurrence of a string within another, case insensitive 
+/* Prototype  : int mb_stripos(string $haystack, string $needle [, int $offset [, string $encoding]])
+ * Description: Find position of first occurrence of a string within another, case insensitive
  * Source code: ext/mbstring/mbstring.c
- * Alias to functions: 
  */
 
 /*
@@ -34,7 +33,7 @@ $needle_mb = base64_decode('44CC');
  * mb_stripos 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(mb_stripos($string_ascii, $needle_ascii, $i));
@@ -48,7 +47,7 @@ echo "Done";
 --EXPECTF--
 *** Testing mb_stripos() : usage variations ***
 
-**-- Offset is: -10 --**
+**-- Offset is: -30 --**
 -- ASCII String --
 
 Warning: mb_stripos(): Offset not contained in string in %s on line %d
@@ -58,6 +57,18 @@ bool(false)
 Warning: mb_stripos(): 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)
@@ -116,4 +127,3 @@ bool(false)
 Warning: mb_stripos(): Offset not contained in string in %s on line %d
 bool(false)
 Done
-
index e1222ca6dd96369fbd4286fbaa1a6c45621f29b4..364bc7cc1a0fcdd76ce956b2b60c455b7c84e74f 100644 (file)
@@ -14,10 +14,14 @@ include_once('common.inc');
 // Test string
 $euc_jp = b'0123¤³¤Îʸ»úÎó¤ÏÆüËܸì¤Ç¤¹¡£EUC-JP¤ò»È¤Ã¤Æ¤¤¤Þ¤¹¡£0123ÆüËܸì¤ÏÌÌÅݽ­¤¤¡£';
 
+$slen = mb_strlen($euc_jp, 'EUC-JP');
+echo "String len: $slen\n";
+
 // EUC-JP - With encoding parameter
 mb_internal_encoding('UTF-8') or print("mb_internal_encoding() failed\n");
 
 echo  "== POSITIVE OFFSET ==\n";
+
 print  mb_strpos($euc_jp, b'ÆüËܸì', 0, 'EUC-JP') . "\n";
 print  mb_strpos($euc_jp, b'0', 0,     'EUC-JP') . "\n";
 print  mb_strpos($euc_jp, 3, 0,       'EUC-JP') . "\n";
@@ -27,29 +31,44 @@ print  mb_strpos($euc_jp, b'0', 15,     'EUC-JP') . "\n";
 print  mb_strpos($euc_jp, 3, 15,       'EUC-JP') . "\n";
 print  mb_strpos($euc_jp, 0, 15,       'EUC-JP') . "\n";
 
+
 // Negative offset
-// Note: PHP Warning - offset is negative.
-// Note: For offset(-15). It does not return position of latter string. (ie the same result as -50)
 echo "== NEGATIVE OFFSET ==\n";
-$r = mb_strpos($euc_jp, b'ÆüËܸì', -15, 'EUC-JP');
-($r === FALSE) ? print "OK_NEGATIVE_OFFSET\n" : print "NG_NEGATIVE_OFFSET\n";
-$r = mb_strpos($euc_jp, b'0', -15,     'EUC-JP');
-($r === FALSE) ? print "OK_NEGATIVE_OFFSET\n" : print "NG_NEGATIVE_OFFSET\n";
-$r = mb_strpos($euc_jp, 3, -15,       'EUC-JP');
-($r === FALSE) ? print "OK_NEGATIVE_OFFSET\n" : print "NG_NEGATIVE_OFFSET\n";
-$r = mb_strpos($euc_jp, 0, -15,       'EUC-JP');
-($r === FALSE) ? print "OK_NEGATIVE_OFFSET\n" : print "NG_NEGATIVE_OFFSET\n";
+
+print mb_strpos($euc_jp, b'ÆüËܸì', -15, 'EUC-JP') . "\n";
+print mb_strpos($euc_jp, b'0', -15,     'EUC-JP') . "\n";
+print mb_strpos($euc_jp, 3, -15,       'EUC-JP') . "\n";
+print mb_strpos($euc_jp, 0, -15,       'EUC-JP') . "\n";
+print mb_strpos($euc_jp, 0, -43,       'EUC-JP') . "\n";
+
+
+// Invalid offset - should return false with warning
+print ("== INVALID OFFSET ==\n");
+
+$r =  mb_strpos($euc_jp, b'ÆüËܸì', 44, 'EUC-JP');
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
+$r =  mb_strpos($euc_jp, b'ÆüËܸì', 50, 'EUC-JP');
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
+$r =  mb_strpos($euc_jp, b'0', 50,     'EUC-JP');
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
+$r =  mb_strpos($euc_jp, 3, 50,       'EUC-JP');
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
+$r =   mb_strpos($euc_jp, 0, 50,       'EUC-JP');
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
 $r = mb_strpos($euc_jp, b'ÆüËܸì', -50, 'EUC-JP');
-($r === FALSE) ? print "OK_NEGATIVE_OFFSET\n" : print "NG_NEGATIVE_OFFSET\n";
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
 $r = mb_strpos($euc_jp, b'0', -50,     'EUC-JP');
-($r === FALSE) ? print "OK_NEGATIVE_OFFSET\n" : print "NG_NEGATIVE_OFFSET\n";
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
 $r = mb_strpos($euc_jp, 3, -50,       'EUC-JP');
-($r === FALSE) ? print "OK_NEGATIVE_OFFSET\n" : print "NG_NEGATIVE_OFFSET\n";
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
 $r = mb_strpos($euc_jp, 0, -50,       'EUC-JP');
-($r === FALSE) ? print "OK_NEGATIVE_OFFSET\n" : print "NG_NEGATIVE_OFFSET\n";
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
+$r = mb_strpos($euc_jp, 0, -44,       'EUC-JP');
+($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
 
 // Out of range - should return false
 print ("== OUT OF RANGE ==\n");
+
 $r =  mb_strpos($euc_jp, b'ÆüËܸì', 40, 'EUC-JP');
 ($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
 $r =  mb_strpos($euc_jp, b'0', 40,     'EUC-JP');
@@ -58,12 +77,19 @@ $r =  mb_strpos($euc_jp, 3, 40,       'EUC-JP');
 ($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
 $r =   mb_strpos($euc_jp, 0, 40,       'EUC-JP');
 ($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
-// Note: Returned NULL string
-// echo gettype($r). ' val '. $r ."\n"; 
+$r =  mb_strpos($euc_jp, b'ÆüËܸì', -3, 'EUC-JP');
+($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
+$r =  mb_strpos($euc_jp, b'0', -3,     'EUC-JP');
+($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
+$r =  mb_strpos($euc_jp, 3, -3,       'EUC-JP');
+($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
+$r =   mb_strpos($euc_jp, 0, -3,       'EUC-JP');
+($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
 
 
 // Non-existent
 echo "== NON-EXISTENT ==\n";
+
 $r = mb_strpos($euc_jp, b'´Ú¹ñ¸ì', 0, 'EUC-JP');
 ($r === FALSE) ? print "OK_STR\n"     : print "NG_STR\n";
 $r = mb_strpos($euc_jp, b"\n",     0, 'EUC-JP');
@@ -72,6 +98,7 @@ $r = mb_strpos($euc_jp, b"\n",     0, 'EUC-JP');
 
 // EUC-JP - No encoding parameter
 echo "== NO ENCODING PARAMETER ==\n";
+
 mb_internal_encoding('EUC-JP')  or print("mb_internal_encoding() failed\n");
 
 print  mb_strpos($euc_jp, b'ÆüËܸì', 0) . "\n";
@@ -86,6 +113,7 @@ $r = mb_strpos($euc_jp, b"\n", 0);
 
 // EUC-JP - No offset and encoding parameter
 echo "== NO OFFSET AND ENCODING PARAMETER ==\n";
+
 mb_internal_encoding('EUC-JP')  or print("mb_internal_encoding() failed\n");
 
 print  mb_strpos($euc_jp, b'ÆüËܸì') . "\n";
@@ -110,11 +138,10 @@ $r = mb_strpos($euc_jp, $t_obj, 'EUC-JP');
 ($r === NULL) ? print("OK_OBJECT\n") : print("NG_OBJECT\n");
 $r = mb_strpos($euc_jp, $t_obj, 'BAD_ENCODING');
 ($r === NULL) ? print("OK_BAD_ENCODING\n") : print("NG_BAD_ENCODING\n");
-
-
 ?>
-
+==DONE==
 --EXPECT--
+String len: 43
 == POSITIVE OFFSET ==
 10
 0
@@ -125,27 +152,41 @@ $r = mb_strpos($euc_jp, $t_obj, 'BAD_ENCODING');
 33
 30
 == NEGATIVE OFFSET ==
+34
+30
+33
+30
+0
+== INVALID OFFSET ==
+ERR: Warning
+OK_INVALID_OFFSET
 ERR: Warning
-OK_NEGATIVE_OFFSET
+OK_INVALID_OFFSET
 ERR: Warning
-OK_NEGATIVE_OFFSET
+OK_INVALID_OFFSET
 ERR: Warning
-OK_NEGATIVE_OFFSET
+OK_INVALID_OFFSET
 ERR: Warning
-OK_NEGATIVE_OFFSET
+OK_INVALID_OFFSET
 ERR: Warning
-OK_NEGATIVE_OFFSET
+OK_INVALID_OFFSET
 ERR: Warning
-OK_NEGATIVE_OFFSET
+OK_INVALID_OFFSET
 ERR: Warning
-OK_NEGATIVE_OFFSET
+OK_INVALID_OFFSET
 ERR: Warning
-OK_NEGATIVE_OFFSET
+OK_INVALID_OFFSET
+ERR: Warning
+OK_INVALID_OFFSET
 == OUT OF RANGE ==
 OK_OUT_RANGE
 OK_OUT_RANGE
 OK_OUT_RANGE
 OK_OUT_RANGE
+OK_OUT_RANGE
+OK_OUT_RANGE
+OK_OUT_RANGE
+OK_OUT_RANGE
 == NON-EXISTENT ==
 OK_STR
 OK_NEWLINE
@@ -172,4 +213,4 @@ ERR: Warning
 OK_OBJECT
 ERR: Warning
 OK_BAD_ENCODING
-
+==DONE==
index 8079a190212898ceaef07f4a7a368db006978e56..f30b70818324d36242183963df69aa09b073c011 100644 (file)
@@ -51,45 +51,47 @@ $inputs = array(
 /*1*/  0,
        1,
        12345,
+          -5,
        -2345,
 
        // float data
-/*5*/  10.5,
-       -10.5,
+/*6*/  10.5,
+       -5.5,
+          -100.5,
        12.3456789000e10,
        12.3456789000E-10,
        .5,
 
        // null data
-/*10*/ NULL,
+/*12*/ NULL,
        null,
 
        // boolean data
-/*12*/ true,
+/*14*/ true,
        false,
        TRUE,
        FALSE,
        
        // empty data
-/*16*/ "",
+/*18*/ "",
        '',
 
        // string data
-/*18*/ "string",
+/*20*/ "string",
        'string',
        $heredoc,
        
        // object data
-/*21*/ new classA(),
+/*23*/ new classA(),
 
        // undefined data
-/*22*/ @$undefined_var,
+/*24*/ @$undefined_var,
 
        // unset data
-/*23*/ @$unset_var,
+/*25*/ @$unset_var,
 
        // resource variable
-/*24*/ $fp
+/*26*/ $fp
 );
 
 // loop through each element of $inputs to check the behavior of mb_strpos()
@@ -119,28 +121,28 @@ Warning: mb_strpos(): Offset not contained in string in %s on line %d
 bool(false)
 
 -- Iteration 4 --
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+int(8)
 
 -- Iteration 5 --
+
+Warning: mb_strpos(): Offset not contained in string in %s on line %d
 bool(false)
 
 -- Iteration 6 --
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
 bool(false)
 
 -- Iteration 7 --
+int(8)
+
+-- Iteration 8 --
 
 Warning: mb_strpos(): Offset not contained in string in %s on line %d
 bool(false)
 
--- Iteration 8 --
-int(8)
-
 -- Iteration 9 --
-int(8)
+
+Warning: mb_strpos(): Offset not contained in string in %s on line %d
+bool(false)
 
 -- Iteration 10 --
 int(8)
@@ -161,42 +163,48 @@ int(8)
 int(8)
 
 -- Iteration 16 --
+int(8)
+
+-- Iteration 17 --
+int(8)
+
+-- Iteration 18 --
 
 Warning: mb_strpos() expects parameter 3 to be integer, string given in %s on line %d
 NULL
 
--- Iteration 17 --
+-- Iteration 19 --
 
 Warning: mb_strpos() expects parameter 3 to be integer, string given in %s on line %d
 NULL
 
--- Iteration 18 --
+-- Iteration 20 --
 
 Warning: mb_strpos() expects parameter 3 to be integer, string given in %s on line %d
 NULL
 
--- Iteration 19 --
+-- Iteration 21 --
 
 Warning: mb_strpos() expects parameter 3 to be integer, string given in %s on line %d
 NULL
 
--- Iteration 20 --
+-- Iteration 22 --
 
 Warning: mb_strpos() expects parameter 3 to be integer, string given in %s on line %d
 NULL
 
--- Iteration 21 --
+-- Iteration 23 --
 
 Warning: mb_strpos() expects parameter 3 to be integer, object given in %s on line %d
 NULL
 
--- Iteration 22 --
+-- Iteration 24 --
 int(8)
 
--- Iteration 23 --
+-- Iteration 25 --
 int(8)
 
--- Iteration 24 --
+-- Iteration 26 --
 
 Warning: mb_strpos() expects parameter 3 to be integer, resource given in %s on line %d
 NULL
index 7a9604abeffd6950117380f74073c5c77adc5e49..23bfa22b61eda7a6e14f1a7b31216b8f1b09cb60 100644 (file)
@@ -33,7 +33,7 @@ $needle_mb = base64_decode('44CC');
  * mb_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(mb_strpos($string_ascii, $needle_ascii, $i));
@@ -47,7 +47,7 @@ echo "Done";
 --EXPECTF--
 *** Testing mb_strpos() : usage variations ***
 
-**-- Offset is: -10 --**
+**-- Offset is: -30 --**
 -- ASCII String --
 
 Warning: mb_strpos(): Offset not contained in string in %s on line %d
@@ -57,6 +57,18 @@ bool(false)
 Warning: mb_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)