Skip to content

[String] Add the reverse() method #35091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/String/AbstractString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/String/AbstractUnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/String/ByteString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/String/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.1.0
-----

* Added the `AbstractString::reverse()` method.

5.0.0
-----

Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
];
}
}
12 changes: 12 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractUnicodeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,4 +568,16 @@ public static function providePadStart(): array
]
);
}

public static function provideReverse()
{
return array_merge(
parent::provideReverse(),
[
['äuß⭐erst', 'tsre⭐ßuä'],
['漢字ーユニコードéèΣσς', 'ςσΣèéドーコニユー字漢'],
['नमस्ते', 'तेस्मन'],
]
);
}
}