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:
parent
fd8010fc0a
commit
fe8002b17b
2 changed files with 29 additions and 10 deletions
|
@ -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>
|
Thu Jun 2 20:05:57 2011 NAKAMURA Usaku <usa@ruby-lang.org>
|
||||||
|
|
||||||
* io.c (rb_io_s_write, rb_io_s_binwrite): return!!!
|
* io.c (rb_io_s_write, rb_io_s_binwrite): return!!!
|
||||||
|
|
34
lib/csv.rb
34
lib/csv.rb
|
@ -207,7 +207,7 @@ require "stringio"
|
||||||
#
|
#
|
||||||
class CSV
|
class CSV
|
||||||
# The version of the installed library.
|
# 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
|
# 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
|
@row_sep = $INPUT_RECORD_SEPARATOR
|
||||||
else
|
else
|
||||||
begin
|
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
|
while @row_sep == :auto
|
||||||
#
|
#
|
||||||
# if we run out of data, it's probably a single line
|
# 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)
|
break unless sample = @io.gets(nil, 1024)
|
||||||
@row_sep = $INPUT_RECORD_SEPARATOR
|
# extend sample if we're unsure of the line ending
|
||||||
break
|
if sample.end_with? encode_str("\r")
|
||||||
|
sample << (@io.gets(nil, 1) || "")
|
||||||
end
|
end
|
||||||
|
|
||||||
# read ahead a bit
|
|
||||||
sample << (@io.gets(nil, 1) || "") if sample.end_with?(encode_str("\r"))
|
|
||||||
# try to find a standard separator
|
# try to find a standard separator
|
||||||
if sample =~ encode_re("\r\n?|\n")
|
if sample =~ encode_re("\r\n?|\n")
|
||||||
@row_sep = $&
|
@row_sep = $&
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# tricky seek() clone to work around GzipReader's lack of seek()
|
# tricky seek() clone to work around GzipReader's lack of seek()
|
||||||
@io.rewind
|
@io.rewind
|
||||||
# reset back to the remembered position
|
# reset back to the remembered position
|
||||||
|
@ -2065,8 +2069,18 @@ class CSV
|
||||||
saved_pos -= 1024
|
saved_pos -= 1024
|
||||||
end
|
end
|
||||||
@io.read(saved_pos) if saved_pos.nonzero?
|
@io.read(saved_pos) if saved_pos.nonzero?
|
||||||
rescue IOError # stream not opened for reading
|
rescue IOError # not opened for reading
|
||||||
@row_sep = $INPUT_RECORD_SEPARATOR
|
# 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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue