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

* 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
This commit is contained in:
jeg2 2011-06-02 14:52:11 +00:00
parent fd8010fc0a
commit fe8002b17b
2 changed files with 29 additions and 10 deletions

View file

@ -1,3 +1,8 @@
Thu Jun 2 23:51:03 2011 James Edward Gray II <jeg2@ruby-lang.org>
* lib/csv.rb: Improve the line ending detection algorithm
(patch by Alexey).
Thu Jun 2 20:05:57 2011 NAKAMURA Usaku <usa@ruby-lang.org>
* io.c (rb_io_s_write, rb_io_s_binwrite): return!!!

View file

@ -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