-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(str-grapheme): add
reverse()
function (#239)
- Loading branch information
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Str\Grapheme; | ||
|
||
/** | ||
* Reverses the string. | ||
* | ||
* @pure | ||
* | ||
* @throws \Psl\Exception\InvariantViolationException If unable to convert $string to UTF-16, | ||
* or split it into graphemes | ||
*/ | ||
function reverse(string $string): string | ||
{ | ||
$reversed = ''; | ||
$offset = length($string); | ||
|
||
while ($offset-- > 0) { | ||
$reversed .= slice($string, $offset, 1); | ||
} | ||
|
||
return $reversed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Str\Grapheme; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Exception; | ||
use Psl\Str\Byte; | ||
use Psl\Str\Grapheme; | ||
|
||
class ReverseTest extends TestCase | ||
{ | ||
public function provideData(): array | ||
{ | ||
return [ | ||
['Hello World 👩🏽❤️👨🏼', '👩🏽❤️👨🏼 dlroW olleH'], | ||
['👩👩👦👩🏽❤️👨🏼👩🏽🏫', '👩🏽🏫👩🏽❤️👨🏼👩👩👦'], | ||
['某物 🖖🏿', '🖖🏿 物某' ], | ||
['👲🏻 что-то', 'от-отч 👲🏻'], | ||
['🙂👨🏼🎤😟', '😟👨🏼🎤🙂'], | ||
['مرحبا👲🏻👨🏼🎤', '👨🏼🎤👲🏻ابحرم'], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function testReverse(string $string, string $expected): void | ||
{ | ||
static::assertSame(Grapheme\reverse($string), $expected); | ||
} | ||
|
||
public function testFailReverse(): void | ||
{ | ||
$string = Byte\slice('🐶🐶🐶', 0, 5); | ||
|
||
$this->expectException(Exception\InvariantViolationException::class); | ||
Grapheme\reverse($string); | ||
} | ||
} |