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
|
retVal
|
||||||
end
|
end
|
||||||
|
|
||||||
def gets(aSepString = $/)
|
def gets(aSepString = $/, numberOfBytes = nil)
|
||||||
@lineno = @lineno.next
|
@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?
|
aSepString = "#{$/}#{$/}" if aSepString.empty?
|
||||||
|
|
||||||
bufferIndex = 0
|
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
|
bufferIndex = [bufferIndex, @outputBuffer.bytesize - aSepString.bytesize].max
|
||||||
if input_finished?
|
if input_finished?
|
||||||
return @outputBuffer.empty? ? nil : flush
|
return @outputBuffer.empty? ? nil : flush
|
||||||
end
|
end
|
||||||
@outputBuffer << produce_input
|
@outputBuffer << produce_input
|
||||||
|
overlimit = (numberOfBytes && @outputBuffer.bytesize >= numberOfBytes)
|
||||||
end
|
end
|
||||||
sepIndex = matchIndex + aSepString.bytesize
|
sepIndex = [matchIndex + aSepString.bytesize, numberOfBytes || @outputBuffer.bytesize].min
|
||||||
return @outputBuffer.slice!(0...sepIndex)
|
return @outputBuffer.slice!(0...sepIndex)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -40,14 +40,14 @@ class AbstractInputStreamTest < Test::Unit::TestCase
|
||||||
@readPointer = 0
|
@readPointer = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
def read(charsToRead)
|
def sysread(charsToRead, buf = nil)
|
||||||
retVal=@contents[@readPointer, charsToRead]
|
retVal=@contents[@readPointer, charsToRead]
|
||||||
@readPointer+=charsToRead
|
@readPointer+=charsToRead
|
||||||
return retVal
|
return retVal
|
||||||
end
|
end
|
||||||
|
|
||||||
def produce_input
|
def produce_input
|
||||||
read(100)
|
sysread(100)
|
||||||
end
|
end
|
||||||
|
|
||||||
def input_finished?
|
def input_finished?
|
||||||
|
@ -87,6 +87,19 @@ class AbstractInputStreamTest < Test::Unit::TestCase
|
||||||
assert_equal(LONG_LINES[2], io.gets("\r\n"))
|
assert_equal(LONG_LINES[2], io.gets("\r\n"))
|
||||||
end
|
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
|
def test_each_line
|
||||||
lineNumber=0
|
lineNumber=0
|
||||||
@io.each_line {
|
@io.each_line {
|
||||||
|
|
Loading…
Reference in New Issue