From c719dd60fb5d43c12b681010dbf88fb7bbc77005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Sun, 28 Apr 2019 09:28:22 +0200 Subject: [PATCH 1/5] Add startsWith method to Str class --- src/Str.php | 20 ++++++++++++++++++++ tests/StrTest.php | 13 +++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/Str.php b/src/Str.php index c165a51..cee4d33 100644 --- a/src/Str.php +++ b/src/Str.php @@ -662,6 +662,26 @@ public function contains(string $value): bool } } + /** + * Check if the current string starts with the given string + * + * @param string $value + * + * @return bool + */ + public function startsWith(string $value): bool + { + if ('' === $value) { + return false; + } + + try { + return 0 === $this->position($value); + } catch (SubstringException $e) { + return false; + } + } + /** * Quote regular expression characters * diff --git a/tests/StrTest.php b/tests/StrTest.php index 5ccb40d..d8b4621 100644 --- a/tests/StrTest.php +++ b/tests/StrTest.php @@ -795,6 +795,19 @@ public function testContains() $this->assertFalse($str->contains('baz')); } + public function testStartsWith() + { + $str = new S('foobar'); + + $this->assertTrue($str->startsWith('foo')); + $this->assertTrue($str->startsWith('foob')); + $this->assertTrue($str->startsWith('foobar')); + $this->assertFalse($str->startsWith('')); + $this->assertFalse($str->startsWith('bar')); + $this->assertFalse($str->startsWith('oobar')); + $this->assertFalse($str->startsWith('foobar ')); + } + public function testPregQuote() { $a = new S('foo#bar.*'); From 27adc198f4c21858cdc15f9c8978ea4f3491c1ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Sun, 28 Apr 2019 09:28:35 +0200 Subject: [PATCH 2/5] Add endsWith method to Str class --- src/Str.php | 16 ++++++++++++++++ tests/StrTest.php | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/Str.php b/src/Str.php index cee4d33..ccc10d3 100644 --- a/src/Str.php +++ b/src/Str.php @@ -682,6 +682,22 @@ public function startsWith(string $value): bool } } + /** + * Check if the current string ends with the given string + * + * @param string $value + * + * @return bool + */ + public function endsWith(string $value): bool + { + if ('' === $value) { + return false; + } + + return $value === (string) $this->substring(-strlen($value)); + } + /** * Quote regular expression characters * diff --git a/tests/StrTest.php b/tests/StrTest.php index d8b4621..867d8c3 100644 --- a/tests/StrTest.php +++ b/tests/StrTest.php @@ -808,6 +808,19 @@ public function testStartsWith() $this->assertFalse($str->startsWith('foobar ')); } + public function testEndsWith() + { + $str = new S('foobar'); + + $this->assertTrue($str->endsWith('bar')); + $this->assertTrue($str->endsWith('obar')); + $this->assertTrue($str->endsWith('foobar')); + $this->assertFalse($str->endsWith('')); + $this->assertFalse($str->endsWith('foo')); + $this->assertFalse($str->endsWith('fooba')); + $this->assertFalse($str->endsWith('xfoobar')); + } + public function testPregQuote() { $a = new S('foo#bar.*'); From 450a2d6de98ff4d577b7c6082aaa1be1d022fafc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Sun, 28 Apr 2019 13:30:26 +0200 Subject: [PATCH 3/5] Remove yoda condition --- src/Str.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Str.php b/src/Str.php index ccc10d3..5e31624 100644 --- a/src/Str.php +++ b/src/Str.php @@ -671,12 +671,12 @@ public function contains(string $value): bool */ public function startsWith(string $value): bool { - if ('' === $value) { + if ($value === '') { return false; } try { - return 0 === $this->position($value); + return $this->position($value) === 0; } catch (SubstringException $e) { return false; } @@ -691,7 +691,7 @@ public function startsWith(string $value): bool */ public function endsWith(string $value): bool { - if ('' === $value) { + if ($value === '') { return false; } From e2f7f4c9fca8beff3093279957cc76122f9d132b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Sun, 28 Apr 2019 13:32:25 +0200 Subject: [PATCH 4/5] Fix Str::endsWith method with multibyte encoding --- src/Str.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Str.php b/src/Str.php index 5e31624..53874c7 100644 --- a/src/Str.php +++ b/src/Str.php @@ -695,7 +695,7 @@ public function endsWith(string $value): bool return false; } - return $value === (string) $this->substring(-strlen($value)); + return (string) $this->takeEnd(self::of($value, $this->encoding)->length()) === $value; } /** From bc5a6b7f19b54a7a0383e96c63d779f5641f47d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Sun, 28 Apr 2019 13:36:36 +0200 Subject: [PATCH 5/5] Fix Str::startsWith and Str::endsWith with empty string parameter --- src/Str.php | 4 ++-- tests/StrTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Str.php b/src/Str.php index 53874c7..fdae7f6 100644 --- a/src/Str.php +++ b/src/Str.php @@ -672,7 +672,7 @@ public function contains(string $value): bool public function startsWith(string $value): bool { if ($value === '') { - return false; + return true; } try { @@ -692,7 +692,7 @@ public function startsWith(string $value): bool public function endsWith(string $value): bool { if ($value === '') { - return false; + return true; } return (string) $this->takeEnd(self::of($value, $this->encoding)->length()) === $value; diff --git a/tests/StrTest.php b/tests/StrTest.php index 867d8c3..368cc56 100644 --- a/tests/StrTest.php +++ b/tests/StrTest.php @@ -799,10 +799,10 @@ public function testStartsWith() { $str = new S('foobar'); + $this->assertTrue($str->startsWith('')); $this->assertTrue($str->startsWith('foo')); $this->assertTrue($str->startsWith('foob')); $this->assertTrue($str->startsWith('foobar')); - $this->assertFalse($str->startsWith('')); $this->assertFalse($str->startsWith('bar')); $this->assertFalse($str->startsWith('oobar')); $this->assertFalse($str->startsWith('foobar ')); @@ -812,10 +812,10 @@ public function testEndsWith() { $str = new S('foobar'); + $this->assertTrue($str->endsWith('')); $this->assertTrue($str->endsWith('bar')); $this->assertTrue($str->endsWith('obar')); $this->assertTrue($str->endsWith('foobar')); - $this->assertFalse($str->endsWith('')); $this->assertFalse($str->endsWith('foo')); $this->assertFalse($str->endsWith('fooba')); $this->assertFalse($str->endsWith('xfoobar'));