From 183635ab5614acfa0acc2d93dc1f460d3e443050 Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Mon, 26 Jun 2023 14:29:26 +0900 Subject: [PATCH] Fix a bug that the same line is used multiple times GitHub: fix GH-279 It's happen when: * `keep_start`/`keep_{drop,back}` are nested. (e.g.: `strip: true, skip_lines: /.../`) * Row separator is `\r\n`. * `InputScanner` is used. (Small input doesn't use `InputScanner`) Reported by Gabriel Nagy. Thanks!!! --- lib/csv/parser.rb | 3 ++- test/csv/parse/test_skip_lines.rb | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/csv/parser.rb b/lib/csv/parser.rb index 3b051320..ed9297fe 100644 --- a/lib/csv/parser.rb +++ b/lib/csv/parser.rb @@ -201,7 +201,8 @@ def keep_back # trace(__method__, :rescan, start, buffer) string = @scanner.string if scanner == @scanner - keep = string.byteslice(start, string.bytesize - start) + keep = string.byteslice(start, + string.bytesize - @scanner.pos - start) else keep = string end diff --git a/test/csv/parse/test_skip_lines.rb b/test/csv/parse/test_skip_lines.rb index d231cc65..2fea02eb 100644 --- a/test/csv/parse/test_skip_lines.rb +++ b/test/csv/parse/test_skip_lines.rb @@ -115,4 +115,12 @@ def test_crlf CSV.parse("a,b\r\n,\r\n", :skip_lines => /^,+$/)) end + + def test_crlf_strip_no_last_crlf + assert_equal([["a"], ["b"]], + CSV.parse("a\r\nb", + row_sep: "\r\n", + skip_lines: /^ *$/, + strip: true)) + end end