Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 816dbdc

Browse files
feature symfony#578 [RFC] Add grapheme_strrev function php 8.6 (PhilDaiguille)
This PR was squashed before being merged into the 1.x branch. Discussion ---------- [RFC] Add grapheme_strrev function php 8.6 Adds the new grapheme_strrev function added to PHP 8.6. This function is part of the Intl extension's Grapheme functions and reverses a string by grapheme clusters, correctly handling multi-byte and multi code-point characters (such as Emoji sequences). - [RFC](https://php.watch/rfcs/grapheme_strrev) - [Commit](php/php-src@7a1c261) - [PHP 8.6: New grapheme_strrev function](https://php.watch/versions/8.6/grapheme_strrev) - [Codex](https://php.watch/codex/grapheme_strrev) Commits ------- ab78662 [RFC] Add grapheme_strrev function php 8.6
2 parents 025f325 + ab78662 commit 816dbdc

8 files changed

Lines changed: 95 additions & 0 deletions

File tree

β€ŽREADME.mdβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Polyfills are provided for:
8888
- the `clamp` function introduced in PHP 8.6;
8989
- the `ARRAY_FILTER_USE_VALUE` constant introduced in PHP 8.6;
9090
- the `SortDirection` enum introduced in PHP 8.6;
91+
- the `grapheme_strrev` function introduced in PHP 8.6;
9192

9293
It is strongly recommended to upgrade your PHP version and/or install the missing
9394
extensions whenever possible. This polyfill should be used only when there is no

β€Žsrc/Intl/Grapheme/Grapheme.phpβ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* - grapheme_substr - Return part of a string
2929
* - grapheme_str_split - Splits a string into an array of individual or chunks of graphemes
3030
* - grapheme_levenshtein - Calculate the grapheme-unit Levenshtein distance between two strings
31+
* - grapheme_strrev - Reverse a string by grapheme clusters
3132
*
3233
* @author Nicolas Grekas <[email protected]>
3334
*
@@ -325,4 +326,15 @@ private static function grapheme_position($s, $needle, $offset, $mode)
325326

326327
return false !== $needlePos ? self::grapheme_strlen(substr($s, 0, $needlePos)) + $offset : false;
327328
}
329+
330+
public static function grapheme_strrev(string $string)
331+
{
332+
$units = grapheme_str_split($string);
333+
334+
if (false === $units) {
335+
return false;
336+
}
337+
338+
return implode('', array_reverse($units));
339+
}
328340
}

β€Žsrc/Intl/Grapheme/bootstrap.phpβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ function grapheme_str_split(string $string, int $length = 1) { return p\Grapheme
5858
if (!function_exists('grapheme_levenshtein')) {
5959
function grapheme_levenshtein(string $string1, string $string2, int $insertion_cost = 1, int $replacement_cost = 1, int $deletion_cost = 1, string $locale = '') { return p\Php85::grapheme_levenshtein($string1, $string2, $insertion_cost, $replacement_cost, $deletion_cost); }
6060
}
61+
if (!function_exists('grapheme_strrev')) {
62+
function grapheme_strrev(string $string) { return p\Grapheme::grapheme_strrev($string); }
63+
}

β€Žsrc/Intl/Grapheme/bootstrap80.phpβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ function grapheme_str_split(string $string, int $length = 1): array|false { retu
1717
if (!function_exists('grapheme_levenshtein')) {
1818
function grapheme_levenshtein(string $string1, string $string2, int $insertion_cost = 1, int $replacement_cost = 1, int $deletion_cost = 1, string $locale = ''): int|false { return p\Grapheme::grapheme_levenshtein($string1, $string2, $insertion_cost, $replacement_cost, $deletion_cost); }
1919
}
20+
if (!function_exists('grapheme_strrev')) {
21+
function grapheme_strrev(string $string): string|false { return p\Grapheme::grapheme_strrev($string); }
22+
}
2023

2124
if (extension_loaded('intl')) {
2225
return;

β€Žsrc/Php86/Php86.phpβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,13 @@ private static function throwValueErrorIfAvailable(string $message): void
6262

6363
throw new \ValueError($message);
6464
}
65+
66+
public static function grapheme_strrev(string $string)
67+
{
68+
if (false === $units = grapheme_str_split($string)) {
69+
return false;
70+
}
71+
72+
return implode('', array_reverse($units));
73+
}
6574
}

β€Žsrc/Php86/bootstrap.phpβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@
3737
*/
3838
function clamp($value, $min, $max) { return p\Php86::clamp($value, $min, $max); }
3939
}
40+
41+
if (extension_loaded('intl') && !function_exists('grapheme_strrev')) {
42+
function grapheme_strrev(string $string) { return p\Php86::grapheme_strrev($string); }
43+
}

β€Žtests/Intl/Grapheme/GraphemeTest.phpβ€Ž

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,38 @@ public function testGraphemeLevenshteinNegativeCost()
296296
$this->expectException(\ValueError::class);
297297
grapheme_levenshtein('a', 'b', -1);
298298
}
299+
300+
/**
301+
* @covers \Symfony\Polyfill\Intl\Grapheme\Grapheme::grapheme_strrev
302+
*
303+
* @dataProvider provideGraphemeStrrev
304+
*/
305+
public function testGraphemeStrrev(string $expected, string $string)
306+
{
307+
$this->assertSame($expected, grapheme_strrev($string));
308+
}
309+
310+
public static function provideGraphemeStrrev(): array
311+
{
312+
return [
313+
['', ''],
314+
['A', 'A'],
315+
['EDCBA', 'ABCDE'],
316+
['elppA eplpA', 'Aplpe Apple'],
317+
['🍏elppA', 'Apple🍏'],
318+
['πŸ‡³πŸ‡¨ - πŸ‡¨πŸ‡³', 'πŸ‡¨πŸ‡³ - πŸ‡³πŸ‡¨'],
319+
['ζ—₯本θͺž', 'θͺžζœ¬ζ—₯'],
320+
['πŸŽ‰πŸ‘πŸ½πŸŽŠ', 'πŸŽŠπŸ‘πŸ½πŸŽ‰'],
321+
['CπŸ‘πŸ½A', 'AπŸ‘πŸ½C'],
322+
["B\0A", "A\0B"],
323+
];
324+
}
325+
326+
/**
327+
* @covers \Symfony\Polyfill\Intl\Grapheme\Grapheme::grapheme_strrev
328+
*/
329+
public function testGraphemeStrrevInvalidUtf8()
330+
{
331+
$this->assertFalse(grapheme_strrev("\xFF"));
332+
}
299333
}

β€Žtests/Php86/Php86Test.phpβ€Ž

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,33 @@ public function testSortDirectionEnum()
183183
$this->assertInstanceOf(\UnitEnum::class, \SortDirection::Ascending);
184184
$this->assertInstanceOf(\UnitEnum::class, \SortDirection::Descending);
185185
}
186+
187+
/**
188+
* @dataProvider provideGraphemeStrrev
189+
*/
190+
public function testGraphemeStrrev(string $expected, string $string)
191+
{
192+
$this->assertSame($expected, grapheme_strrev($string));
193+
}
194+
195+
public static function provideGraphemeStrrev(): array
196+
{
197+
return [
198+
['', ''],
199+
['A', 'A'],
200+
['EDCBA', 'ABCDE'],
201+
['elppA eplpA', 'Aplpe Apple'],
202+
['🍏elppA', 'Apple🍏'],
203+
['πŸ‡³πŸ‡¨ - πŸ‡¨πŸ‡³', 'πŸ‡¨πŸ‡³ - πŸ‡³πŸ‡¨'],
204+
['ζ—₯本θͺž', 'θͺžζœ¬ζ—₯'],
205+
['πŸŽ‰πŸ‘πŸ½πŸŽŠ', 'πŸŽŠπŸ‘πŸ½πŸŽ‰'],
206+
['CπŸ‘πŸ½A', 'AπŸ‘πŸ½C'],
207+
["B\0A", "A\0B"],
208+
];
209+
}
210+
211+
public function testGraphemeStrrevInvalidUtf8()
212+
{
213+
$this->assertFalse(grapheme_strrev("\xFF"));
214+
}
186215
}

0 commit comments

Comments
Β (0)