Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Str\Grapheme\reverse() function #239

Merged
merged 8 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions docs/component/str-grapheme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [ends_with](./../../src/Psl/Str/Grapheme/ends_with.php#L17)
- [ends_with_ci](./../../src/Psl/Str/Grapheme/ends_with_ci.php#L17)
- [length](./../../src/Psl/Str/Grapheme/length.php#L19)
- [reverse](./../../src/Psl/Str/Grapheme/reverse.php#L19)
- [search](./../../src/Psl/Str/Grapheme/search.php#L24)
- [search_ci](./../../src/Psl/Str/Grapheme/search_ci.php#L24)
- [search_last](./../../src/Psl/Str/Grapheme/search_last.php#L25)
Expand Down
1 change: 1 addition & 0 deletions src/Psl/Internal/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ final class Loader
'Psl\Str\Grapheme\ends_with',
'Psl\Str\Grapheme\ends_with_ci',
'Psl\Str\Grapheme\length',
'Psl\Str\Grapheme\reverse',
'Psl\Str\Grapheme\search',
'Psl\Str\Grapheme\search_ci',
'Psl\Str\Grapheme\search_last',
Expand Down
34 changes: 34 additions & 0 deletions src/Psl/Str/Grapheme/reverse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Psl\Str\Grapheme;

use Psl\Exception\InvalidArgumentException;

use function grapheme_strlen;
use function grapheme_substr;

/**
* Reverses the string.
*
* @pure
*
* @throws \Psl\Exception\InvalidArgumentException If $string length cannot be determined.
*/
function reverse(string $string): string
{
$reversed = '';

$length = grapheme_strlen($string);

if ($length === false || $length === null) {
throw new InvalidArgumentException('Cannot get string length. Possibly invalid UTF-8 sequence');
}

while ($length-- > 0) {
$reversed .= grapheme_substr($string, $length, 1);
}
azjezz marked this conversation as resolved.
Show resolved Hide resolved

return $reversed;
}
39 changes: 39 additions & 0 deletions tests/unit/Str/Grapheme/ReverseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Psl\Tests\Unit\Str\Grapheme;

use PHPUnit\Framework\TestCase;
use Psl;
azjezz marked this conversation as resolved.
Show resolved Hide resolved

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(Psl\Str\Grapheme\reverse($string), $expected);
}

public function testFailReverse(): void
{
$string = Psl\Str\Byte\slice('🐶🐶🐶', 0, 5);

$this->expectException(Psl\Exception\InvalidArgumentException::class);
Psl\Str\Grapheme\reverse($string);
}
}