Permalink
Browse files

implement :x in Str.subst; implement :all in Str.split(Str)

  • Loading branch information...
1 parent 16efb68 commit 3fba2c3a343806885b983c624c4c1aeb7389c009 @moritz moritz committed Mar 9, 2010
Showing with 16 additions and 11 deletions.
  1. +14 −9 src/core/Any-str.pm
  2. +2 −2 t/spectest.data
View
@@ -21,18 +21,22 @@ augment class Any {
}
}
- multi method subst($matcher, Str $replacement, :global(:g($g))) {
- self.split($matcher, :limit($g ?? * !! 2)).join($replacement);
- }
-
- multi method subst($matcher, &replacement, :global(:g($g))) {
- my @chunks = self.split($matcher, :limit($g ?? * !! 2), :all);
+ multi method subst($matcher, $replacement, :g(:$global), :$x) {
+ die "Can't combine :g/:global and :x in subst"
+ if defined($global) && defined($x);
+ my $limit = defined($x) ?? $x +1 !! 2;
+ my @chunks = self.split($matcher, :limit($global ?? * !! $limit), :all);
+ if defined($x) && (@chunks < 2 * $x) {
+ return self;
+ }
loop (my $i = 1; $i < @chunks; $i += 2) {
- @chunks[$i] = replacement(@chunks[$i]);
+ pir::store_dynamic_lex__vSP('$/', @chunks[$i]);
+ @chunks[$i] = $replacement ~~ Callable ?? $replacement(@chunks[$i]) !! $replacement;
}
@chunks.join('');
}
+
multi method comb(Regex $matcher = /./, $limit = *, :$match) {
my $c = 0;
my $l = $limit ~~ ::Whatever ?? Inf !! $limit;
@@ -60,8 +64,8 @@ augment class Any {
}
}
- multi method split($delimiter, $limit = *) {
- my Str $match-string = $delimiter ~~ Str ?? $delimiter !! $delimiter.Str;
+ multi method split($delimiter, $limit = *, :$all) {
+ my $match-string = $delimiter.Str;
my $c = 0;
my $l = $limit ~~ ::Whatever ?? Inf !! $limit - 1;
if $l >= 0 {
@@ -75,6 +79,7 @@ augment class Any {
my $m = self.index($match-string, $c);
last if $m.notdef; # CHEAT, but the best I can do for now
take self.substr($c, $m - $c);
+ take $match-string if $all;
$c = $m + $match-string.chars;
}
}
View
@@ -262,8 +262,8 @@ S05-metasyntax/single-quotes.t
# S05-modifier/perl5_2.t
# S05-modifier/perl5_7.t
# S05-modifier/perl5_8.t
-# S05-substitution/match.t
-# S05-substitution/subst.t
+S05-substitution/match.t
+S05-substitution/subst.t
# S05-transliteration/trans.t
# S05-transliteration/with-closure.t
S06-advanced_subroutine_features/lexical-subs.t

0 comments on commit 3fba2c3

Please sign in to comment.