Skip to content

Commit 6ffd258

Browse files
hsbtclaude
andcommitted
Trim the comments added with the readuntil rewind
The floor's two reasons fit in one block, and the two tests that restated them are named for what they cover. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent e1b38d9 commit 6ffd258

2 files changed

Lines changed: 6 additions & 14 deletions

File tree

‎lib/net/protocol.rb‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,10 @@ def readuntil(terminator, ignore_eof = false, limit: nil)
240240
if limit && rbuf_size > limit
241241
raise ReadLimitExceeded, "exceeded the #{limit} byte read limit"
242242
end
243-
# Rewind by terminator.bytesize - 1 so that a terminator split
244-
# across reads is not missed, however many reads it spans.
245-
# @rbuf_offset is the floor for two reasons. A negative offset
246-
# makes String#index search relative to the end of the buffer,
247-
# skipping a match near its start. An offset below @rbuf_offset
248-
# matches a terminator beginning inside bytes already returned
249-
# to the caller, yielding a slice that does not end with one.
243+
# Rewind so a terminator split across reads is still found. The
244+
# floor guards two things. String#index reads a negative offset
245+
# as counting from the end, and an offset below @rbuf_offset
246+
# matches inside bytes already returned.
250247
offset = [@rbuf.bytesize - terminator.bytesize + 1, @rbuf_offset].max
251248
rbuf_fill
252249
end

‎test/net/protocol/test_protocol.rb‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,8 @@ def test_write0_timeout_multi2
239239

240240
class FakeReadPartialIO
241241
def initialize(chunks)
242-
# Binary, like the bytes a real IO hands back. String#b also copies,
243-
# which matters because rbuf_fill clears a string read_nonblock
244-
# returns without having been handed it as the buffer.
242+
# Binary, like a real IO. String#b also copies, which matters
243+
# because rbuf_fill clears a string it was not handed as the buffer.
245244
@chunks = chunks.map(&:b)
246245
end
247246

@@ -295,17 +294,13 @@ def test_readuntil_terminator_spanning_more_than_two_chunks # https://github.com
295294
def test_readuntil_clamps_a_negative_rewind # https://github.com/ruby/net-protocol/pull/66
296295
fake_io = FakeReadPartialIO.new(["ab\n"])
297296
io = Net::BufferedIO.new(fake_io)
298-
# Any buffer shorter than the terminator drives the rewind below zero,
299-
# and String#index reads a negative offset as counting from the end.
300297
assert_equal "ab", io.readuntil("ab")
301298
end
302299

303300
def test_readuntil_does_not_rewind_into_consumed_bytes # https://github.com/ruby/net-protocol/pull/66
304301
fake_io = FakeReadPartialIO.new(["ab\r\n\r", "\nc"])
305302
io = Net::BufferedIO.new(fake_io)
306303
assert_equal "ab\r", io.readuntil("\r")
307-
# The terminator is longer than what is left unconsumed, so the rewind
308-
# would reach back into the bytes readuntil already returned.
309304
assert_raise(EOFError) { io.readuntil("\r\n\r\n") }
310305
end
311306

0 commit comments

Comments
 (0)