Took out useless @matched_item variable; some small refactoring.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4545 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
dblack 2003-09-11 22:06:14 +00:00
Родитель fb65eed72e
Коммит e7653fa093
1 изменённых файлов: 18 добавлений и 18 удалений

Просмотреть файл

@ -304,8 +304,7 @@ module Scanf
class FormatSpecifier class FormatSpecifier
attr_reader :re_string, :matched_string, :conversion attr_reader :re_string, :matched_string, :conversion, :matched
attr_writer :i
private private
@ -456,12 +455,14 @@ module Scanf
end end
def match(str) def match(str)
@matched = false
s = str.dup s = str.dup
s.sub!(/\A\s+/,'') unless count_space? s.sub!(/\A\s+/,'') unless count_space?
res = to_re.match(s) res = to_re.match(s)
if res if res
@conversion = send(@handler, res[1]) @conversion = send(@handler, res[1])
@matched_string = @matched_item.to_s @matched_string = @conversion.to_s
@matched = true
end end
res res
end end
@ -472,22 +473,24 @@ module Scanf
def width def width
w = /%\*?(\d+)/.match(@spec_string).to_a[1] w = /%\*?(\d+)/.match(@spec_string).to_a[1]
w && w.to_i || 0 w && w.to_i
end end
def mid_match? def mid_match?
cc_no_width = letter == '[' && width.zero? return false unless @matched
c_or_cc_width = (letter == 'c' || letter == '[') &&! width.zero? cc_no_width = letter == '[' &&! width
c_or_cc_open = c_or_cc_width && (matched_string.size < width) c_or_cc_width = (letter == 'c' || letter == '[') && width
width_left = c_or_cc_width && (matched_string.size < width)
return c_or_cc_open || cc_no_width
return width_left || cc_no_width
end end
end end
class FormatString class FormatString
attr_reader :string_left, :last_spec_tried, :last_match_tried, :matched_count, :space attr_reader :string_left, :last_spec_tried,
:last_match_tried, :matched_count, :space
SPECIFIERS = 'diuXxofeEgsc' SPECIFIERS = 'diuXxofeEgsc'
REGEX = / REGEX = /
@ -512,6 +515,7 @@ module Scanf
def initialize(str) def initialize(str)
@specs = [] @specs = []
@i = 1
s = str.to_s s = str.to_s
return unless /\S/.match(s) return unless /\S/.match(s)
@space = true if /\s\z/.match(s) @space = true if /\s\z/.match(s)
@ -566,16 +570,12 @@ class IO
# That's why this is much more elaborate than the string # That's why this is much more elaborate than the string
# version. # version.
# #
# For each line:
# Match succeeds (non-emptily) # Match succeeds (non-emptily)
# and the last attempted spec/string sub-match succeeded: # and the last attempted spec/string sub-match succeeded:
# #
# is the current matched spec a '%[...]' or '%c' with a width? # could the last spec keep matching?
# yes: is current.string.size < available width? # yes: save interim results and continue (next line)
# yes: save interim results
# no: width is used up, so move on (next)
# no: is it a '%[...]' with no width?
# yes: evidently nothing violated it yet, so store
# interim results and continue (next)
# #
# The last attempted spec/string did not match: # The last attempted spec/string did not match:
# #
@ -611,7 +611,7 @@ class IO
spec = fstr.last_spec_tried spec = fstr.last_spec_tried
if fstr.last_match_tried if spec.matched
if spec.mid_match? if spec.mid_match?
result_buffer.replace(current_match) result_buffer.replace(current_match)
next next