Navigation Menu

Skip to content

Commit

Permalink
add Any.split(stuff) and Str.split(Str)
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed Feb 23, 2009
1 parent 1fa37c0 commit 215ad17
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 49 deletions.
1 change: 1 addition & 0 deletions build/Makefile.in
Expand Up @@ -106,6 +106,7 @@ BUILTINS_PIR = \
src/builtins/traits.pir \

SETTING = \
src/setting/Any.pm \
src/setting/Array.pm \
src/setting/Bool.pm \
src/setting/Hash.pm \
Expand Down
48 changes: 0 additions & 48 deletions src/builtins/any-str.pir
Expand Up @@ -503,54 +503,6 @@ B<Note:> partial implementation only
.tailcall target.'split'(sep)
.end
.namespace['Any']
.sub 'split' :method :multi(_, _)
.param string delim
.param int count :optional
.param int has_count :opt_flag
.local string objst
.local pmc pieces
.local pmc retv
.local int len
.local int pos
.local int i
retv = new 'List'
# per Perl 5's negative LIMIT behavior
unless has_count goto positive_count
unless count < 1 goto positive_count
has_count = 0

positive_count:
objst = self
length $I0, delim
split pieces, delim, objst
len = pieces
pos = 0
i = 0

loop:
unless has_count goto skip_count
dec count
unless count < 1 goto skip_count
$S0 = substr objst, pos
retv.'push'($S0)
goto done
skip_count:
if i == len goto done
$S0 = pieces[i]
retv.'push'($S0)
length $I1, $S0
pos += $I0
pos += $I1
inc i
goto loop

done:
.return(retv)
.end

=item substr()
=cut
Expand Down
11 changes: 11 additions & 0 deletions src/setting/Any.pm
@@ -0,0 +1,11 @@
class Any is also {
method split($delimiter, Int $limit = *) {
if $limit ~~ Whatever {
return (~self).split($delimiter);
} else {
return (~self).split($delimiter, $limit);
}
}
}

# vim: ft=perl6
16 changes: 15 additions & 1 deletion src/setting/Str.pm
Expand Up @@ -15,9 +15,23 @@ class Str is also {
}
}

our List multi method split(Code $delimiter, Int $limit) {
our List multi method split($delimiter, Int $limit) {
self.split($delimiter).[0..$limit];
}

# TODO: substitute with $delimiter as Str once coercion is implemented
our List multi method split($delimiter is copy) {
my Int $prev = 0;
$delimiter = ~$delimiter;
return gather {
my $pos;
while defined ($pos = self.index($delimiter, $prev)) {
take self.substr($prev, $pos - $prev);
$prev = $pos + $delimiter.chars;
}
take self.substr($prev);
}
}
}

# vim: ft=perl6

0 comments on commit 215ad17

Please sign in to comment.