Skip to content

Instantly share code, notes, and snippets.

@zoffixznet

zoffixznet/p6.p6 Secret

Created August 8, 2017 02:55
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/0c2cbd7acaaf0d3b27d245ad2e2bc737 to your computer and use it in GitHub Desktop.
Save zoffixznet/0c2cbd7acaaf0d3b27d245ad2e2bc737 to your computer and use it in GitHub Desktop.
class IO::String is IO::Handle {
has Str $.buffer = '';
has Int $.pos = 0;
method get(IO::String:D:) {
return Nil if $!pos >= $.buffer.chars;
my $start = $!pos;
my $next-nl = [min] $.nl-in.map({
$_ => $^nl with $.buffer.index($^nl, $start)
}).grep(*.key.defined);
if $next-nl ~~ Num { # if it's Inf, then there's no minimum
$!pos = $.buffer.chars;
return $.buffer.substr($start);
}
my $chars = 1 + $next-nl.key - $start;
$!pos += $chars;
$chars -= $next-nl.value.chars if $.chomp;
$.buffer.substr($start, $chars);
}
}
dd IO::String.new(buffer => "hello,\r\nworld!\r\n").get;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment