From 198281a71d95c1a48445dc6e913bc1bd62350054 Mon Sep 17 00:00:00 2001 From: Kouhei Sutou Date: Tue, 23 Apr 2019 05:54:44 +0900 Subject: [PATCH] [ruby/csv] Fix a bug that strip: true removes newlines https://github.com/ruby/csv/commit/5540d35a30 --- lib/csv/parser.rb | 2 +- test/csv/parse/test_strip.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/csv/parser.rb b/lib/csv/parser.rb index 85252203e4..2ef2a28ff3 100644 --- a/lib/csv/parser.rb +++ b/lib/csv/parser.rb @@ -429,7 +429,7 @@ class CSV end @need_robust_parsing = true elsif @strip - strip_values = " \t\r\n\f\v" + strip_values = " \t\f\v" @escaped_strip = strip_values.encode(@encoding) if @quote_character @strip_value = Regexp.new("[#{strip_values}]+".encode(@encoding)) diff --git a/test/csv/parse/test_strip.rb b/test/csv/parse/test_strip.rb index 160407bd94..0255bb9a30 100644 --- a/test/csv/parse/test_strip.rb +++ b/test/csv/parse/test_strip.rb @@ -45,4 +45,34 @@ class TestCSVParseStrip < Test::Unit::TestCase strip: %Q{"}, quote_char: nil)) end + + def test_do_not_strip_cr + assert_equal([ + ["a", "b "], + ["a", "b "], + ], + CSV.parse(%Q{"a" ,"b " \r} + + %Q{"a" ,"b " \r}, + strip: true)) + end + + def test_do_not_strip_lf + assert_equal([ + ["a", "b "], + ["a", "b "], + ], + CSV.parse(%Q{"a" ,"b " \n} + + %Q{"a" ,"b " \n}, + strip: true)) + end + + def test_do_not_strip_crlf + assert_equal([ + ["a", "b "], + ["a", "b "], + ], + CSV.parse(%Q{"a" ,"b " \r\n} + + %Q{"a" ,"b " \r\n}, + strip: true)) + end end