Skip to content
This repository has been archived by the owner on Jan 8, 2021. It is now read-only.

Commit

Permalink
Fix mb_strrpos shim with negative offset
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Jun 25, 2015
1 parent 8acabb3 commit fca739c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Patchwork/PHP/Shim/Mbstring.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,12 @@ public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = I
if ($offset != (int) $offset) {
$offset = 0;
} elseif ($offset = (int) $offset) {
$haystack = self::mb_substr($haystack, $offset, 2147483647, $encoding);
if ($offset < 0) {
$haystack = self::mb_substr($haystack, 0, $offset, $encoding);
$offset = 0;
} else {
$haystack = self::mb_substr($haystack, $offset, 2147483647, $encoding);
}
}

$pos = iconv_strrpos($haystack, $needle, $encoding.'//IGNORE');
Expand Down
2 changes: 2 additions & 0 deletions tests/Patchwork/Tests/PHP/Shim/MbstringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public function testmb_strpos()
$this->assertSame(3, mb_strripos('DÉJÀ', 'à'));
$this->assertSame(1, mb_stripos('aςσb', 'ΣΣ'));
$this->assertSame(1, mb_strripos('aςσb', 'ΣΣ'));
$this->assertSame(3, mb_strrpos('ababab', 'b', -2));

$this->assertSame(false, @p::mb_strpos('abc', ''));
$this->assertSame(false, @p::mb_strpos('abc', 'a', -1));
Expand All @@ -131,6 +132,7 @@ public function testmb_strpos()
$this->assertSame(3, p::mb_strripos('DÉJÀ', 'à'));
$this->assertSame(1, p::mb_stripos('aςσb', 'ΣΣ'));
$this->assertSame(1, p::mb_strripos('aςσb', 'ΣΣ'));
$this->assertSame(3, p::mb_strrpos('ababab', 'b', -2));
}

/**
Expand Down

0 comments on commit fca739c

Please sign in to comment.