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
Comments
From @cspencerThis patch removes the PIR implementations of ucfirst, lcfirst, chop |
From @cspencer0001-Added-Perl6-lcfirst-ucfirst-chop-and-fmt-to-Any.pm.patchFrom 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
|
From @pmichaudOn Wed, Mar 11, 2009 at 07:49:16PM -0700, Cory Spencer wrote: We need to be careful about that boolean test for 'self' -- 0.ucfirst I've applied the patch in c48d6a3, after changing the We probably need some spectests to test the edge cases Thanks for the patch! Pm |
The RT System itself - Status changed from 'new' to 'open' |
From @moritzTests are now in t/spec/S32-str/uc.t (and some of them fail, but we have |
@moritz - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#63796 (status was 'resolved')
Searchable as RT63796$
The text was updated successfully, but these errors were encountered: