Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
trim-trailing, trim
  • Loading branch information
moritz committed Aug 8, 2011
1 parent ed3094f commit b6e553a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/core/Str.pm
Expand Up @@ -404,6 +404,23 @@ my class Str does Stringy {
);
self.substr($pos);
}


method trim-trailing(Str:D:) {
my Int $pos = self.chars - 1;
--$pos while $pos >= 0 && nqp::p6bool(
nqp::iscclass(
pir::const::CCLASS_WHITESPACE,
nqp::unbox_s(self),
nqp::unbox_i($pos)
)
);
$pos < 0 ?? '' !! self.substr(0, $pos + 1);
}

method trim(Str:D:) {
self.trim-leading.trim-trailing;
}
}


Expand Down Expand Up @@ -471,3 +488,8 @@ multi sub ords(Str $s) {
my str $ns = nqp::unbox_s($s);
(^$c).map: { nqp::p6box_i(nqp::ord(nqp::substr($ns, $_, 1))) }
}

# TODO: Cool variants
sub trim (Str:D $s) { $s.trim }
sub trim-leading (Str:D $s) { $s.trim-leading }
sub trim-trailing(Str:D $s) { $s.trim-trailing }
2 changes: 1 addition & 1 deletion t/spectest.data
Expand Up @@ -560,7 +560,7 @@ S32-str/samecase.t # icu
S32-str/split.t
S32-str/sprintf.t
# S32-str/substr.t # err: Method 'capitalize' not found for invocant of class 'Str'
# S32-str/trim.t # err: Could not find sub &trim
S32-str/trim.t
S32-str/uc.t # icu
S32-str/ucfirst.t # icu
# S32-str/unpack.t # err: Default constructor only takes named arguments
Expand Down

0 comments on commit b6e553a

Please sign in to comment.