support gets-with-limit from ruby 1.9
This commit is contained in:
parent
a64b01fe96
commit
11df150531
|
@ -76,20 +76,34 @@ module IOExtras #:nodoc:
|
|||
retVal
|
||||
end
|
||||
|
||||
def gets(aSepString = $/)
|
||||
def gets(aSepString = $/, numberOfBytes = nil)
|
||||
@lineno = @lineno.next
|
||||
return read if aSepString.nil?
|
||||
|
||||
if numberOfBytes.respond_to?(:to_int)
|
||||
numberOfBytes = numberOfBytes.to_int
|
||||
aSepString = aSepString.to_str if aSepString
|
||||
elsif aSepString.respond_to?(:to_int)
|
||||
numberOfBytes = aSepString.to_int
|
||||
aSepString = $/
|
||||
else
|
||||
numberOfBytes = nil
|
||||
aSepString = aSepString.to_str if aSepString
|
||||
end
|
||||
|
||||
return read(numberOfBytes) if aSepString.nil?
|
||||
aSepString = "#{$/}#{$/}" if aSepString.empty?
|
||||
|
||||
bufferIndex = 0
|
||||
while ((matchIndex = @outputBuffer.index(aSepString, bufferIndex)) == nil)
|
||||
overLimit = (numberOfBytes && @outputBuffer.bytesize >= numberOfBytes)
|
||||
while ((matchIndex = @outputBuffer.index(aSepString, bufferIndex)) == nil && !overLimit)
|
||||
bufferIndex = [bufferIndex, @outputBuffer.bytesize - aSepString.bytesize].max
|
||||
if input_finished?
|
||||
return @outputBuffer.empty? ? nil : flush
|
||||
end
|
||||
@outputBuffer << produce_input
|
||||
overlimit = (numberOfBytes && @outputBuffer.bytesize >= numberOfBytes)
|
||||
end
|
||||
sepIndex = matchIndex + aSepString.bytesize
|
||||
sepIndex = [matchIndex + aSepString.bytesize, numberOfBytes || @outputBuffer.bytesize].min
|
||||
return @outputBuffer.slice!(0...sepIndex)
|
||||
end
|
||||
|
||||
|
|
|
@ -40,14 +40,14 @@ class AbstractInputStreamTest < Test::Unit::TestCase
|
|||
@readPointer = 0
|
||||
end
|
||||
|
||||
def read(charsToRead)
|
||||
def sysread(charsToRead, buf = nil)
|
||||
retVal=@contents[@readPointer, charsToRead]
|
||||
@readPointer+=charsToRead
|
||||
return retVal
|
||||
end
|
||||
|
||||
def produce_input
|
||||
read(100)
|
||||
sysread(100)
|
||||
end
|
||||
|
||||
def input_finished?
|
||||
|
@ -87,6 +87,19 @@ class AbstractInputStreamTest < Test::Unit::TestCase
|
|||
assert_equal(LONG_LINES[2], io.gets("\r\n"))
|
||||
end
|
||||
|
||||
def test_getsWithSepAndIndex
|
||||
io = TestAbstractInputStream.new(LONG_LINES.join)
|
||||
assert_equal('x', io.gets("\r\n", 1))
|
||||
assert_equal('x'*47 + "\r", io.gets("\r\n", 48))
|
||||
assert_equal("\n", io.gets(nil, 1))
|
||||
assert_equal('yy', io.gets(nil, 2))
|
||||
end
|
||||
|
||||
def test_getsWithIndex
|
||||
assert_equal(TEST_LINES[0], @io.gets(100))
|
||||
assert_equal('this', @io.gets(4))
|
||||
end
|
||||
|
||||
def test_each_line
|
||||
lineNumber=0
|
||||
@io.each_line {
|
||||
|
|
Loading…
Reference in New Issue