Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add match and subst to the setting.
  • Loading branch information
jnthn committed Feb 17, 2013
1 parent aba0d5c commit 1a5005a
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions nqp-src/NQPCORE.setting
Expand Up @@ -596,6 +596,65 @@ sub join($delim, @things) {
nqp::join($delim, @strs)
}

# vim: ft=perl6
# From src\core\Regex.pm

=begin

Regex methods and functions

=end

=begin item match
Match C<$text> against C<$regex>. If the C<$global> flag is
given, then return an array of all non-overlapping matches.
=end item

sub match ($text, $regex, :$global?) {
my $match := $text ~~ $regex;
if $global {
my @matches;
while $match {
nqp::push(@matches, $match);
$match := $match.CURSOR.parse($text, :rule($regex), :c($match.to));
}
@matches;
}
else {
$match;
}
}


=begin item subst
Substitute an match of C<$regex> in C<$text> with C<$replacement>,
returning the substituted string. If C<$global> is given, then
perform the replacement on all matches of C<$text>.
=end item

sub subst ($text, $regex, $repl, :$global?) {
my @matches := $global ?? match($text, $regex, :global)
!! [ $text ~~ $regex ];
my $is_code := nqp::isinvokable($repl);
my $offset := 0;
my @result;

for @matches -> $match {
if $match {
nqp::push(@result, nqp::substr($text, $offset, $match.from - $offset))
if $match.from > $offset;
nqp::push(@result, $is_code ?? ~$repl($match) !! ~$repl);
$offset := $match.to;
}
}

my $chars := nqp::chars($text);
nqp::push(@result, nqp::substr($text, $offset, $chars))
if $chars > $offset;

nqp::join("", @result);
}

# vim: ft=perl6
# From src\core\Hash.pm

Expand Down

0 comments on commit 1a5005a

Please sign in to comment.