Skip to content

Commit

Permalink
Changed line ending handling to consider the combination \r\n as a si…
Browse files Browse the repository at this point in the history
…ngle entry when row is faulty (#220)
  • Loading branch information
anakinj committed Oct 3, 2021
1 parent b0b1325 commit 29cef9e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/csv/parser.rb
Expand Up @@ -527,7 +527,7 @@ def prepare_separators

@cr = "\r".encode(@encoding)
@lf = "\n".encode(@encoding)
@cr_or_lf = Regexp.new("[\r\n]".encode(@encoding))
@line_end = Regexp.new("\r\n|\n|\r".encode(@encoding))
@not_line_end = Regexp.new("[^\r\n]+".encode(@encoding))
end

Expand Down Expand Up @@ -915,7 +915,7 @@ def parse_quotable_robust(&block)
message = "Any value after quoted field isn't allowed"
raise MalformedCSVError.new(message, @lineno)
elsif @unquoted_column_value and
(new_line = @scanner.scan(@cr_or_lf))
(new_line = @scanner.scan(@line_end))
ignore_broken_line
message = "Unquoted fields do not allow new line " +
"<#{new_line.inspect}>"
Expand All @@ -924,7 +924,7 @@ def parse_quotable_robust(&block)
ignore_broken_line
message = "Illegal quoting"
raise MalformedCSVError.new(message, @lineno)
elsif (new_line = @scanner.scan(@cr_or_lf))
elsif (new_line = @scanner.scan(@line_end))
ignore_broken_line
message = "New line must be <#{@row_separator.inspect}> " +
"not <#{new_line.inspect}>"
Expand Down Expand Up @@ -1090,7 +1090,7 @@ def strip_value(value)

def ignore_broken_line
@scanner.scan_all(@not_line_end)
@scanner.scan_all(@cr_or_lf)
@scanner.scan_all(@line_end)
@lineno += 1
end

Expand Down
13 changes: 13 additions & 0 deletions test/csv/parse/test_invalid.rb
Expand Up @@ -36,4 +36,17 @@ def test_ignore_invalid_line
csv.shift)
assert_equal(true, csv.eof?)
end

def test_ignore_invalid_line_cr_lf
data = <<-CSV
"1","OK"\r
"2",""NOT" OK"\r
"3","OK"\r
CSV
csv = CSV.new(data)

assert_equal(['1', 'OK'], csv.shift)
assert_raise(CSV::MalformedCSVError) { csv.shift }
assert_equal(['3', 'OK'], csv.shift)
end
end

0 comments on commit 29cef9e

Please sign in to comment.