File tree Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -259,6 +259,22 @@ describe "IO::Delimited" do
259
259
io.gets_to_end.should eq(" hello" )
260
260
end
261
261
262
+ it " handles the case of peek matching first byte, not having enough room, but later not matching (limted slice)" do
263
+ # not a delimiter
264
+ # ---
265
+ io = MemoryIOWithFixedPeek .new(" abcdefgwijkfghhello" )
266
+ # ------- ---
267
+ # peek delimiter
268
+ io.peek_size = 7
269
+ delimited = IO ::Delimited .new(io, read_delimiter: " fgh" )
270
+
271
+ delimited.peek.should eq(" abcde" .to_slice)
272
+ delimited.read_string(6 ).should eq " abcdef"
273
+ delimited.read_string(5 ).should eq(" gwijk" )
274
+ delimited.gets_to_end.should eq(" " )
275
+ io.gets_to_end.should eq(" hello" )
276
+ end
277
+
262
278
it " handles the case of peek matching first byte, not having enough room, later only partially matching" do
263
279
# delimiter
264
280
# ------------
Original file line number Diff line number Diff line change @@ -111,11 +111,13 @@ class IO::Delimited < IO
111
111
next_index = @active_delimiter_buffer .index(first_byte, 1 )
112
112
113
113
# We read up to that new match, if any, or the entire buffer
114
- read_bytes = next_index || @active_delimiter_buffer .size
114
+ read_bytes = Math .min( next_index || @active_delimiter_buffer .size, slice.size)
115
115
116
116
slice.copy_from(@active_delimiter_buffer [0 , read_bytes])
117
117
slice += read_bytes
118
118
@active_delimiter_buffer += read_bytes
119
+
120
+ return read_bytes if slice.empty?
119
121
return read_bytes + read_internal(slice)
120
122
end
121
123
end
You can’t perform that action at this time.
0 commit comments