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 Perl6 versions of ucfirst, lcfirst, chop and fmt to Any.pm #768

Closed
p6rt opened this issue Mar 12, 2009 · 6 comments
Closed

Add Perl6 versions of ucfirst, lcfirst, chop and fmt to Any.pm #768

p6rt opened this issue Mar 12, 2009 · 6 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Mar 12, 2009

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

Searchable as RT63796$

@p6rt
Copy link
Author

p6rt commented Mar 12, 2009

From @cspencer

This patch removes the PIR implementations of ucfirst, lcfirst, chop
and fmt from any-str.pir and replaces them with Perl 6 versions in Any-
str.pm.

@p6rt
Copy link
Author

p6rt commented Mar 12, 2009

From @cspencer

0001-Added-Perl6-lcfirst-ucfirst-chop-and-fmt-to-Any.pm.patch
From f34205a4276b8f121895e8aa1d0b4fd5bfed5d89 Mon Sep 17 00:00:00 2001
From: Cory Spencer <cspencer@sprocket.org>
Date: Wed, 11 Mar 2009 19:45:32 -0700
Subject: [PATCH] Added Perl6 lcfirst, ucfirst, chop and fmt to Any.pm

---
 src/builtins/any-str.pir |  102 +---------------------------------------------
 src/setting/Any-str.pm   |   16 +++++++
 2 files changed, 17 insertions(+), 101 deletions(-)

diff --git a/src/builtins/any-str.pir b/src/builtins/any-str.pir
index 6cce3e6..1535b3d 100644
--- a/src/builtins/any-str.pir
+++ b/src/builtins/any-str.pir
@@ -23,7 +23,7 @@ the size of that file down and to emphasize their generic,
 .namespace []
 .sub 'onload' :anon :init :load
     $P0 = get_hll_namespace ['Any']
-    '!EXPORT'('capitalize,chop,chomp,chars,:d,:e,:f,index,lc,lcfirst,rindex,ord,substr,trim,uc,ucfirst,unpack', 'from'=>$P0)
+    '!EXPORT'('capitalize,chomp,chars,:d,:e,:f,index,lc,rindex,ord,substr,trim,uc,unpack', 'from'=>$P0)
 .end
 
 
@@ -81,26 +81,6 @@ C<s:g/(\w+)/{ucfirst $1}/> on it.
     .return ($I0)
 .end
 
-
-=item chop
-
- our Str method Str::chop ( Str  $string: )
-
-Returns string with one Char removed from the end.
-
-=cut
-
-.sub 'chop' :method :multi(_)
-    .local string tmps
-    .local pmc retv
-
-    tmps = self
-    chopn tmps, 1
-    retv = new 'Str'
-    retv = tmps
-    .return(retv)
-.end
-
 =item chomp
 
  our Str method Str::chomp ( Str $string: )
@@ -240,23 +220,6 @@ file.
     .return ($P0)
 .end
 
-=item fmt
-
- our Str multi Any::fmt ( Str $format )
-
-Returns the invocant formatted by an implicit call to C<sprintf>.
-
-=cut
-
-.sub 'fmt' :method :multi(_)
-    .param string format
-    .local pmc retv
-
-    retv = 'sprintf'(format, self)
-
-    .return(retv)
-.end
-
 =item index()
 
 =cut
@@ -320,38 +283,6 @@ form, if uppercase.
     .return(retv)
 .end
 
-=item lcfirst
-
- our Str multi Str::lcfirst ( Str $string )
-
-Like C<lc>, but only affects the first character.
-
-=cut
-
-.sub 'lcfirst' :method :multi(_)
-    .local string tmps
-    .local string fchr
-    .local pmc retv
-    .local int len
-
-    retv = new 'Str'
-    tmps = self
-
-    len = length tmps
-    if len == 0 goto done
-
-    substr fchr, tmps, 0, 1
-    downcase fchr
-
-    concat retv, fchr
-    substr tmps, tmps, 1
-    concat retv, tmps
-
-  done:
-    .return(retv)
-.end
-
-
 
 =item match()
 
@@ -1075,37 +1006,6 @@ full "uppercase".
     .return(retv)
 .end
 
-=item ucfirst
-
- our Str multi Str::ucfirst ( Str $string )
-
-Performs a Unicode "titlecase" operation on the first character of the string.
-
-=cut
-
-.sub 'ucfirst' :method :multi(_)
-    .local string tmps
-    .local string fchr
-    .local pmc retv
-    .local int len
-
-    retv = new 'Str'
-    tmps = self
-
-    len = length tmps
-    if len == 0 goto done
-
-    substr fchr, tmps, 0, 1
-    upcase fchr
-
-    concat retv, fchr
-    substr tmps, tmps, 1
-    concat retv, tmps
-
-  done:
-    .return(retv)
-.end
-
 =item unpack
 
  our List multi Str::unpack ( Str $template, Str $packval )
diff --git a/src/setting/Any-str.pm b/src/setting/Any-str.pm
index 08a2408..3813b15 100644
--- a/src/setting/Any-str.pm
+++ b/src/setting/Any-str.pm
@@ -1,4 +1,20 @@
 class Any is also {
+    our Str multi method chop is export {
+        self.substr(0, -1)
+    }
+
+    our Str multi method fmt(Str $format) {
+        sprintf($format, self)
+    }
+
+    our Str multi method lcfirst is export {
+        self ?? self.substr(0,1).lc ~ self.substr(1) !! ""
+    }
+
+    our Str multi method ucfirst is export {
+        self ?? self.substr(0,1).uc ~ self.substr(1) !! ""
+    }
+
     our List multi method split(Code $delimiter, $limit = *) {
         my $s = ~self;
         my $l = $limit ~~ Whatever ?? Inf !! $limit;
-- 
1.6.1.3

@p6rt
Copy link
Author

p6rt commented Mar 12, 2009

From @pmichaud

On Wed, Mar 11, 2009 at 07​:49​:16PM -0700, Cory Spencer wrote​:
+ our Str multi method lcfirst is export {
+ self ?? self.substr(0,1).lc ~ self.substr(1) !! ""
+ }
+
+ our Str multi method ucfirst is export {
+ self ?? self.substr(0,1).uc ~ self.substr(1) !! ""
+ }

We need to be careful about that boolean test for 'self' --
what about things like...

  0.ucfirst
  ('hello' but False).ucfirst
  ('' but True).ucfirst

I've applied the patch in c48d6a3, after changing the
test on C<self> to C< self gt '' > .

We probably need some spectests to test the edge cases
I've identified above, so I'll leave the ticket open until
that's done.

Thanks for the patch!

Pm

@p6rt
Copy link
Author

p6rt commented Mar 12, 2009

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

@p6rt
Copy link
Author

p6rt commented Mar 12, 2009

From @moritz

Tests are now in t/spec/S32-str/uc.t (and some of them fail, but we have
RT #​63816 for that, so closing this ticket.

@p6rt
Copy link
Author

p6rt commented Mar 12, 2009

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

@p6rt p6rt closed this as completed Mar 12, 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