Skip to content

Instantly share code, notes, and snippets.

@zoffixznet

zoffixznet/p6.p6 Secret

Created April 17, 2017 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoffixznet/4d20494d4ad3bbacd10f30e28f634ab7 to your computer and use it in GitHub Desktop.
Save zoffixznet/4d20494d4ad3bbacd10f30e28f634ab7 to your computer and use it in GitHub Desktop.
proto method slurp() { * }
multi method slurp(IO::Path:D:) {
# clean call, try the fast way
if nqp::iseq_i(nqp::elems(nqp::getattr(%_,Map,'$!storage')),0)
&& nqp::open(self.absolute,"r") -> $PIO {
LEAVE nqp::closefh(nqp::decont($PIO));
nqp::p6box_s(nqp::readallfh(nqp::decont($PIO)))
}
# need to do the slow way
else {
my $handle = self.open;
$handle // $handle.throw;
LEAVE $handle.close;
my Mu $PIO := nqp::getattr(nqp::decont($handle),IO::Handle,'$!PIO');
if %_<bin> {
my $res;
# normal file
if Rakudo::Internals.FILETEST-S(self.absolute) -> int $size {
$res := nqp::readfh($PIO,buf8.new,$size)
}
# spooky file with zero size?
else {
$res := buf8.new();
loop {
my $buf := nqp::readfh($PIO,buf8.new,0x100000);
last unless nqp::elems($buf);
$res.append($buf);
}
}
$res
}
else {
$handle.encoding($_) with %_<enc>;
nqp::p6box_s(nqp::readallfh($PIO))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment