Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nicolas-grekas merged 1 commit intosymfony:masterfromfancyweb:string-reverse
Dec 26, 2019
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionssrc/Symfony/Component/String/AbstractString.php
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/Symfony/Component/String/AbstractUnicodeString.php
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/Symfony/Component/String/ByteString.php
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/Symfony/Component/String/CHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
View file
Open in desktop
Original file line numberDiff line numberDiff 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"],
];
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff 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ä'],
['漢字ーユニコードéèΣσς', 'ςσΣèéドーコニユー字漢'],
['नमस्ते', 'तेस्मन'],
]
);
}
}

[8]ページ先頭

©2009-2025 Movatter.jp