Create a gist now

Instantly share code, notes, and snippets.

@zoffixznet /p6.p6 Secret
Created Aug 8, 2017

What would you like to do?
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