-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathsearch.php
39 lines (32 loc) · 1.01 KB
/
search.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
declare(strict_types=1);
namespace Psl\Str\Grapheme;
use Psl\Str;
use function grapheme_strpos;
/**
* Returns the first position of the 'needle' string in the 'haystack' string
* grapheme units, or null if it isn't found.
*
* An optional offset determines where in the haystack the search begins.
*
* If the offset is negative, the search will begin that many characters from the end
* of the string.
*
* @pure
*
* @throws Str\Exception\OutOfBoundsException If $offset is out-of-bounds.
* @throws Str\Exception\InvalidArgumentException If $haystack is not made of grapheme clusters.
*
* @return null|int<0, max>
*
* @mago-expect best-practices/no-boolean-literal-comparison
*/
function search(string $haystack, string $needle, int $offset = 0): null|int
{
if ('' === $needle) {
return null;
}
$offset = Str\Internal\validate_offset($offset, length($haystack));
/** @var null|int<0, max> */
return false === ($pos = grapheme_strpos($haystack, $needle, $offset)) ? null : $pos;
}