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

ruby-changes:45051

From: nobu <ko1@a...>
Date: Tue, 20 Dec 2016 17:25:09 +0900 (JST)
Subject: [ruby-changes:45051] nobu:r57124 (trunk): io.c: update argf lineno

nobu	2016-12-20 17:25:02 +0900 (Tue, 20 Dec 2016)

  New Revision: 57124

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

  Log:
    io.c: update argf lineno
    
    * io.c (argf_block_call_line): update line number in non-global
      ARGF instance.  [ruby-core:78728] [Bug #13051]

  Modified files:
    trunk/io.c
    trunk/test/ruby/test_argf.rb
Index: io.c
===================================================================
--- io.c	(revision 57123)
+++ io.c	(revision 57124)
@@ -10925,6 +10925,12 @@ rb_stdio_set_default_encoding(void) https://github.com/ruby/ruby/blob/trunk/io.c#L10925
     rb_io_set_encoding(1, &val, rb_stderr);
 }
 
+static inline int
+global_argf_p(VALUE arg)
+{
+    return arg == argf;
+}
+
 /*
  *  call-seq:
  *     ARGF.external_encoding   -> encoding
@@ -11541,6 +11547,22 @@ argf_block_call(ID mid, int argc, VALUE https://github.com/ruby/ruby/blob/trunk/io.c#L11547
     if (ret != Qundef) ARGF.next_p = 1;
 }
 
+static VALUE
+argf_block_call_line_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, argf))
+{
+    if (!global_argf_p(argf)) {
+	ARGF.last_lineno = ++ARGF.lineno;
+    }
+    return argf_block_call_i(i, argf, argc, argv, blockarg);
+}
+
+static void
+argf_block_call_line(ID mid, int argc, VALUE *argv, VALUE argf)
+{
+    VALUE ret = rb_block_call(ARGF.current_file, mid, argc, argv, argf_block_call_line_i, argf);
+    if (ret != Qundef) ARGF.next_p = 1;
+}
+
 /*
  *  call-seq:
  *     ARGF.each(sep=$/)            {|line| block }  -> ARGF
@@ -11578,7 +11600,7 @@ argf_each_line(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/io.c#L11600
 {
     RETURN_ENUMERATOR(argf, argc, argv);
     FOREACH_ARGF() {
-	argf_block_call(rb_intern("each_line"), argc, argv, argf);
+	argf_block_call_line(rb_intern("each_line"), argc, argv, argf);
     }
     return argf;
 }
Index: test/ruby/test_argf.rb
===================================================================
--- test/ruby/test_argf.rb	(revision 57123)
+++ test/ruby/test_argf.rb	(revision 57124)
@@ -137,6 +137,18 @@ class TestArgf < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_argf.rb#L137
     INPUT
   end
 
+  def test_new_lineno
+    f = ARGF.class.new(@t1.path, @t2.path, @t3.path)
+    result = []
+    f.each {|line| result << [f.lineno, line]; break if result.size == 3}
+    assert_equal(3, f.lineno)
+    assert_equal((1..3).map {|i| [i, "#{i}\n"]}, result)
+
+    f = ARGF.class.new(@t1.path, @t2.path, @t3.path)
+    f.each_char.to_a
+    assert_equal(0, f.lineno)
+  end
+
   def test_inplace
     assert_in_out_err(["-", @t1.path, @t2.path, @t3.path], <<-INPUT, [], [])
       ARGF.inplace_mode = '.bak'

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

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