Skip to content

Commit

Permalink
Strings::indexOf() fixed empty $haystack and negative $nth [Closes #271]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 22, 2021
1 parent 2ca60ae commit 075301b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Utils/Strings.php
Expand Up @@ -456,6 +456,8 @@ private static function pos(string $haystack, string $needle, int $nth = 1): ?in
$len = strlen($haystack);
if ($needle === '') {
return $len;
} elseif ($len === 0) {
return null;
}
$pos = $len - 1;
while (($pos = strrpos($haystack, $needle, $pos - $len)) !== false && ++$nth) {
Expand Down
2 changes: 2 additions & 0 deletions tests/Utils/Strings.indexOf().phpt
Expand Up @@ -31,6 +31,8 @@ test('', function () {
Assert::null(Strings::indexOf($foo, 'not-in-string'));
Assert::null(Strings::indexOf($foo, 'b', -2));
Assert::null(Strings::indexOf($foo, 'b', 2));
Assert::null(Strings::indexOf('', 'a', 1));
Assert::null(Strings::indexOf('', 'a', -1));
});


Expand Down

0 comments on commit 075301b

Please sign in to comment.