[ruby/csv] Fix a parse bug when split character exists in middle of column value
GitHub: fix #115 Reported by TOMITA Masahiro. Thanks!!! https://github.com/ruby/csv/commit/398b3564c5
This commit is contained in:
parent
aeac7db823
commit
814bfc8adc
Notes:
git
2020-07-20 03:35:46 +09:00
@ -446,6 +446,7 @@ class CSV
|
|||||||
@strip = @options[:strip]
|
@strip = @options[:strip]
|
||||||
@escaped_strip = nil
|
@escaped_strip = nil
|
||||||
@strip_value = nil
|
@strip_value = nil
|
||||||
|
@rstrip_value = nil
|
||||||
if @strip.is_a?(String)
|
if @strip.is_a?(String)
|
||||||
case @strip.length
|
case @strip.length
|
||||||
when 0
|
when 0
|
||||||
@ -460,6 +461,8 @@ class CSV
|
|||||||
if @quote_character
|
if @quote_character
|
||||||
@strip_value = Regexp.new(@escaped_strip +
|
@strip_value = Regexp.new(@escaped_strip +
|
||||||
"+".encode(@encoding))
|
"+".encode(@encoding))
|
||||||
|
@rstrip_value = Regexp.new(@escaped_strip +
|
||||||
|
"+\\z".encode(@encoding))
|
||||||
end
|
end
|
||||||
@need_robust_parsing = true
|
@need_robust_parsing = true
|
||||||
elsif @strip
|
elsif @strip
|
||||||
@ -467,6 +470,7 @@ class CSV
|
|||||||
@escaped_strip = strip_values.encode(@encoding)
|
@escaped_strip = strip_values.encode(@encoding)
|
||||||
if @quote_character
|
if @quote_character
|
||||||
@strip_value = Regexp.new("[#{strip_values}]+".encode(@encoding))
|
@strip_value = Regexp.new("[#{strip_values}]+".encode(@encoding))
|
||||||
|
@rstrip_value = Regexp.new("[#{strip_values}]+\\z".encode(@encoding))
|
||||||
end
|
end
|
||||||
@need_robust_parsing = true
|
@need_robust_parsing = true
|
||||||
end
|
end
|
||||||
@ -561,9 +565,6 @@ class CSV
|
|||||||
unless @liberal_parsing
|
unless @liberal_parsing
|
||||||
no_unquoted_values << @escaped_quote_character
|
no_unquoted_values << @escaped_quote_character
|
||||||
end
|
end
|
||||||
if @escaped_strip
|
|
||||||
no_unquoted_values << @escaped_strip
|
|
||||||
end
|
|
||||||
@unquoted_value = Regexp.new("[^".encode(@encoding) +
|
@unquoted_value = Regexp.new("[^".encode(@encoding) +
|
||||||
no_unquoted_values +
|
no_unquoted_values +
|
||||||
"]+".encode(@encoding))
|
"]+".encode(@encoding))
|
||||||
@ -939,6 +940,7 @@ class CSV
|
|||||||
if @liberal_parsing
|
if @liberal_parsing
|
||||||
quoted_value = parse_quoted_column_value
|
quoted_value = parse_quoted_column_value
|
||||||
if quoted_value
|
if quoted_value
|
||||||
|
@scanner.scan_all(@strip_value) if @strip_value
|
||||||
unquoted_value = parse_unquoted_column_value
|
unquoted_value = parse_unquoted_column_value
|
||||||
if unquoted_value
|
if unquoted_value
|
||||||
if @double_quote_outside_quote
|
if @double_quote_outside_quote
|
||||||
@ -986,6 +988,9 @@ class CSV
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
value.gsub!(@backslash_quote_character, @quote_character) if @backslash_quote
|
value.gsub!(@backslash_quote_character, @quote_character) if @backslash_quote
|
||||||
|
if @rstrip_value
|
||||||
|
value.gsub!(@rstrip_value, "")
|
||||||
|
end
|
||||||
value
|
value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -21,6 +21,11 @@ class TestCSVParseStrip < Test::Unit::TestCase
|
|||||||
CSV.parse_line(%Q{a ,b }, strip: true))
|
CSV.parse_line(%Q{a ,b }, strip: true))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_middle
|
||||||
|
assert_equal(["a b"],
|
||||||
|
CSV.parse_line(%Q{a b}, strip: true))
|
||||||
|
end
|
||||||
|
|
||||||
def test_quoted
|
def test_quoted
|
||||||
assert_equal([" a ", " b "],
|
assert_equal([" a ", " b "],
|
||||||
CSV.parse_line(%Q{" a "," b "}, strip: true))
|
CSV.parse_line(%Q{" a "," b "}, strip: true))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user