8000 feature #35091 [String] Add the reverse() method (fancyweb) · symfony/symfony@d021ff1 · GitHub
[go: up one dir, main page]

Skip to content

Commit d021ff1

Browse files
feature #35091 [String] Add the reverse() method (fancyweb)
This PR was merged into the 5.1-dev branch. Discussion ---------- [String] Add the reverse() method | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Allows to easily reverse a string since `mb_strrev` does not exist. Commits ------- c369598 [String] Add the reverse method
2 parents cdaebf6 + c369598 commit d021ff1

File tree

6 files changed

+63
-0
lines changed

6 files changed

+63
-0
lines changed

src/Symfony/Component/String/AbstractString.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,11 @@ abstract public function replace(string $from, string $to): self;
467467
*/
468468
abstract public function replaceMatches(string $fromRegexp, $to): self;
469469

470+
/**
471+
* @return static
472+
*/
473+
abstract public function reverse(): self;
474+
470475
/**
471476
* @return static
472477
*/

src/Symfony/Component/String/AbstractUnicodeString.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,17 @@ public function replaceMatches(string $fromRegexp, $to): parent
342342
return $str;
343343
}
344344

345+
/**
346+
* {@inheritdoc}
347+
*/
348+
public function reverse(): parent
349+
{
350+
$str = clone $this;
351+
$str->string = implode('', array_reverse(preg_split('/(\X)/u', $str->string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY)));
352+
353+
return $str;
354+
}
355+
345356
public function snake(): parent
346357
{
347358
$str = $this->camel()->title();

src/Symfony/Component/String/ByteString.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,17 @@ public function replaceMatches(string $fromRegexp, $to): parent
303303
return $str;
304304
}
305305

306+
/**
307+
* {@inheritdoc}
308+
*/
309+
public function reverse(): parent
310+
{
311+
$str = clone $this;
312+
$str->string = strrev($str->string);
313+
314+
return $str;
315+
}
316+
306317
public function slice(int $start = 0, int $length = null): parent
307318
{
308319
$str = clone $this;

src/Symfony/Component/String/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.1.0
5+
-----
6+
7+
* Added the `AbstractString::reverse()` method.
8+
49
5.0.0
510
-----
611

src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,4 +1377,23 @@ public function testToString()
13771377

13781378
self::assertSame('foobar', $instance->toString());
13791379
}
1380+
1381+
/**
1382+
* @dataProvider provideReverse
1383+
*/
1384+
public function testReverse(string $expected, string $origin)
1385+
{
1386+
$instance = static::createFromString($origin)->reverse();
1387+
1388+
$this->assertEquals(static::createFromString($expected), $instance);
1389+
}
1390+
1391+
public static function provideReverse()
1392+
{
1393+
return [
1394+
['', ''],
1395+
['oof', 'foo'],
1396+
["\n!!!\tTAERG SI ynofmyS ", " Symfony IS GREAT\t!!!\n"],
1397+
];
1398+
}
13801399
}

src/Symfony/Component/String/Tests/AbstractUnicodeTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,4 +568,16 @@ public static function providePadStart(): array
568568
]
569569
);
570570
}
571+
572+
public static function provideReverse()
573+
{
574+
return array_merge(
575+
parent::provideReverse(),
576+
[
577+
['äuß⭐erst', 'tsre⭐ßuä'],
578+
['漢字ーユニコードéèΣσς', 'ςσΣèéドーコニユー字漢'],
579+
['नमस्ते', 'तेस्मन'],
580+
]
581+
);
582+
}
571583
}

0 commit comments

Comments
 (0)
0