Skip to content

Commit

Permalink
Change .match to support the new list approach for :nth.
Browse files Browse the repository at this point in the history
  • Loading branch information
colomon committed May 26, 2010
1 parent bafb6d9 commit 475d1c7
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/core/Cool-str.pm
Expand Up @@ -186,7 +186,7 @@ augment class Cool {
:g(:$global),
:pos(:$p),
:$x,
Mu :$nth,
:$nth,
:ov(:$overlap)) {
if $continue ~~ Bool {
note ":c / :continue requires a position in the string";
Expand All @@ -206,13 +206,29 @@ augment class Cool {
}

if $global || $nth.defined || $overlap || ($x.defined && $x_upper > 1) {
my $next-index;
my $next-iterator;
if $nth.defined {
$next-iterator = $nth.list.iterator;
$next-index = $next-iterator.get;
return if $next-index ~~ EMPTY || +$next-index < 1;
}

my $taken = 0;
my $i = 1;
my @r = gather while my $m = Regex::Cursor.parse(self, :rule($pat), |%opts) {
my $m-copy = $m;
if !$nth.defined || ($i ~~ any |$nth) {
if !$nth.defined || $i == $next-index {
take $m-copy;
$taken++;

if ($nth.defined) {
$next-index = $next-iterator.get;
while $next-index !~~ EMPTY && $next-index <= $i {
$next-index = $next-iterator.get;
}
last if $next-index ~~ EMPTY;
}
}
last if $taken == $x_upper;

Expand Down

0 comments on commit 475d1c7

Please sign in to comment.