module IO::Stream::Readable

def discard_until(pattern, offset = 0, limit: nil)

@returns [String | Nil] The contents of the stream up until the pattern, or nil if the pattern was not found.
@parameter limit [Integer] The maximum number of bytes to read, including the pattern.
@parameter offset [Integer] The offset to start searching from.
@parameter pattern [String] The pattern to match.
Efficiently discard data from the stream until encountering pattern.
def discard_until(pattern, offset = 0, limit: nil)
	if index = index_of(pattern, offset, limit, true)
		@read_buffer.freeze
		
		if limit and index >= limit
			@read_buffer = @read_buffer.byteslice(limit, @read_buffer.bytesize)
			
			return nil
		end
		
		matched = @read_buffer.byteslice(0, index+pattern.bytesize)
		@read_buffer = @read_buffer.byteslice(index+pattern.bytesize, @read_buffer.bytesize)
		
		return matched
	end
end