mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Escape skip_lines string before convert to Regexp.
It ignored all of lines when given Regexp special characters. [Feature #9147][ruby-core:58549] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59639 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
84bc175cab
commit
2c69f7b278
2 changed files with 10 additions and 1 deletions
|
@ -2159,7 +2159,7 @@ class CSV
|
||||||
# See also CSV.new
|
# See also CSV.new
|
||||||
def init_comments(skip_lines)
|
def init_comments(skip_lines)
|
||||||
@skip_lines = skip_lines
|
@skip_lines = skip_lines
|
||||||
@skip_lines = Regexp.new(@skip_lines) if @skip_lines.is_a? String
|
@skip_lines = Regexp.new(Regexp.escape(@skip_lines)) if @skip_lines.is_a? String
|
||||||
if @skip_lines and not @skip_lines.respond_to?(:match)
|
if @skip_lines and not @skip_lines.respond_to?(:match)
|
||||||
raise ArgumentError, ":skip_lines has to respond to matches"
|
raise ArgumentError, ":skip_lines has to respond to matches"
|
||||||
end
|
end
|
||||||
|
|
|
@ -353,6 +353,15 @@ class TestCSV::Features < TestCSV
|
||||||
assert_equal [["line", "1", "a"], ["line", "2", "b"]], c.each.to_a
|
assert_equal [["line", "1", "a"], ["line", "2", "b"]], c.each.to_a
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_comment_rows_are_ignored_with_heredoc
|
||||||
|
c = csv = CSV.new(<<~EOL, skip_lines: ".")
|
||||||
|
1,foo
|
||||||
|
.2,bar
|
||||||
|
3,baz
|
||||||
|
EOL
|
||||||
|
assert_equal [["1", "foo"], ["3", "baz"]], c.each.to_a
|
||||||
|
end
|
||||||
|
|
||||||
def test_quoted_skip_line_markers_are_ignored
|
def test_quoted_skip_line_markers_are_ignored
|
||||||
sample_data = "line,1,a\n\"#not\",a,line\nline,2,b"
|
sample_data = "line,1,a\n\"#not\",a,line\nline,2,b"
|
||||||
c = CSV.new sample_data, :skip_lines => /\A\s*#/
|
c = CSV.new sample_data, :skip_lines => /\A\s*#/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue