Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
Fix subst for non-matching patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
bacek committed May 23, 2010
1 parent b228f8f commit 04de3a0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/setting/Regex.pm
Expand Up @@ -13,17 +13,17 @@ given, then return an array of all non-overlapping matches.

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


Expand All @@ -34,8 +34,8 @@ perform the replacement on all matches of C<$text>.
=end item

our sub subst ($text, $regex, $repl, :$global?) {
my @matches := $global ?? match($text, $regex, :global)
!! [ $text ~~ $regex ];
my @matches := match($text, $regex, $global);

my $is_code := pir::isa($repl, 'Sub');
my $offset := 0;
my $result := pir::new__Ps('StringBuilder');
Expand Down
7 changes: 6 additions & 1 deletion t/setting/05-subst.t
Expand Up @@ -2,7 +2,7 @@

pir::load_bytecode('nqp-setting.pbc' );

plan(5);
plan(7);

my $str := 'hello';

Expand All @@ -15,3 +15,8 @@ my $i := 0;
ok(subst($str, /l/, {$i++}) eq 'he0lo', 'We can have a closure as replacement');
ok($i == 1, '.. and $i updated');

ok(subst($str, /FOO/, 'BAR') eq 'hello', "Non-existing string doesn't clobber string");
ok(subst($str, /FOO/, 'BAR', :global) eq 'hello', "Non-existing string doesn't clobber string globally");

# vim: ft=perl6

0 comments on commit 04de3a0

Please sign in to comment.