Skip to content

Commit

Permalink
Fix #40: Use str_starts_with() and str_ends_with() if available
Browse files Browse the repository at this point in the history
viktorprogger authored Sep 29, 2020

Unverified

No user is associated with the committer email.
1 parent 35f0209 commit ae87766
Showing 3 changed files with 39 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Yii String Change Log

## 1.1.1 under development

- Enh #40: Use `str_starts_with()` and `str_ends_with()` if available (viktorprogger)

## 1.0.0 August 31, 2020

- Initial release.
16 changes: 16 additions & 0 deletions src/StringHelper.php
Original file line number Diff line number Diff line change
@@ -153,6 +153,14 @@ public static function replaceSubstring(string $string, string $replacement, int
*/
public static function startsWith(string $input, ?string $with): bool
{
if ($with === null) {
return true;
}

if (function_exists('\str_starts_with')) {
return \str_starts_with($input, $with);
}

$bytes = static::byteLength($with);
if ($bytes === 0) {
return true;
@@ -189,6 +197,14 @@ public static function startsWithIgnoringCase(string $input, ?string $with): boo
*/
public static function endsWith(string $input, ?string $with): bool
{
if ($with === null) {
return true;
}

if (function_exists('\str_ends_with')) {
return \str_ends_with($input, $with);
}

$bytes = static::byteLength($with);
if ($bytes === 0) {
return true;
41 changes: 19 additions & 22 deletions tests/StringHelperTest.php
Original file line number Diff line number Diff line change
@@ -116,10 +116,7 @@ public function testTruncateWords(): void
*/
public function testStartsWith(bool $result, string $string, ?string $with): void
{
// case sensitive version check
$this->assertSame($result, StringHelper::startsWith($string, $with));
// case insensitive version check
$this->assertSame($result, StringHelper::startsWith($string, $with, false));
}

/**
@@ -129,26 +126,26 @@ public function providerStartsWith(): array
{
return [
// positive check
[true, '', ''],
[true, '', null],
[true, 'string', ''],
[true, ' string', ' '],
[true, 'abc', 'abc'],
[true, 'Bürger', 'Bürger'],
[true, '我Я multibyte', '我Я'],
[true, 'Qנטשופ צרכנות', ''],
[true, 'ไทย.idn.icann.org', ''],
[true, '!?+', "\x21\x3F"],
[true, "\x21?+", '!?'],
'empty strings' => [true, '', ''],
'starts with null' => [true, '', null],
'starts with empty string' => [true, 'string', ''],
'starts with a space' => [true, ' string', ' '],
'fully identical strings' => [true, 'abc', 'abc'],
'fully identical multibyte strings' => [true, 'Bürger', 'Bürger'],
'starts with multibyte symbols' => [true, '我Я multibyte', '我Я'],
'starts with ascii and multibyte symbols' => [true, 'Qנטשופ צרכנות', ''],
'starts with multibyte symbol ไ' => [true, 'ไทย.idn.icann.org', ''],
'starts with hex code' => [true, '!?+', "\x21\x3F"],
'hex code starts with ascii symbols' => [true, "\x21?+", '!?'],
// false-positive check
[false, '', ' '],
[false, ' ', ' '],
[false, 'Abc', 'a'],
[false, 'Abc', 'Abcde'],
[false, 'abc', 'abe'],
[false, 'abc', 'b'],
[false, 'abc', 'c'],
[false, 'üЯ multibyte', 'Üя multibyte'],
'empty string and a space' => [false, '', ' '],
'a space and two spaces' => [false, ' ', ' '],
'case-sensitive check' => [false, 'Abc', 'a'],
'needle is longer' => [false, 'Abc', 'Abcde'],
'one of the symbols of the needle is not equal' => [false, 'abc', 'abe'],
'contains, but not starts with' => [false, 'abc', 'b'],
'contains, but not starts with again' => [false, 'abc', 'c'],
'case-sensitive check with multibyte symbol' => [false, 'üЯ multibyte', 'Üя multibyte'],
];
}

0 comments on commit ae87766

Please sign in to comment.