Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implement limit for Str.split
  • Loading branch information
moritz committed Jul 24, 2011
1 parent 3867e02 commit 0cbd875
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/core/Cool.pm
Expand Up @@ -88,6 +88,9 @@ my class Cool {

method ords(Cool:D:) { self.Str.ords }
proto method split(|$) {*}
multi method split(Regex $pat, $limit = $Inf, :$all) {
self.Stringy.split($pat, $limit, :$all);
}
proto method match(|$) {*}
multi method match(Cool:D: Cool $target, *%adverbs) {
self.Str.match($target.Stringy, |%adverbs)
Expand Down Expand Up @@ -132,3 +135,6 @@ sub sprintf(Cool $format, *@args) {

sub printf(Cool $format, *@args) { print sprintf $format, @args };
sub samecase(Cool $string, Cool $pattern) { $string.samecase($pattern) }
sub split(Regex $pat, Cool $target, $limit = $Inf, :$all) {
$target.split($pat, $limit, :$all);
}
5 changes: 3 additions & 2 deletions src/core/Str.pm
Expand Up @@ -306,8 +306,9 @@ my class Str does Stringy {
}
}

multi method split(Regex $pat, :$all) {
my @matches := self.match($pat, :g);
multi method split(Regex $pat, $limit = *, :$all) {
my $l = $limit ~~ Whatever ?? $Inf !! $limit - 1;
my @matches := self.match($pat, :x(1..$l), :g);
gather {
my $prev-pos = 0;
for @matches {
Expand Down

0 comments on commit 0cbd875

Please sign in to comment.