Skip to content
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

Closed
p6rt opened this issue Mar 22, 2009 · 9 comments
Closed

Add Perl 6 versions of p5chomp and p5chop to Any.pm #821

p6rt opened this issue Mar 22, 2009 · 9 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Mar 22, 2009

Migrated from rt.perl.org#64092 (status was 'resolved')

Searchable as RT64092$

@p6rt
Copy link
Author

p6rt commented Mar 22, 2009

From @cspencer

The following patch adds Perl 6 versions of the p5chomp and p5chop methods
to Any-str.pm

@p6rt
Copy link
Author

p6rt commented Mar 22, 2009

From @cspencer

0001-Added-Perl-6-version-of-the-p5chop-p5chomp-methods-t.patch
From 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

@p6rt
Copy link
Author

p6rt commented Mar 25, 2009

From @pmichaud

On Sun Mar 22 10​:11​:06 2009, cspencer wrote​:

The following patch adds Perl 6 versions of the p5chomp and p5chop
methods
to Any-str.pm

Applying this patch causes me to get failures in t/spec/S05-mass/rx.t .
I'm not exactly sure _why_ I'm getting the failures yet, but perhaps
others can try it out and let me know if they get similar results.

I should also note that I suspect there's a problem with the p5chomp
method -- it currently removes one character from the end of the string
if it matches /\n$/, but in Perl 6 regexes it's possible for \n to match
_two_ characters (e.g., a CR+LF sequence).

Also, we should double-check that negative offsets to substr() work as
expected here -- S32 doesn't mention anything about them.

Thanks,

Pm

@p6rt
Copy link
Author

p6rt commented Mar 25, 2009

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Mar 25, 2009

From @cspencer

This patches addresses the issues in the previous version as identified
by pmichaud. (Except for the failures in t/spec/S05-mass/rx.t)

On Wed Mar 25 15​:11​:44 2009, pmichaud wrote​:

On Sun Mar 22 10​:11​:06 2009, cspencer wrote​:

The following patch adds Perl 6 versions of the p5chomp and p5chop
methods
to Any-str.pm

Applying this patch causes me to get failures in t/spec/S05-mass/rx.t .
I'm not exactly sure _why_ I'm getting the failures yet, but perhaps
others can try it out and let me know if they get similar results.

I should also note that I suspect there's a problem with the p5chomp
method -- it currently removes one character from the end of the string
if it matches /\n$/, but in Perl 6 regexes it's possible for \n to match
_two_ characters (e.g., a CR+LF sequence).

Also, we should double-check that negative offsets to substr() work as
expected here -- S32 doesn't mention anything about them.

Thanks,

Pm

@p6rt
Copy link
Author

p6rt commented Mar 25, 2009

From @cspencer

0001-Fixed-p5chop-p5chomp.patch
From 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

@p6rt
Copy link
Author

p6rt commented Mar 26, 2009

From @pmichaud

Applied 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'll add them to spectest.data.

I'm also curious if checking for only \x0a is correct; the synopsis says
it removes anything that matches /\n/ which in Perl 6 would mean more
than just \x0a. However, the patch passes the existing spectests, so we
likely need some clarification of the spec and/or some additional tests.

Thanks!

Pm

1 similar comment
@p6rt
Copy link
Author

p6rt commented Mar 26, 2009

From @pmichaud

Applied 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'll add them to spectest.data.

I'm also curious if checking for only \x0a is correct; the synopsis says
it removes anything that matches /\n/ which in Perl 6 would mean more
than just \x0a. However, the patch passes the existing spectests, so we
likely need some clarification of the spec and/or some additional tests.

Thanks!

Pm

@p6rt
Copy link
Author

p6rt commented Mar 26, 2009

@pmichaud - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed Mar 26, 2009
@p6rt p6rt added the patch label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant