[前][次][番号順一覧][スレッド一覧]

ruby-changes:47523

From: hsbt <ko1@a...>
Date: Tue, 22 Aug 2017 14:32:39 +0900 (JST)
Subject: [ruby-changes:47523] hsbt:r59639 (trunk): Escape skip_lines string before convert to Regexp.

hsbt	2017-08-22 14:32:34 +0900 (Tue, 22 Aug 2017)

  New Revision: 59639

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59639

  Log:
    Escape skip_lines string before convert to Regexp.
    
      It ignored all of lines when given Regexp special characters.
    
      [Feature #9147][ruby-core:58549]

  Modified files:
    trunk/lib/csv.rb
    trunk/test/csv/test_features.rb
Index: lib/csv.rb
===================================================================
--- lib/csv.rb	(revision 59638)
+++ lib/csv.rb	(revision 59639)
@@ -2159,7 +2159,7 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L2159
   # See also CSV.new
   def init_comments(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)
       raise ArgumentError, ":skip_lines has to respond to matches"
     end
Index: test/csv/test_features.rb
===================================================================
--- test/csv/test_features.rb	(revision 59638)
+++ test/csv/test_features.rb	(revision 59639)
@@ -353,6 +353,15 @@ class TestCSV::Features < TestCSV https://github.com/ruby/ruby/blob/trunk/test/csv/test_features.rb#L353
     assert_equal [["line", "1", "a"], ["line", "2", "b"]], c.each.to_a
   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
     sample_data = "line,1,a\n\"#not\",a,line\nline,2,b"
     c = CSV.new sample_data, :skip_lines => /\A\s*#/

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]