Skip to content

Commit

Permalink
Add support for Supply.split(Str, :skip-empty)
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Jan 12, 2020
1 parent cac64ca commit 4fa60ab
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/core.c/Supply.pm6
Expand Up @@ -674,9 +674,9 @@ my class Supply does Awaitable {
# split the supply on the given string
multi method split(Supply:D: Str(Cool) $the-needle) {
supply {
my str $str;
my str $needle = $the-needle;

my str $str;
whenever self -> str $val {
my $matches := nqp::split($needle,nqp::concat($str,$val));
$str = nqp::pop($matches); # keep last for next batch
Expand All @@ -694,6 +694,35 @@ my class Supply does Awaitable {
}
}

# split the supply on the given string
multi method split(Supply:D: Str(Cool) $the-needle, :$skip-empty!) {
$skip-empty
?? supply {
my str $needle = $the-needle;

my str $str;
my str $emittee;
whenever self -> str $val {
my $matches := nqp::split($needle,nqp::concat($str,$val));
$str = nqp::pop($matches); # keep last for next batch

my $iterator := nqp::iterator($matches);
nqp::while(
$iterator,
nqp::if(
nqp::chars($emittee = nqp::shift($iterator)),
emit nqp::p6box_s($emittee)
)
);

LAST {
emit nqp::p6box_s($str) if nqp::chars($str);
}
}
}
!! self.split($the-needle)
}

##
## Coercions
##
Expand Down

0 comments on commit 4fa60ab

Please sign in to comment.