New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Perl 6 versions of p5chomp and p5chop to Any.pm #821
Comments
From @cspencerThe following patch adds Perl 6 versions of the p5chomp and p5chop methods |
From @cspencer0001-Added-Perl-6-version-of-the-p5chop-p5chomp-methods-t.patchFrom 3648e7bf639b0b189309ff122e9f4e2bfe9fd2da Mon Sep 17 00:00:00 2001
From: git <cspencer@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 <cspencer@sprocket.org>
Date: Sun Mar 22 10:02:40 2009 -0700
Removed reference to p5chomp from Str.pir
commit 09c01053b5fc1dde4c26639fb0e1baa87c82f31a
Author: git <cspencer@sprocket.org>
Date: Sun Mar 22 10:00:40 2009 -0700
Added a Perl 6 version of p5chomp.
commit 6fe1ea7661c5e0388eee45e9290ca0db7f9e935f
Author: git <cspencer@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
|
From @pmichaudOn Sun Mar 22 10:11:06 2009, cspencer wrote:
Applying this patch causes me to get failures in t/spec/S05-mass/rx.t . I should also note that I suspect there's a problem with the p5chomp Also, we should double-check that negative offsets to substr() work as Thanks, Pm |
The RT System itself - Status changed from 'new' to 'open' |
From @cspencerThis patches addresses the issues in the previous version as identified On Wed Mar 25 15:11:44 2009, pmichaud wrote:
|
From @cspencer0001-Fixed-p5chop-p5chomp.patchFrom 11a3ab091fada3782f1998a0fe9b0bfbe2044c80 Mon Sep 17 00:00:00 2001
From: git <cspencer@sprocket.org>
Date: Wed, 25 Mar 2009 16:28:10 -0700
Subject: [PATCH] Fixed p5chop/p5chomp as per suggestions from pmichaud. Removed return from lc/uc methods.
---
src/classes/Str.pir | 17 -----------------
src/setting/Any-str.pm | 31 +++++++++++++++++++++++++++++--
2 files changed, 29 insertions(+), 19 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 f32f841..eb17e7f 100644
--- a/src/setting/Any-str.pm
+++ b/src/setting/Any-str.pm
@@ -12,7 +12,7 @@ class Any is also {
}
our Str multi method lc is export {
- return Q:PIR {
+ Q:PIR {
$S0 = self
downcase $S0
%r = box $S0
@@ -23,6 +23,33 @@ class Any is also {
self gt '' ?? self.substr(0,1).lc ~ self.substr(1) !! ""
}
+ our Int multi method p5chomp is export(:P5) {
+ my $num = 0;
+
+ for @.list -> $str is rw {
+ if $str ~~ /\o12$/ {
+ $str = $str.substr(0, $str.chars - 1);
+ $num++;
+ }
+ }
+
+ $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($str.chars - 1, 1);
+ $str = $str.chop;
+ }
+ }
+
+ $char
+ }
+
our List multi method split(Code $delimiter, $limit = *) {
my $s = ~self;
my $l = $limit ~~ Whatever ?? Inf !! $limit;
@@ -87,7 +114,7 @@ class Any is also {
}
our Str multi method uc is export {
- return Q:PIR {
+ Q:PIR {
$S0 = self
upcase $S0
%r = box $S0
--
1.6.0.6
|
From @pmichaudApplied in a0c6e3d, with a small change (switching \o12 to \x0a) in 5c07c7b. There are already tests for p5chomp and p5chop in the test suite, so I'm also curious if checking for only \x0a is correct; the synopsis says Thanks! Pm |
1 similar comment
From @pmichaudApplied in a0c6e3d, with a small change (switching \o12 to \x0a) in 5c07c7b. There are already tests for p5chomp and p5chop in the test suite, so I'm also curious if checking for only \x0a is correct; the synopsis says Thanks! Pm |
@pmichaud - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#64092 (status was 'resolved')
Searchable as RT64092$
The text was updated successfully, but these errors were encountered: