summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-12-25 09:04:33 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-12-25 09:04:33 +0000
commit08d99ac9e35ddcd22885303306116cb10c6a992c (patch)
tree3059c36fe97ea5b9784c1e7dbf25df8b64d85f52
parent9ff4e23741448fb6c02090ebc95671d83b2d03a0 (diff)
* lib/csv.rb (CSV.foreach): 'rb' mode is defaulted in open.
* lib/csv.rb (CSV#init_separators): cannonicalize encoding options as Encoding objects. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30360 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog7
-rw-r--r--lib/csv.rb19
-rwxr-xr-xtest/csv/test_encodings.rb17
3 files changed, 36 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index 0881fc7865..65281fd824 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Sat Dec 25 18:04:30 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * lib/csv.rb (CSV.foreach): 'rb' mode is defaulted in open.
+
+ * lib/csv.rb (CSV#init_separators): cannonicalize encoding options
+ as Encoding objects.
+
Sat Dec 25 18:30:34 2010 KOSAKI Motohiro <kosaki.motohiro@gmail.com>
* thread.c (rb_thread_atfork): Add small comment why we need
diff --git a/lib/csv.rb b/lib/csv.rb
index 278abc1eea..45273f9978 100644
--- a/lib/csv.rb
+++ b/lib/csv.rb
@@ -1203,7 +1203,7 @@ class CSV
# but transcode it to UTF-8 before CSV parses it.
#
def self.foreach(path, options = Hash.new, &block)
- open(path, 'rb', options) do |csv|
+ open(path, options) do |csv|
csv.each(&block)
end
end
@@ -1561,13 +1561,18 @@ class CSV
# create the IO object we will read from
@io = data.is_a?(String) ? StringIO.new(data) : data
# honor the IO encoding if we can, otherwise default to ASCII-8BIT
- @encoding = options.delete(:internal_encoding) ||
+ @encoding = raw_encoding(nil) ||
+ (if encoding = options.delete(:internal_encoding)
+ case encoding
+ when Encoding; encoding
+ else Encoding.find(encoding)
+ end
+ end) ||
(case encoding = options.delete(:encoding)
when Encoding; encoding
- when /\A[^:]+/; $1
+ when /\A[^:]+/; Encoding.find($&)
end) ||
- raw_encoding || Encoding.default_internal ||
- Encoding.default_external
+ Encoding.default_internal || Encoding.default_external
#
# prepare for building safe regular expressions in the target encoding,
# if we can transcode the needed characters
@@ -2283,7 +2288,7 @@ class CSV
private
- def raw_encoding
+ def raw_encoding(default = Encoding::ASCII_8BIT)
if @io.respond_to? :internal_encoding
@io.internal_encoding || @io.external_encoding
elsif @io.is_a? StringIO
@@ -2291,7 +2296,7 @@ class CSV
elsif @io.respond_to? :encoding
@io.encoding
else
- Encoding::ASCII_8BIT
+ default
end
end
end
diff --git a/test/csv/test_encodings.rb b/test/csv/test_encodings.rb
index 59f43b1be9..46e992977a 100755
--- a/test/csv/test_encodings.rb
+++ b/test/csv/test_encodings.rb
@@ -260,6 +260,23 @@ class TestCSV::Encodings < TestCSV
end unless encoding == __ENCODING__
rescue Encoding::ConverterNotFoundError
end
+ options[:encoding] = encoding.name
+ CSV.open(@temp_csv_path, options) do |csv|
+ csv.each_with_index do |row, i|
+ assert_equal(fields[i], row)
+ end
+ end
+ options.delete(:encoding)
+ options[:external_encoding] = encoding.name
+ options[:internal_encoding] = __ENCODING__.name
+ begin
+ CSV.open(@temp_csv_path, options) do |csv|
+ csv.each_with_index do |row, i|
+ assert_equal(orig_fields[i], row)
+ end
+ end unless encoding == __ENCODING__
+ rescue Encoding::ConverterNotFoundError
+ end
end
def encode_ary(ary, encoding)