From 24aac11a56933f55149ead00b2dfab18f4facf16 Mon Sep 17 00:00:00 2001 From: dblack Date: Tue, 2 Mar 2004 15:59:30 +0000 Subject: [PATCH] * 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 --- lib/scanf.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/scanf.rb b/lib/scanf.rb index 1662d08e6c..aeff61c115 100644 --- a/lib/scanf.rb +++ b/lib/scanf.rb @@ -587,6 +587,7 @@ class IO # no: continue [this state could be analyzed further] # # + def scanf(str,&b) return block_scanf(str,&b) if b return [] unless str.size > 0 @@ -606,6 +607,7 @@ class IO end source_buffer << gets + current_match = fstr.match(source_buffer) spec = fstr.last_spec_tried @@ -632,7 +634,6 @@ class IO break if fstr.last_spec fstr.prune end - seek(start_position + matched_so_far, IO::SEEK_SET) rescue Errno::ESPIPE soak_up_spaces if fstr.last_spec && fstr.space @@ -647,15 +648,18 @@ class IO until eof ||! c || /\S/.match(c.chr) c = getc end - ungetc(c) if c + ungetc(c) if (c && /\S/.match(c.chr)) end def block_scanf(str) 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 current = scanf(str) final.push(yield(current)) unless current.empty? - end until current.empty? || eof + end until eof || current.size < fstr.spec_count return final end end