10000 [String] Add the reverse method · symfony/symfony@da17343 · GitHub
[go: up one dir, main page]

Skip to content

Commit da17343

Browse files
committed
[String] Add the reverse method
1 parent 4e5b153 commit da17343

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
@@ -692,6 +692,11 @@ public function wordwrap(int $width = 75, string $break = "\n", bool $cut = fals
692692
return $str;
693693
}
694694

695+
/**
696+
* @return static
697+
*/
698+
abstract public function reverse(): self;
699+
695700
public function __sleep(): array
696701
{
697702
return ['string'];

src/Symfony/Component/String/AbstractUnicodeString.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,17 @@ public function width(bool $ignoreAnsiDecoration = true): int
444444
return $width;
445445
}
446446

447+
/**
448+
* {@inheritdoc}
449+
*/
450+
public function reverse(): parent
451+
{
452+
$str = clone $this;
453+
$str->string = implode('', array_reverse(preg_split('/(\X)/u', $str->string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY)));
454+
455+
return $str;
456+
}
457+
447458
/**
448459
* @return static
449460
*/

src/Symfony/Component/String/ByteString.php

Lines changed: 11 additions & 0 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,15 @@ public function width(bool $ignoreAnsiDecoration = true): int
474474

475475
return $width;
476476
}
477+
478+
/**
479+
* {@inheritdoc}
480+
*/
481+
public function reverse(): parent
482+
{
483+
$str = clone $this;
484+
$str->string = strrev($str->string);
485+
486+
return $str;
487+
}
477488
}

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