Skip to content

Commit 398b356

Browse files
committed
Fix a parse bug when split character exists in middle of column value
GitHub: fix #115 Reported by TOMITA Masahiro. Thanks!!!
1 parent fb10925 commit 398b356

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/csv/parser.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ def prepare_strip
446446
@strip = @options[:strip]
447447
@escaped_strip = nil
448448
@strip_value = nil
449+
@rstrip_value = nil
449450
if @strip.is_a?(String)
450451
case @strip.length
451452
when 0
@@ -460,13 +461,16 @@ def prepare_strip
460461
if @quote_character
461462
@strip_value = Regexp.new(@escaped_strip +
462463
"+".encode(@encoding))
464+
@rstrip_value = Regexp.new(@escaped_strip +
465+
"+\\z".encode(@encoding))
463466
end
464467
@need_robust_parsing = true
465468
elsif @strip
466469
strip_values = " \t\f\v"
467470
@escaped_strip = strip_values.encode(@encoding)
468471
if @quote_character
469472
@strip_value = Regexp.new("[#{strip_values}]+".encode(@encoding))
473+
@rstrip_value = Regexp.new("[#{strip_values}]+\\z".encode(@encoding))
470474
end
471475
@need_robust_parsing = true
472476
end
@@ -561,9 +565,6 @@ def prepare_unquoted
561565
unless @liberal_parsing
562566
no_unquoted_values << @escaped_quote_character
563567
end
564-
if @escaped_strip
565-
no_unquoted_values << @escaped_strip
566-
end
567568
@unquoted_value = Regexp.new("[^".encode(@encoding) +
568569
no_unquoted_values +
569570
"]+".encode(@encoding))
@@ -939,6 +940,7 @@ def parse_column_value
939940
if @liberal_parsing
940941
quoted_value = parse_quoted_column_value
941942
if quoted_value
943+
@scanner.scan_all(@strip_value) if @strip_value
942944
unquoted_value = parse_unquoted_column_value
943945
if unquoted_value
944946
if @double_quote_outside_quote
@@ -986,6 +988,9 @@ def parse_unquoted_column_value
986988
end
987989
end
988990
value.gsub!(@backslash_quote_character, @quote_character) if @backslash_quote
991+
if @rstrip_value
992+
value.gsub!(@rstrip_value, "")
993+
end
989994
value
990995
end
991996

test/csv/parse/test_strip.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ def test_right
2121
CSV.parse_line(%Q{a ,b }, strip: true))
2222
end
2323

24+
def test_middle
25+
assert_equal(["a b"],
26+
CSV.parse_line(%Q{a b}, strip: true))
27+
end
28+
2429
def test_quoted
2530
assert_equal([" a ", " b "],
2631
CSV.parse_line(%Q{" a "," b "}, strip: true))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy