-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(str): add a new
reverse()
function (#238)
- Loading branch information
Showing
4 changed files
with
55 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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Str; | ||
|
||
use Psl\Vec; | ||
|
||
/** | ||
* Returns the given string reversed. | ||
* | ||
* @psalm-pure | ||
*/ | ||
function reverse(string $string, ?string $encoding = null): string | ||
{ | ||
$chunks = chunk($string, encoding: $encoding); | ||
|
||
/** @psalm-suppress ImpureFunctionCall */ | ||
return join(Vec\reverse($chunks), ''); | ||
} |
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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Str; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Str; | ||
|
||
final class ReverseTest extends TestCase | ||
{ | ||
|
||
public function provideData(): array | ||
{ | ||
return [ | ||
['Hello World', 'dlroW olleH'], | ||
['héllö wôrld', 'dlrôw ölléh'], | ||
['Iñigo Montoya', 'ayotnoM ogiñI'], | ||
['某物', '物某' ], | ||
['что-то', 'от-отч'], | ||
['🙂😟', '😟🙂'], | ||
['مرحبا', 'ابحرم'], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function testReverse(string $string, string $expected): void | ||
{ | ||
static::assertSame(Str\reverse($string), $expected); | ||
} | ||
} |