-
-
Save zoffixznet/4d20494d4ad3bbacd10f30e28f634ab7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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