ruby-changes:19859
From: jeg2 <ko1@a...>
Date: Thu, 2 Jun 2011 23:52:19 +0900 (JST)
Subject: [ruby-changes:19859] jeg2:r31906 (trunk): * lib/csv.rb: Improve the line ending detection algorithm
jeg2 2011-06-02 23:52:11 +0900 (Thu, 02 Jun 2011) New Revision: 31906 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=31906 Log: * lib/csv.rb: Improve the line ending detection algorithm (patch by Alexey). Modified files: trunk/ChangeLog trunk/lib/csv.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 31905) +++ ChangeLog (revision 31906) @@ -1,3 +1,8 @@ +Thu Jun 2 23:51:03 2011 James Edward Gray II <jeg2@r...> + + * lib/csv.rb: Improve the line ending detection algorithm + (patch by Alexey). + Thu Jun 2 20:05:57 2011 NAKAMURA Usaku <usa@r...> * io.c (rb_io_s_write, rb_io_s_binwrite): return!!! Index: lib/csv.rb =================================================================== --- lib/csv.rb (revision 31905) +++ lib/csv.rb (revision 31906) @@ -207,7 +207,7 @@ # class CSV # The version of the installed library. - VERSION = "2.4.7".freeze + VERSION = "2.4.8".freeze # # A CSV::Row is part Array and part Hash. It retains an order for the fields @@ -2038,25 +2038,29 @@ @row_sep = $INPUT_RECORD_SEPARATOR else begin - saved_pos = @io.pos # remember where we were + # + # remember where we were (pos() will raise an axception if @io is pipe + # or not opened for reading) + # + saved_pos = @io.pos while @row_sep == :auto # # if we run out of data, it's probably a single line - # (use a sensible default) + # (ensure will set default value) # - unless sample = @io.gets(nil, 1024) - @row_sep = $INPUT_RECORD_SEPARATOR - break + break unless sample = @io.gets(nil, 1024) + # extend sample if we're unsure of the line ending + if sample.end_with? encode_str("\r") + sample << (@io.gets(nil, 1) || "") end - # read ahead a bit - sample << (@io.gets(nil, 1) || "") if sample.end_with?(encode_str("\r")) # try to find a standard separator if sample =~ encode_re("\r\n?|\n") @row_sep = $& break end end + # tricky seek() clone to work around GzipReader's lack of seek() @io.rewind # reset back to the remembered position @@ -2065,8 +2069,18 @@ saved_pos -= 1024 end @io.read(saved_pos) if saved_pos.nonzero? - rescue IOError # stream not opened for reading - @row_sep = $INPUT_RECORD_SEPARATOR + rescue IOError # not opened for reading + # do nothing: ensure will set default + rescue NoMethodError # Zlib::GzipWriter doesn't have some IO methods + # do nothing: ensure will set default + rescue SystemCallError # pipe + # do nothing: ensure will set default + ensure + # + # set default if we failed to detect + # (stream not opened for reading, a pipe, or a single line of data) + # + @row_sep = $INPUT_RECORD_SEPARATOR if @row_sep == :auto end end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/