From 933b092c00a3a7a27f886acd7a4ca8acdf005595 Mon Sep 17 00:00:00 2001 From: Kristina Spurgin Date: Fri, 29 Sep 2023 20:56:28 -0400 Subject: [PATCH] Make row-specific options approach work --- lib/csvlint/validate.rb | 45 ++++++++++++----------------------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/lib/csvlint/validate.rb b/lib/csvlint/validate.rb index 50b0f68..da65bb8 100644 --- a/lib/csvlint/validate.rb +++ b/lib/csvlint/validate.rb @@ -184,27 +184,9 @@ def parse_contents(stream, line = nil) @csv_options[:encoding] = @encoding - # From CSV.parse_line(stream, **@csv_options), called via inheritance by - # LineCSV.parse_line(stream, **lineopts) - # - # line = "Short,Multi,Bool\r\n" - # options = {:col_sep=>",", :row_sep=>:auto, :quote_char=>"\"", - # :skip_blanks=>false, :encoding=>"UTF-8"} - # new(line, **options) = # - # - # line = "a,\"b\nc\",0\r\n" - # options = {:col_sep=>",", :row_sep=>:auto, :quote_char=>"\"", - # :skip_blanks=>false, :encoding=>"UTF-8"} - # new(line, **options) = # - # - # So, what is setting row_sep incorrectly to "\n" when that character - # appears in quotes, but not at the end of the line? begin - # lineopts = csv_options_for_line(stream) - # row = LineCSV.parse_line(stream, **lineopts) - row = LineCSV.parse_line(stream, **@csv_options) + lineopts = csv_options_for_line(stream) + row = LineCSV.parse_line(stream, **lineopts) rescue LineCSV::MalformedCSVError => e build_exception_messages(e, stream, current_line) unless e.message.include?("UTF") && @reported_invalid_encoding end @@ -234,19 +216,18 @@ def parse_contents(stream, line = nil) @data << row end - # def csv_options_for_line(line) - # # If a specific row_sep has been explicitly specified, don't mess with it - # return @csv_options unless @csv_options[:row_sep] == :auto + def csv_options_for_line(line) + # If a specific row_sep has been explicitly specified, don't mess with it + return @csv_options unless @csv_options[:row_sep] == :auto - # # CSV.parse_line does not seem to know how to auto-determine row_sep - # # from the single line it is given, and throws a MalformedCSVError if - # # \r\n or \r is present at the end of the line. - # if line.end_with?("\r\n") - # @csv_options.merge({row_sep: "\r\n"}) - # else - # @csv_options.merge({row_sep: line[-1, 1]}) - # end - # end + if line.end_with?("\r\n") + @csv_options.merge({row_sep: "\r\n"}) + elsif ["\r", "\n"].include?(line[-1, 1]) + @csv_options.merge({row_sep: line[-1, 1]}) + else + @csv_options + end + end def finish sum = @col_counts.inject(:+)