forked from azjezz/psl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathends_with.php
54 lines (48 loc) · 1.15 KB
/
ends_with.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
declare(strict_types=1);
namespace Psl\Str;
/**
* Returns whether the string ends with the given suffix.
*
* Example:
*
* Str\ends_with('Hello, World', 'd')
* => Bool(true)
*
* Str\ends_with('Hello, World', 'D')
* => Bool(false)
*
* Str\ends_with('Hello, World', 'world')
* => Bool(false)
*
* Str\ends_with('Hello, World', 'World')
* => Bool(true)
*
* Str\ends_with('Tunisia', 'e')
* => Bool(false)
*
* Str\ends_with('تونس', 'س')
* => Bool(true)
*
* Str\ends_with('تونس', 'ش')
* => Bool(false)
*
* @pure
*/
function ends_with(string $string, string $suffix, Encoding $encoding = Encoding::UTF_8): bool
{
if ($suffix === $string) {
return true;
}
$suffix_length = length($suffix, $encoding);
$total_length = length($string, $encoding);
if ($suffix_length > $total_length) {
return false;
}
/** @psalm-suppress MissingThrowsDocblock */
$position = search_last($string, $suffix, 0, $encoding);
if (null === $position) {
return false;
}
return $position + $suffix_length === $total_length;
}