Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Introduce IO::Handle.supply
Change an open IO::Handle into an on-demand Supply, so you can now
also say: "words".IO.open.supply.lines.tap: { .say } .  TIMTOWTDI
  • Loading branch information
lizmat committed Sep 28, 2015
1 parent 9e8b7b4 commit 3b1aa2e
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/core/IO/Handle.pm
Expand Up @@ -464,6 +464,30 @@ my class IO::Handle does IO {
$buf;
}

method supply(IO::Handle:D: :$size = 65536, :$bin) {
if $bin {
supply {
my $buf := buf8.new;
my int $bytes = $size;
nqp::readfh($!PIO, $buf, $bytes);
while nqp::elems($buf) {
emit $buf;
nqp::readfh($!PIO, $buf, $bytes);
}
}
}
else {
supply {
my int $chars = $size;
my str $str = nqp::readcharsfh($!PIO,$chars);
while nqp::chars($str) {
emit nqp::p6box_s($str);
$str = nqp::readcharsfh($!PIO,$chars);
}
}
}
}

# second arguemnt should probably be an enum
# valid values for $whence:
# 0 -- seek from beginning of file
Expand Down

0 comments on commit 3b1aa2e

Please sign in to comment.