Skip to content

Commit e6c033d

Browse files
author
Ary Borenszweig
committed
Optimized StringIO#gets(Char)
1 parent da90aa0 commit e6c033d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/io/string_io.cr

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ class StringIO
3636
count
3737
end
3838

39+
def gets(delimiter : Char)
40+
if delimiter.ord >= 128
41+
return super
42+
end
43+
44+
index = (@buffer + @pos).to_slice(@bytesize - @pos).index(delimiter.ord)
45+
if index
46+
index += 1
47+
else
48+
index = @bytesize - @pos
49+
return nil if index == 0
50+
end
51+
52+
string = String.new(@buffer + @pos, index)
53+
@pos += index
54+
string
55+
end
56+
3957
def clear
4058
@bytesize = 0
4159
end

0 commit comments

Comments
 (0)