diff --git a/src/Patchwork/Utf8.php b/src/Patchwork/Utf8.php index 585f063..4363a08 100644 --- a/src/Patchwork/Utf8.php +++ b/src/Patchwork/Utf8.php @@ -235,7 +235,9 @@ public static function strlen($s) } public static function strpos($s, $needle, $offset = 0) { - return grapheme_strpos($s, $needle, $offset); + // ignore invalid negative offset to keep compatility + // with php < 5.5.35, < 5.6.21, < 7.0.6 + return grapheme_strpos($s, $needle, $offset > 0 ? $offset : 0); } public static function strrpos($s, $needle, $offset = 0) { diff --git a/tests/PHP/Shim/IntlTest.php b/tests/PHP/Shim/IntlTest.php index 1d4d4a7..83b3aa8 100644 --- a/tests/PHP/Shim/IntlTest.php +++ b/tests/PHP/Shim/IntlTest.php @@ -122,7 +122,7 @@ public function testGrapheme_strpos() $this->assertSame(false, grapheme_strpos('abc', '')); $this->assertSame(false, grapheme_strpos('abc', 'd')); $this->assertSame(false, grapheme_strpos('abc', 'a', 3)); - $this->assertSame(0, grapheme_strpos('abc', 'a', -1)); + $this->assertSame(0, grapheme_strpos('abc', 'a', 0)); $this->assertSame(1, grapheme_strpos('한국어', '국')); $this->assertSame(3, grapheme_stripos('DÉJÀ', 'à')); $this->assertSame(false, grapheme_strrpos('한국어', '')); diff --git a/tests/Utf8/HhvmTest.php b/tests/Utf8/HhvmTest.php index 8a77da0..4eb6866 100644 --- a/tests/Utf8/HhvmTest.php +++ b/tests/Utf8/HhvmTest.php @@ -12,7 +12,8 @@ public function test1() public function test2() { // Negative offset are not allowed but native PHP silently casts them to zero - $this->assertSame(0, grapheme_strpos('abc', 'a', -1)); + // Starting with 5.5.35, 5.6.21, 7.0.6, PHP refuse them + $this->assertSame(0, grapheme_strpos('abc', 'a', 0)); } public function test3()