Skip to content

Commit

Permalink
Don't build quoted_fields array when not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
marshall-lee committed Jul 21, 2024
1 parent e75132e commit 5eef0b3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
9 changes: 8 additions & 1 deletion lib/csv/fields_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ class CSV
# Note: Don't use this class directly. This is an internal class.
class FieldsConverter
include Enumerable

NO_QUOTED_FIELDS = [] # :nodoc:
def NO_QUOTED_FIELDS.[](_index)
false
end
NO_QUOTED_FIELDS.freeze

#
# A CSV::FieldsConverter is a data structure for storing the
# fields converter properties to be passed as a parameter
Expand Down Expand Up @@ -44,7 +51,7 @@ def empty?
@converters.empty?
end

def convert(fields, headers, lineno, quoted_fields)
def convert(fields, headers, lineno, quoted_fields=NO_QUOTED_FIELDS)
return fields unless need_convert?

fields.collect.with_index do |field, index|
Expand Down
14 changes: 6 additions & 8 deletions lib/csv/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ def prepare_header
case headers
when Array
@raw_headers = headers
quoted_fields = [false] * @raw_headers.size
quoted_fields = FieldsConverter::NO_QUOTED_FIELDS
@use_headers = true
when String
@raw_headers, quoted_fields = parse_headers(headers)
Expand Down Expand Up @@ -941,11 +941,9 @@ def parse_no_quote(&block)
if line.empty?
next if @skip_blanks
row = []
quoted_fields = []
else
line = strip_value(line)
row = line.split(@split_column_separator, -1)
quoted_fields = [false] * row.size
if @max_field_size
row.each do |column|
validate_field_size(column)
Expand All @@ -959,7 +957,7 @@ def parse_no_quote(&block)
end
end
@last_line = original_line
emit_row(row, quoted_fields, &block)
emit_row(row, &block)
end
end

Expand All @@ -981,7 +979,7 @@ def parse_quotable_loose(&block)
next
end
row = []
quoted_fields = []
quoted_fields = FieldsConverter::NO_QUOTED_FIELDS
elsif line.include?(@cr) or line.include?(@lf)
@scanner.keep_back
@parse_method = :parse_quotable_robust
Expand Down Expand Up @@ -1043,13 +1041,13 @@ def parse_quotable_robust(&block)
quoted_fields << @quoted_column_value
elsif parse_row_end
if row.empty? and value.nil?
emit_row([], [], &block) unless @skip_blanks
emit_row(row, &block) unless @skip_blanks
else
row << value
quoted_fields << @quoted_column_value
emit_row(row, quoted_fields, &block)
row = []
quoted_fields = []
quoted_fields.clear
end
skip_needless_lines
start_row
Expand Down Expand Up @@ -1254,7 +1252,7 @@ def start_row
@scanner.keep_start
end

def emit_row(row, quoted_fields, &block)
def emit_row(row, quoted_fields=FieldsConverter::NO_QUOTED_FIELDS, &block)
@lineno += 1

raw_row = row
Expand Down
3 changes: 1 addition & 2 deletions lib/csv/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def <<(row)
@lineno += 1

if @fields_converter
quoted_fields = [false] * row.size
row = @fields_converter.convert(row, nil, lineno, quoted_fields)
row = @fields_converter.convert(row, nil, lineno)
end

i = -1
Expand Down

0 comments on commit 5eef0b3

Please sign in to comment.