Skip to content

Commit

Permalink
Merge branch 'move_split_to_any_str'
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed Feb 25, 2009
2 parents 5944501 + 67fc2fb commit 7f8ba6f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 79 deletions.
2 changes: 1 addition & 1 deletion build/Makefile.in
Expand Up @@ -106,7 +106,7 @@ BUILTINS_PIR = \
src/builtins/traits.pir \

SETTING = \
src/setting/Any.pm \
src/setting/Any-str.pm \
src/setting/Array.pm \
src/setting/Bool.pm \
src/setting/Hash.pm \
Expand Down
7 changes: 0 additions & 7 deletions src/builtins/any-str.pir
Expand Up @@ -455,13 +455,6 @@ B<Note:> partial implementation only

=cut

.namespace[]
.sub 'split' :multi(_,_)
.param pmc sep
.param pmc target
.tailcall target.'split'(sep)
.end

=item substr()

=cut
Expand Down
67 changes: 67 additions & 0 deletions src/setting/Any-str.pm
@@ -0,0 +1,67 @@
class Any is also {
our List multi method split(Code $delimiter, $limit = *) {
my $s = ~self;
my $l = $limit ~~ Whatever ?? Inf !! $limit;
my $keep = '';
return gather {
while $l > 1 && $s ~~ $delimiter {
take $keep ~ $s.substr(0, $/.from);
if $/.from == $/.to {
$keep = $s.substr($/.to, 1);
$s.=substr($/.to + 1);
} else {
$keep = '';
$s.=substr($/.to)
}
$l--;
}
take $keep ~ $s if $l > 0;
}
}

# TODO: substitute with '$delimiter as Str' once coercion is implemented
our List multi method split($delimiter, $limit = *) {
my Int $prev = 0;
my $l = $limit ~~ Whatever ?? Inf !! $limit;
my $s = ~self;
if $delimiter eq '' {
return gather {
take $s.substr($_, 1) for 0 .. $s.chars - 1;
}
}
return gather {
my $pos = 0;
while $l > 1
&& $pos < $s.chars
&& defined ($pos = $s.index($delimiter, $prev)) {
take $s.substr($prev, $pos - $prev);
$prev = [max] 1 + $prev, $pos + (~$delimiter).chars;
$l--;
}
take $s.substr($prev) if $l > 0;
}
}

our List multi method comb (Code $matcher = /\S+/, $limit = *) {
my $l = $limit ~~ Whatever ?? Inf !! $limit;
# currently we use a copy of self and destroy it piece by piece.
# the preferred way of doing it is using self, not destroying it,
# and use the :pos modifier to the regex. That way the offsets into
# self will be right
my $s = ~self;
return gather {
while $l > 0 && $s ~~ $matcher {
# if we have captures, return the actual match object
take @($/) || %($/) ?? $/.clone !! ~$/;
$l--;
$s.=substr([max] 1, $/.to);
}
}
}
}

sub split($delimiter, $target) {
$target.split($delimiter);
}

# vim: ft=perl6
11 changes: 0 additions & 11 deletions src/setting/Any.pm

This file was deleted.

60 changes: 0 additions & 60 deletions src/setting/Str.pm
Expand Up @@ -2,66 +2,6 @@ class Str is also {
our Str multi method reverse ($str: ) is export {
return $str.split('').reverse.join('');
}

our List multi method split(Code $delimiter, $limit = *) {
my $s = self;
my $l = $limit ~~ Whatever ?? Inf !! $limit;
my $keep = '';
return gather {
while $l > 1 && $s ~~ $delimiter {
take $keep ~ $s.substr(0, $/.from);
if $/.from == $/.to {
$keep = $s.substr($/.to, 1);
$s.=substr($/.to + 1);
} else {
$keep = '';
$s.=substr($/.to)
}
$l--;
}
take $keep ~ $s if $l > 0;
}
}

# TODO: substitute with '$delimiter as Str' once coercion is implemented
our List multi method split($delimiter is copy, $limit = *) {
my Int $prev = 0;
my $l = $limit ~~ Whatever ?? Inf !! $limit;
$delimiter = ~$delimiter;
if $delimiter eq '' {
return gather {
take self.substr($_, 1) for 0 .. self.chars - 1;
}
}
return gather {
my $pos;
while $l > 1
&& $pos < self.chars
&& defined ($pos = self.index($delimiter, $prev)) {
take self.substr($prev, $pos - $prev);
$prev = [max] 1 + $prev, $pos + $delimiter.chars;
$l--;
}
take self.substr($prev) if $l > 0;
}
}

our List multi method comb (Code $matcher = /\S+/, $limit = *) {
my $l = $limit ~~ Whatever ?? Inf !! $limit;
# currently we use a copy of self and destroy it piece by piece.
# the preferred way of doing it is using self, not destroying it,
# and use the :pos modifier to the regex. That way the offsets into
# self will be right
my $s = self;
return gather {
while $l > 0 && $s ~~ $matcher {
# if we have captures, return the actual match object
take @($/) || %($/) ?? $/.clone !! ~$/;
$l--;
$s.=substr([max] 1, $/.to);
}
}
}
}

# vim: ft=perl6

0 comments on commit 7f8ba6f

Please sign in to comment.