-
-
Save zoffixznet/0c2cbd7acaaf0d3b27d245ad2e2bc737 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
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