Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid deprecation warnings by not modifying $/ global #7

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading