Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

in Str.subst, never produce the whole list of matches

for very long strings this is too memory intensive. not_gerd++
  • Loading branch information...
commit 494bd4f5a4525654161974950fb02392186d6836 1 parent b057f6f
@moritz moritz authored
Showing with 11 additions and 6 deletions.
  1. +11 −6 src/core/Str.pm
View
17 src/core/Str.pm
@@ -586,13 +586,19 @@ my class Str does Stringy {
multi method subst($matcher, $replacement,
:ii(:$samecase), :ss(:$samespace),
:$SET_CALLER_DOLLAR_SLASH, *%options) {
- my @matches = self.match($matcher, |%options);
- return self unless @matches;
- return self if @matches == 1 && !@matches[0];
+ my $matches := self.match($matcher, |%options);
+ return self unless $matches;
+ if $matches ~~ Match {
+ $matches := ($matches,).list;
+ }
+ else {
+ $matches := $matches.list;
+ }
my $caller_dollar_slash := pir::find_caller_lex__Ps('$/');
my $prev = 0;
my $result = '';
- for @matches -> $m {
+ while $matches {
+ my $m = $matches.shift;
$result ~= self.substr($prev, $m.from - $prev);
$caller_dollar_slash = $m if $SET_CALLER_DOLLAR_SLASH;
@@ -602,8 +608,7 @@ my class Str does Stringy {
$result ~= $real_replacement;
$prev = $m.to;
}
- my $last = @matches.pop;
- $result ~= self.substr($last.to);
+ $result ~= self.substr($prev);
$result;
}
Please sign in to comment.
Something went wrong with that request. Please try again.