Skip to content

Commit

Permalink
grapheme_reverse works more like CORE::reverse
Browse files Browse the repository at this point in the history
+ works on lists of strings instead of just a single string
+ works just like reverse() in list context
  • Loading branch information
patch committed Feb 7, 2013
1 parent 3b9bd0c commit 7fa0d1f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
6 changes: 3 additions & 3 deletions lib/Unicode/Util.pm
Expand Up @@ -80,9 +80,9 @@ sub grapheme_chop (;\[$@%]) {
}

sub grapheme_reverse (;@) {
my ($str) = @_;
utf8::upgrade($str);
return join '', reverse $str =~ m{ \X }xg;
my (@strings) = @_;
return reverse @strings if wantarray;
return join '', map { reverse m{ \X }xg } reverse @strings;
}

# experimental functions
Expand Down
29 changes: 24 additions & 5 deletions t/grapheme_reverse.t
Expand Up @@ -2,14 +2,33 @@ use strict;
use warnings;
use utf8;
use open qw( :encoding(UTF-8) :std );
use Test::More tests => 1;
use Test::More tests => 4;
use Unicode::Util qw( grapheme_reverse );

# Unicode::Util tests for grapheme_reverse

# 'ю́xя̡̅̈' to 'я̡̅̈xю́'
my $str = "ю\x{0301}\x{0305}\x{0308}\x{0321}"; # ю́xя̡̅̈

is(
scalar grapheme_reverse($str),
"я\x{0305}\x{0308}\x{0321}\x{0301}", # я̡̅̈xю́
'grapheme_reverse on string of grapheme clusters in scalar context'
);

is_deeply(
[grapheme_reverse($str)],
[$str],
'grapheme_reverse on string of grapheme clusters in list context'
);

is(
scalar grapheme_reverse("\x{44E}\x{301}x\x{44F}\x{305}\x{308}\x{321}"),
"\x{44F}\x{305}\x{308}\x{321}x\x{44E}\x{301}",
'grapheme_reverse on grapheme clusters'
scalar grapheme_reverse('a', ($str) x 2, 'z'),
'z' . "я\x{0305}\x{0308}\x{0321}\x{0301}" x 2 . 'a', # zя̡̅̈xю́я̡̅̈xю́a
'grapheme_reverse on list of strings of grapheme clusters in scalar context'
);

is_deeply(
[grapheme_reverse('a', ($str) x 2, 'z')],
['z', ($str) x 2, 'a'],
'grapheme_reverse on list of strings of grapheme clusters in list context'
);

0 comments on commit 7fa0d1f

Please sign in to comment.