Refactored two code paths of GridIO#gets into well-named methods

This commit is contained in:
Ryan McGeary 2011-05-03 11:54:18 -04:00
parent c672168236
commit 7f384ebd51
1 changed files with 44 additions and 36 deletions

View File

@ -208,43 +208,9 @@ module Mongo
elsif separator.is_a?(Integer)
read_length(separator)
elsif separator.length > 1
result = ''
len = 0
match_idx = 0
match_num = separator.length - 1
to_match = separator[match_idx].chr
if length
matcher = lambda {|idx, num| idx < num && len < length }
else
matcher = lambda {|idx, num| idx < num}
end
while matcher.call(match_idx, match_num) && char = getc
result << char
len += 1
if char == to_match
while match_idx < match_num do
match_idx += 1
to_match = separator[match_idx].chr
char = getc
result << char
if char != to_match
match_idx = 0
to_match = separator[match_idx].chr
break
end
end
end
end
result.length > 0 ? result : nil
read_to_string(separator, length)
else
result = ''
len = 0
while char = getc
result << char
len += 1
break if char == separator || (length ? len >= length : false)
end
result.length > 0 ? result : nil
read_to_character(separator, length)
end
end
@ -359,6 +325,48 @@ module Mongo
buf
end
def read_to_character(character="\n", length=nil)
result = ''
len = 0
while char = getc
result << char
len += 1
break if char == character || (length ? len >= length : false)
end
result.length > 0 ? result : nil
end
def read_to_string(string="\n", length=nil)
result = ''
len = 0
match_idx = 0
match_num = string.length - 1
to_match = string[match_idx].chr
if length
matcher = lambda {|idx, num| idx < num && len < length }
else
matcher = lambda {|idx, num| idx < num}
end
while matcher.call(match_idx, match_num) && char = getc
result << char
len += 1
if char == to_match
while match_idx < match_num do
match_idx += 1
to_match = string[match_idx].chr
char = getc
result << char
if char != to_match
match_idx = 0
to_match = string[match_idx].chr
break
end
end
end
end
result.length > 0 ? result : nil
end
def cache_chunk_data
@current_chunk_data = @current_chunk['data'].to_s
if @current_chunk_data.respond_to?(:force_encoding)