Skip to content

Commit

Permalink
Avoid deprecation warnings by not modifying $/ global
Browse files Browse the repository at this point in the history
  • Loading branch information
kspurgin committed Dec 23, 2024
1 parent 26d071e commit b5e05ed
Showing 1 changed file with 20 additions and 26 deletions.
46 changes: 20 additions & 26 deletions lib/csvlint/validate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -562,10 +560,6 @@ def explicitly_set_sep
sep
end

def reset_sep
$/ = @orig_sep
end

def parse_extension(source)
case source
when File
Expand Down

0 comments on commit b5e05ed

Please sign in to comment.