Skip to content

Commit

Permalink
Move ucfirst, lcfirst, chop, and fmt into settings. (RT #63796, cspen…
Browse files Browse the repository at this point in the history
…cer++)

Patch courtesy Cory Spencer <cspencer  at sprocket.org>, with minor
modifications.
  • Loading branch information
pmichaud committed Mar 12, 2009
1 parent ea32839 commit c48d6a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 101 deletions.
102 changes: 1 addition & 101 deletions src/builtins/any-str.pir
Expand Up @@ -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
Expand Down Expand Up @@ -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: )
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 )
Expand Down
17 changes: 17 additions & 0 deletions src/setting/Any-str.pm
@@ -1,4 +1,16 @@
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 gt '' ?? self.substr(0,1).lc ~ self.substr(1) !! ""
}

our List multi method split(Code $delimiter, $limit = *) {
my $s = ~self;
my $l = $limit ~~ Whatever ?? Inf !! $limit;
Expand Down Expand Up @@ -58,6 +70,11 @@ class Any is also {
}
}
}

our Str multi method ucfirst is export {
self gt '' ?? self.substr(0,1).uc ~ self.substr(1) !! ""
}

}

sub split($delimiter, $target) {
Expand Down

0 comments on commit c48d6a3

Please sign in to comment.