# New Ticket Created by Cory Spencer
# Please include the string: [perl #64092]
# in the subject line of all future correspondence about this issue.
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=64092 >
The following patch adds Perl 6 versions of the p5chomp and p5chop methods
to Any-str.pm
>From 3648e7bf639b0b189309ff122e9f4e2bfe9fd2da Mon Sep 17 00:00:00 2001
From: git <cspen...@sprocket.org>
Date: Sun, 22 Mar 2009 10:05:16 -0700
Subject: [PATCH] Added Perl 6 version of the p5chop/p5chomp methods to Any.pm
Squashed commit of the following:
commit b5638c38b34f2f6d9a490f69d73d551e87364932
Author: git <cspen...@sprocket.org>
Date: Sun Mar 22 10:02:40 2009 -0700
Removed reference to p5chomp from Str.pir
commit 09c01053b5fc1dde4c26639fb0e1baa87c82f31a
Author: git <cspen...@sprocket.org>
Date: Sun Mar 22 10:00:40 2009 -0700
Added a Perl 6 version of p5chomp.
commit 6fe1ea7661c5e0388eee45e9290ca0db7f9e935f
Author: git <cspen...@sprocket.org>
Date: Sun Mar 22 09:47:10 2009 -0700
Added a Perl 6 version of the p5chop method.
---
src/classes/Str.pir | 17 -------------
src/setting/Any-str.pm | 61 ++++++++++++++++++++++++++++++++++-------------
2 files changed, 44 insertions(+), 34 deletions(-)
diff --git a/src/classes/Str.pir b/src/classes/Str.pir
index c54610a..5621e34 100644
--- a/src/classes/Str.pir
+++ b/src/classes/Str.pir
@@ -163,23 +163,6 @@ Overridden for Str.
=over 4
-=item p5chop
-
- our Char multi P5emul::Str::p5chop ( Str $string is rw )
- our Char multi P5emul::Str::p5chop ( Str *...@strings = ($+_) is rw )
-
-Trims the last character from C<$string>, and returns it. Called with a
-list, it chops each item in turn, and returns the last character
-chopped.
-
-=item p5chomp
-
- our Int multi P5emul::Str::p5chomp ( Str $string is rw )
- our Int multi P5emul::Str::p5chomp ( Str *...@strings = ($+_) is rw )
-
-Related to C<p5chop>, only removes trailing chars that match C</\n/>. In
-either case, it returns the number of chars removed.
-
=item length
This word is banned in Perl 6. You must specify units.
diff --git a/src/setting/Any-str.pm b/src/setting/Any-str.pm
index bbf28a3..a266921 100644
--- a/src/setting/Any-str.pm
+++ b/src/setting/Any-str.pm
@@ -3,6 +3,23 @@ class Any is also {
self.substr(0, -1)
}
+ our List multi method comb (Code $matcher = /\S+/, $limit = *) {
+ my $l = $limit ~~ Whatever ?? Inf !! $limit;
+ # currently we use a copy of self and destroy it piece by piece.
+ # the preferred way of doing it is using self, not destroying it,
+ # and use the :pos modifier to the regex. That way the offsets into
+ # self will be right
+ my $s = ~self;
+ return gather {
+ while $l > 0 && $s ~~ $matcher {
+ # if we have captures, return the actual match object
+ take @($/) || %($/) ?? $/.clone !! ~$/;
+ $l--;
+ $s.=substr([max] 1, $/.to);
+ }
+ }
+ }
+
our Str multi method fmt(Str $format) {
sprintf($format, self)
}
@@ -15,6 +32,33 @@ class Any is also {
}
}
+ our Int multi method p5chomp is export(:P5) {
+ my $num = 0;
+
+ for @.list -> $str is rw {
+ if $str ~~ /\n$/ {
+ $str = $str.substr(0, -1);
+ $num++;
+ }
+ }
+
+ return $num;
+ }
+
+ # TODO: Return type should be a Char once that is supported.
+ our Str multi method p5chop is export(:P5) {
+ my $char = '';
+
+ for @.list -> $str is rw {
+ if $str gt '' {
+ $char = $str.substr(-1, 1);
+ $str = $str.chop;
+ }
+ }
+
+ return $char
+ }
+
our Str multi method lcfirst is export {
self gt '' ?? self.substr(0,1).lc ~ self.substr(1) !! ""
}
@@ -62,23 +106,6 @@ class Any is also {
}
}
- our List multi method comb (Code $matcher = /\S+/, $limit = *) {
- my $l = $limit ~~ Whatever ?? Inf !! $limit;
- # currently we use a copy of self and destroy it piece by piece.
- # the preferred way of doing it is using self, not destroying it,
- # and use the :pos modifier to the regex. That way the offsets into
- # self will be right
- my $s = ~self;
- return gather {
- while $l > 0 && $s ~~ $matcher {
- # if we have captures, return the actual match object
- take @($/) || %($/) ?? $/.clone !! ~$/;
- $l--;
- $s.=substr([max] 1, $/.to);
- }
- }
- }
-
our Str multi method uc is export {
return Q:PIR {
$S0 = self
--
1.6.0.6