From b5e05edff2e7480872237df1f0aae356318e8527 Mon Sep 17 00:00:00 2001 From: Kristina Spurgin Date: Mon, 23 Dec 2024 14:45:41 -0500 Subject: [PATCH] Avoid deprecation warnings by not modifying $/ global --- lib/csvlint/validate.rb | 46 ++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/lib/csvlint/validate.rb b/lib/csvlint/validate.rb index 11b8060..a997fda 100644 --- a/lib/csvlint/validate.rb +++ b/lib/csvlint/validate.rb @@ -67,7 +67,6 @@ def initialize(source, dialect = {}, schema = nil, options = {}) @formats = [] @schema = schema @dialect = dialect - @orig_sep = $/ @csv_header = true @headers = {} @lambda = options[:lambda] @@ -108,17 +107,7 @@ def validate def validate_stream @current_line = 1 - # StringIO.each_line splits on the value of $/ (input record separator), - # which defaults to \n. This is why CSVs with \r EOL character don't get - # lines counted properly? - set_sep(@source) - - @source.each_line do |line| - break if line_limit_reached? - parse_line(line) - end - - reset_sep + parse_lines(@source) validate_line(@leading, @current_line) unless @leading == "" end @@ -139,17 +128,30 @@ def validate_url request.on_body do |chunk| chunk.force_encoding(Encoding::UTF_8) if chunk.encoding == Encoding::ASCII_8BIT io = StringIO.new(chunk) - set_sep(io) + parse_lines(io) + end + request.run + # Validate the last line too + validate_line(@leading, @current_line) unless @leading == "" + end - io.each_line do |line| + # #each_line splits on the value of $/ (input record separator), + # which defaults to \n. This is why CSVs with \r EOL character don't get + # lines counted properly? + def parse_lines(source) + sep = determine_sep(source) + + if sep == "\n" + source.each_line do |line| + break if line_limit_reached? + parse_line(line) + end + else + source.each_line(sep) do |line| break if line_limit_reached? parse_line(line) end - reset_sep end - request.run - # Validate the last line too - validate_line(@leading, @current_line) unless @leading == "" end def parse_line(line) @@ -529,10 +531,6 @@ def locate_schema private - def set_sep(source) - $/ = determine_sep(source) - end - def determine_sep(source) return explicitly_set_sep if explicitly_set_sep @@ -562,10 +560,6 @@ def explicitly_set_sep sep end - def reset_sep - $/ = @orig_sep - end - def parse_extension(source) case source when File