From 489cab06decee5ebec13be5e81898109a402fed7 Mon Sep 17 00:00:00 2001 From: Aditya <152765789+adityawrk@users.noreply.github.com> Date: Sun, 29 Nov 2026 08:33:34 +0530 Subject: [PATCH 1/2] Test stripping a long padded line in linear time quote_char: nil and strip: " " removed each padding character with delete_prefix!, which copies the rest of the string. A 400_000-character pad took about a second. --- test/csv/parse/test_strip.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/csv/parse/test_strip.rb b/test/csv/parse/test_strip.rb index c5e35209..717278c8 100644 --- a/test/csv/parse/test_strip.rb +++ b/test/csv/parse/test_strip.rb @@ -44,6 +44,38 @@ def test_string strip: " ")) end + def test_string_strip_long_padding + pad = " " * 20 + assert_equal(["value"], + CSV.parse_line("#{pad}value#{pad}", + strip: " ", + quote_char: nil)) + assert_equal(["a b"], + CSV.parse_line("#{pad}a b#{pad}", + strip: " ", + quote_char: nil)) + assert_equal(["a", "b"], + CSV.parse_line("#{pad}a,b#{pad}", + strip: " ", + quote_char: nil)) + ideographic_space = "\u3000" + padding = ideographic_space * 3 + assert_equal(["a#{ideographic_space}b"], + CSV.parse_line("#{padding}a#{ideographic_space}b#{padding}", + strip: ideographic_space, + quote_char: nil)) + + # Time the strip itself. A full parse is dominated by the + # one-byte scanner used when CSV_PARSER_SCANNER_TEST is set. + parser = CSV.new("", strip: " ", quote_char: nil).send(:parser) + value = "#{" " * 400_000}value#{" " * 400_000}" + started = Process.clock_gettime(Process::CLOCK_MONOTONIC) + parser.send(:strip_value, value) + elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started + assert_equal("value", value) + assert_operator(elapsed, :<, 0.25) + end + def test_no_quote assert_equal([" a ", " b "], CSV.parse_line(%Q{" a ", b }, From d9bf395da36fa32c5b7479e6579ff06eeb6474e0 Mon Sep 17 00:00:00 2001 From: Aditya <152765789+adityawrk@users.noreply.github.com> Date: Sun, 29 Nov 2026 08:33:34 +0530 Subject: [PATCH 2/2] Strip a repeated character from both ends in one pass delete_prefix! in a loop copied the remainder for every leading character. A single regexp removes both ends. Fixes #396 --- lib/csv/parser.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/csv/parser.rb b/lib/csv/parser.rb index f14be3af..8b94771c 100644 --- a/lib/csv/parser.rb +++ b/lib/csv/parser.rb @@ -558,6 +558,13 @@ def prepare_strip end @strip = @strip.encode(@encoding) @escaped_strip = Regexp.escape(@strip) + # One match for both ends. A delete_prefix! loop copies the + # remainder on every character (csv#396). + @line_strip = Regexp.new( + "\\A".encode(@encoding) + @escaped_strip + + "+|".encode(@encoding) + @escaped_strip + + "+\\z".encode(@encoding) + ) if @quote_character @strip_value = Regexp.new(@escaped_strip + "+".encode(@encoding)) @@ -1238,12 +1245,7 @@ def strip_value(value) case @strip when String - while value.delete_prefix!(@strip) - # do nothing - end - while value.delete_suffix!(@strip) - # do nothing - end + value.gsub!(@line_strip, "") else value.strip! end