From 31598e90f3035b1154925d7329bd98a17b84a3f1 Mon Sep 17 00:00:00 2001 From: Solomon Foster Date: Tue, 18 May 2010 08:28:20 -0400 Subject: [PATCH] Add beginnings of support for :nth argument to .match. --- src/core/Cool-str.pm | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/core/Cool-str.pm b/src/core/Cool-str.pm index 6aad21ae4eb..4aa10cf6c01 100644 --- a/src/core/Cool-str.pm +++ b/src/core/Cool-str.pm @@ -177,7 +177,7 @@ augment class Cool { self gt '' ?? self.substr(0,1).lc ~ self.substr(1) !! "" } - our multi method match(Regex $pat, :c(:$continue), :g(:$global), :pos(:$p)) { + our multi method match(Regex $pat, :c(:$continue), :g(:$global), :pos(:$p), Mu :$nth) { if $continue ~~ Bool { note ":c / :continue requires a position in the string"; fail ":c / :continue requires a position in the string"; @@ -186,15 +186,21 @@ augment class Cool { %opts

= $p if defined $p; %opts = $continue // 0 unless defined $p; - if $global { + if $global || $nth.defined { + my $i = 1; gather while my $m = Regex::Cursor.parse(self, :rule($pat), |%opts) { my $m-copy = $m; - take $m-copy; + if $nth.defined { + take $m-copy if $i ~~ any(|$nth); + } else { + take $m-copy; + } if $m.to == $m.from { %opts = $m.to + 1; } else { %opts = $m.to; } + $i++; } } else { Regex::Cursor.parse(self, :rule($pat), |%opts);