diff options
author | jeg2 <jeg2@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-06-02 14:52:11 +0000 |
---|---|---|
committer | jeg2 <jeg2@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-06-02 14:52:11 +0000 |
commit | fe8002b17bb232b443a1dd6469318527db5b690e (patch) | |
tree | 9bf01aafa8d756e7ec1b6d9c8891afa5e2f08502 /lib | |
parent | fd8010fc0ae51c711d61652516afbd1a80f435a5 (diff) | |
download | ruby-fe8002b17bb232b443a1dd6469318527db5b690e.tar.gz |
* lib/csv.rb: Improve the line ending detection algorithm
(patch by Alexey).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31906 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/csv.rb | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/lib/csv.rb b/lib/csv.rb index c7649b8842..50aba3d91e 100644 --- a/lib/csv.rb +++ b/lib/csv.rb @@ -207,7 +207,7 @@ require "stringio" # 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 @@ class CSV @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 @@ class CSV 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 |