summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSutou Kouhei <kou@clear-code.com>2021-12-24 10:18:18 +0900
committerSutou Kouhei <kou@cozmixng.org>2021-12-24 14:35:33 +0900
commit4a5d372ca8902a649928eb0689aca7edcfaa07b6 (patch)
treecd315c5b43a2a3741b36add194aafb0bcc531f4c /lib
parent56a5ae9f52920982a2f1571a57090772c94c8243 (diff)
[ruby/csv] parser: fix a keep bug that some texts may be dropped unexpectedly
Ruby: [Bug #18245] [ruby-core:105587] Reported by Hassan Abdul Rehman. https://github.com/ruby/csv/commit/5c6523da0a
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/5336
Diffstat (limited to 'lib')
-rw-r--r--lib/csv/parser.rb85
1 files changed, 51 insertions, 34 deletions
diff --git a/lib/csv/parser.rb b/lib/csv/parser.rb
index ef33a69478..e1fe559a41 100644
--- a/lib/csv/parser.rb
+++ b/lib/csv/parser.rb
@@ -166,6 +166,7 @@ class CSV
end
def keep_start
+ adjust_last_keep
@keeps.push([@scanner.pos, nil])
end
@@ -196,7 +197,17 @@ class CSV
end
def keep_drop
- @keeps.pop
+ _, buffer = @keeps.pop
+ return unless buffer
+
+ last_keep = @keeps.last
+ return unless last_keep
+
+ if last_keep[1]
+ last_keep[1] << buffer
+ else
+ last_keep[1] = buffer
+ end
end
def rest
@@ -204,24 +215,30 @@ class CSV
end
private
+ def adjust_last_keep
+ keep = @keeps.last
+ return if keep.nil?
+
+ keep_start = keep[0]
+ return if @scanner.pos == keep_start
+
+ string = @scanner.string
+ keep_data = string.byteslice(keep_start, @scanner.pos - keep_start)
+ if keep_data
+ keep_buffer = keep[1]
+ if keep_buffer
+ keep_buffer << keep_data
+ else
+ keep[1] = keep_data.dup
+ end
+ end
+ keep[0] = 0
+ end
+
def read_chunk
return false if @last_scanner
- unless @keeps.empty?
- keep = @keeps.last
- keep_start = keep[0]
- string = @scanner.string
- keep_data = string.byteslice(keep_start, @scanner.pos - keep_start)
- if keep_data
- keep_buffer = keep[1]
- if keep_buffer
- keep_buffer << keep_data
- else
- keep[1] = keep_data.dup
- end
- end
- keep[0] = 0
- end
+ adjust_last_keep
input = @inputs.first
case input
@@ -728,28 +745,26 @@ class CSV
sample[0, 128].index(@quote_character)
end
- SCANNER_TEST = (ENV["CSV_PARSER_SCANNER_TEST"] == "yes")
- if SCANNER_TEST
- class UnoptimizedStringIO
- def initialize(string)
- @io = StringIO.new(string, "rb:#{string.encoding}")
- end
+ class UnoptimizedStringIO # :nodoc:
+ def initialize(string)
+ @io = StringIO.new(string, "rb:#{string.encoding}")
+ end
- def gets(*args)
- @io.gets(*args)
- end
+ def gets(*args)
+ @io.gets(*args)
+ end
- def each_line(*args, &block)
- @io.each_line(*args, &block)
- end
+ def each_line(*args, &block)
+ @io.each_line(*args, &block)
+ end
- def eof?
- @io.eof?
- end
+ def eof?
+ @io.eof?
end
+ end
- SCANNER_TEST_CHUNK_SIZE =
- Integer((ENV["CSV_PARSER_SCANNER_TEST_CHUNK_SIZE"] || "1"), 10)
+ SCANNER_TEST = (ENV["CSV_PARSER_SCANNER_TEST"] == "yes")
+ if SCANNER_TEST
def build_scanner
inputs = @samples.collect do |sample|
UnoptimizedStringIO.new(sample)
@@ -759,9 +774,11 @@ class CSV
else
inputs << @input
end
+ chunk_size =
+ Integer((ENV["CSV_PARSER_SCANNER_TEST_CHUNK_SIZE"] || "1"), 10)
InputsScanner.new(inputs,
@encoding,
- chunk_size: SCANNER_TEST_CHUNK_SIZE)
+ chunk_size: chunk_size)
end
else
def build_scanner