1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* soak_up_spaces only ungetc's non-space last character

* IO#block_scanf now returns partial last iteration
  array if format string matches partly


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5874 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
dblack 2004-03-02 15:59:30 +00:00
parent 760691ca01
commit 24aac11a56

View file

@ -587,6 +587,7 @@ class IO
# no: continue [this state could be analyzed further] # no: continue [this state could be analyzed further]
# #
# #
def scanf(str,&b) def scanf(str,&b)
return block_scanf(str,&b) if b return block_scanf(str,&b) if b
return [] unless str.size > 0 return [] unless str.size > 0
@ -606,6 +607,7 @@ class IO
end end
source_buffer << gets source_buffer << gets
current_match = fstr.match(source_buffer) current_match = fstr.match(source_buffer)
spec = fstr.last_spec_tried spec = fstr.last_spec_tried
@ -632,7 +634,6 @@ class IO
break if fstr.last_spec break if fstr.last_spec
fstr.prune fstr.prune
end end
seek(start_position + matched_so_far, IO::SEEK_SET) rescue Errno::ESPIPE seek(start_position + matched_so_far, IO::SEEK_SET) rescue Errno::ESPIPE
soak_up_spaces if fstr.last_spec && fstr.space soak_up_spaces if fstr.last_spec && fstr.space
@ -647,15 +648,18 @@ class IO
until eof ||! c || /\S/.match(c.chr) until eof ||! c || /\S/.match(c.chr)
c = getc c = getc
end end
ungetc(c) if c ungetc(c) if (c && /\S/.match(c.chr))
end end
def block_scanf(str) def block_scanf(str)
final = [] final = []
# Sub-ideal, since another FS gets created in scanf.
# But used here to determine the number of specifiers.
fstr = Scanf::FormatString.new(str)
begin begin
current = scanf(str) current = scanf(str)
final.push(yield(current)) unless current.empty? final.push(yield(current)) unless current.empty?
end until current.empty? || eof end until eof || current.size < fstr.spec_count
return final return final
end end
end end