Skip to content

Commit

Permalink
improve grapheme_index like CORE::index
Browse files Browse the repository at this point in the history
  • Loading branch information
patch committed Feb 11, 2013
1 parent 2afb387 commit 5e82f88
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions lib/Unicode/Util.pm
Expand Up @@ -87,16 +87,14 @@ sub grapheme_index ($$;$) {
if (!looks_like_number($pos) || $pos < 0) {
$pos = 0;
}
elsif ($pos > (my $graphs = grapheme_length($str))) {
$pos = $graphs;
elsif ($pos > (my $length = grapheme_length($str))) {
$pos = $length;
}

if ($str =~ m{ ^ ( \X{$pos} \X*? ) \Q$substr\E }xg) {
return grapheme_length($1);
}
else {
return -1;
}
return grapheme_length($1) + $[
if $str =~ m{ ^ ( \X{$pos} \X*? ) \Q$substr\E }xg;

return -1;
}

sub grapheme_rindex ($$;$) {
Expand All @@ -111,12 +109,10 @@ sub grapheme_rindex ($$;$) {
$str = substr $str, 0, $pos + ($substr ? 1 : 0);
}

if ($str =~ m{ ^ ( \X* ) \Q$substr\E }xg) {
return grapheme_length($1);
}
else {
return -1;
}
return grapheme_length($1)
if $str =~ m{ ^ ( \X* ) \Q$substr\E }xg;

return -1;
}

sub grapheme_substr ($$;$$) :lvalue {
Expand Down

0 comments on commit 5e82f88

Please sign in to comment.