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

Commit

Permalink
Use StringBuilder in subst instead of manually collect pieces.
Browse files Browse the repository at this point in the history
  • Loading branch information
bacek committed May 15, 2010
1 parent 7516db0 commit 05c8f66
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/setting/Regex.pm
Expand Up @@ -38,21 +38,21 @@ sub subst ($text, $regex, $repl, :$global?) {
!! [ $text ~~ $regex ];
my $is_code := pir::isa($repl, 'Sub');
my $offset := 0;
my @pieces;
my $result := pir::new__Ps('StringBuilder');

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

my $chars := pir::length($text);
@pieces.push(pir::substr($text, $offset, $chars))
pir::push($result, pir::substr($text, $offset, $chars))
if $chars > $offset;

@pieces.join('');
~$result;
}

# vim: ft=perl6

0 comments on commit 05c8f66

Please sign in to comment.