Hi! Attached are code and tests for my trying of implementing the :samecase for .subst.
diff --git a/src/core/Cool-str.pm b/src/core/Cool-str.pm index 918ec68..42a2321 100644 --- a/src/core/Cool-str.pm +++ b/src/core/Cool-str.pm @@ -22,7 +22,7 @@ augment class Cool { } } - multi method subst($matcher, $replacement, *%options) { + multi method subst($matcher, $replacement, :$samecase, *%options) { my @matches = self.match($matcher, |%options); return self unless @matches; return self if @matches == 1 && !...@matches[0]; @@ -30,9 +30,10 @@ augment class Cool { my $result = ''; for @matches -> $m { $result ~= self.substr($prev, $m.from - $prev); - $result ~= ~($replacement ~~ Callable - ?? $replacement($m) - !! $replacement); + + my $real_replacement = ~($replacement ~~ Callable ?? $replacement($m) !! $replacement); + $real_replacement = $real_replacement.samecase(~$m) if $samecase; + $result ~= $real_replacement; $prev = $m.to; } my $last = @matches.pop;
Index: t/spec/S05-substitution/subst.t =================================================================== --- t/spec/S05-substitution/subst.t (revision 30804) +++ t/spec/S05-substitution/subst.t (working copy) @@ -290,6 +290,16 @@ ok s (foo) = 'bar', 'bare s is substitution before whitespace then parens'; } +# Test for :samecase +{ + is 'The foo and the bar'.subst('the', 'that', :samecase), 'The foo and that bar', '.substr and :samecase (1)'; + is 'The foo and the bar'.subst('the', 'That', :samecase), 'The foo and that bar', '.substr and :samecase (2)'; + is 'The foo and the bar'.subst(/:i the/, 'that', :samecase), 'That foo and the bar', '.substr (string pattern) and : samecase (1)'; + is 'The foo and the bar'.subst(/:i The/, 'That', :samecase), 'That foo and the bar', '.substr (string pattern) and : samecase (2)'; + is 'The foo and the bar'.subst(/:i the/, 'that', :g, :samecase), 'That foo and that bar', '.substr (string pattern) and :g and :samecase (1)'; + is 'The foo and the bar'.subst(/:i The/, 'That', :g, :samecase), 'That foo and that bar', '.substr (string pattern) and :g and :samecase (2)'; +} + done_testing; # vim: ft=perl6