From c369598b05e95380d173027c1d458e4ab33dcabb Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Mon, 23 Dec 2019 17:36:34 +0100 Subject: [PATCH] [String] Add the reverse method --- .../Component/String/AbstractString.php | 5 +++++ .../String/AbstractUnicodeString.php | 11 +++++++++++ src/Symfony/Component/String/ByteString.php | 11 +++++++++++ src/Symfony/Component/String/CHANGELOG.md | 5 +++++ .../String/Tests/AbstractAsciiTestCase.php | 19 +++++++++++++++++++ .../String/Tests/AbstractUnicodeTestCase.php | 12 ++++++++++++ 6 files changed, 63 insertions(+) diff --git a/src/Symfony/Component/String/AbstractString.php b/src/Symfony/Component/String/AbstractString.php index e56697e391549..ec981176d25d8 100644 --- a/src/Symfony/Component/String/AbstractString.php +++ b/src/Symfony/Component/String/AbstractString.php @@ -467,6 +467,11 @@ abstract public function replace(string $from, string $to): self; */ abstract public function replaceMatches(string $fromRegexp, $to): self; + /** + * @return static + */ + abstract public function reverse(): self; + /** * @return static */ diff --git a/src/Symfony/Component/String/AbstractUnicodeString.php b/src/Symfony/Component/String/AbstractUnicodeString.php index 19ad5e8939d95..fed79e2f536a0 100644 --- a/src/Symfony/Component/String/AbstractUnicodeString.php +++ b/src/Symfony/Component/String/AbstractUnicodeString.php @@ -342,6 +342,17 @@ public function replaceMatches(string $fromRegexp, $to): parent return $str; } + /** + * {@inheritdoc} + */ + public function reverse(): parent + { + $str = clone $this; + $str->string = implode('', array_reverse(preg_split('/(\X)/u', $str->string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY))); + + return $str; + } + public function snake(): parent { $str = $this->camel()->title(); diff --git a/src/Symfony/Component/String/ByteString.php b/src/Symfony/Component/String/ByteString.php index d831940e16b07..ab44882ceade1 100644 --- a/src/Symfony/Component/String/ByteString.php +++ b/src/Symfony/Component/String/ByteString.php @@ -303,6 +303,17 @@ public function replaceMatches(string $fromRegexp, $to): parent return $str; } + /** + * {@inheritdoc} + */ + public function reverse(): parent + { + $str = clone $this; + $str->string = strrev($str->string); + + return $str; + } + public function slice(int $start = 0, int $length = null): parent { $str = clone $this; diff --git a/src/Symfony/Component/String/CHANGELOG.md b/src/Symfony/Component/String/CHANGELOG.md index 28b9c6254196b..050c734f8982d 100644 --- a/src/Symfony/Component/String/CHANGELOG.md +++ b/src/Symfony/Component/String/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +5.1.0 +----- + + * Added the `AbstractString::reverse()` method. + 5.0.0 ----- diff --git a/src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php b/src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php index 934b29c1486bd..c7be84faabef3 100644 --- a/src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php +++ b/src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php @@ -1377,4 +1377,23 @@ public function testToString() self::assertSame('foobar', $instance->toString()); } + + /** + * @dataProvider provideReverse + */ + public function testReverse(string $expected, string $origin) + { + $instance = static::createFromString($origin)->reverse(); + + $this->assertEquals(static::createFromString($expected), $instance); + } + + public static function provideReverse() + { + return [ + ['', ''], + ['oof', 'foo'], + ["\n!!!\tTAERG SI ynofmyS ", " Symfony IS GREAT\t!!!\n"], + ]; + } } diff --git a/src/Symfony/Component/String/Tests/AbstractUnicodeTestCase.php b/src/Symfony/Component/String/Tests/AbstractUnicodeTestCase.php index 173abd026a026..84e64b02e9f6a 100644 --- a/src/Symfony/Component/String/Tests/AbstractUnicodeTestCase.php +++ b/src/Symfony/Component/String/Tests/AbstractUnicodeTestCase.php @@ -568,4 +568,16 @@ public static function providePadStart(): array ] ); } + + public static function provideReverse() + { + return array_merge( + parent::provideReverse(), + [ + ['äuß⭐erst', 'tsre⭐ßuä'], + ['漢字ーユニコードéèΣσς', 'ςσΣèéドーコニユー字漢'], + ['नमस्ते', 'तेस्मन'], + ] + ); + } }