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

ruby-changes:41839

From: nobu <ko1@a...>
Date: Wed, 24 Feb 2016 17:20:26 +0900 (JST)
Subject: [ruby-changes:41839] nobu:r53913 (trunk): parse without $.

nobu	2016-02-24 17:21:04 +0900 (Wed, 24 Feb 2016)

  New Revision: 53913

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

  Log:
    parse without $.
    
    * io.c (rb_io_gets_internal): read one line from an IO without
      setting ARGF.lineno.
    
    * parse.y (lex_io_gets): use rb_io_gets_internal not to affect
      $. global variable.
    
    * ruby.c (load_file): no longer reset $.

  Modified files:
    trunk/io.c
    trunk/parse.y
    trunk/ruby.c
Index: parse.y
===================================================================
--- parse.y	(revision 53912)
+++ parse.y	(revision 53913)
@@ -5680,10 +5680,12 @@ rb_parser_compile_cstr(VALUE vparser, co https://github.com/ruby/ruby/blob/trunk/parse.y#L5680
     return parser_compile_string(vparser, rb_filesystem_str_new_cstr(f), str, line);
 }
 
+VALUE rb_io_gets_internal(VALUE io);
+
 static VALUE
 lex_io_gets(struct parser_params *parser, VALUE io)
 {
-    return rb_io_gets(io);
+    return rb_io_gets_internal(io);
 }
 
 NODE*
Index: io.c
===================================================================
--- io.c	(revision 53912)
+++ io.c	(revision 53913)
@@ -2975,7 +2975,7 @@ swallow(rb_io_t *fptr, int term) https://github.com/ruby/ruby/blob/trunk/io.c#L2975
 }
 
 static VALUE
-rb_io_getline_fast(rb_io_t *fptr, rb_encoding *enc, VALUE io)
+rb_io_getline_fast(rb_io_t *fptr, rb_encoding *enc)
 {
     VALUE str = Qnil;
     int len = 0;
@@ -3014,13 +3014,6 @@ rb_io_getline_fast(rb_io_t *fptr, rb_enc https://github.com/ruby/ruby/blob/trunk/io.c#L3014
     str = io_enc_str(str, fptr);
     ENC_CODERANGE_SET(str, cr);
     fptr->lineno++;
-    if (io == ARGF.current_file) {
-	ARGF.lineno++;
-	ARGF.last_lineno = ARGF.lineno;
-    }
-    else {
-	ARGF.last_lineno = fptr->lineno;
-    }
 
     return str;
 }
@@ -3072,14 +3065,12 @@ prepare_getline_args(int argc, VALUE *ar https://github.com/ruby/ruby/blob/trunk/io.c#L3065
 }
 
 static VALUE
-rb_io_getline_1(VALUE rs, long limit, VALUE io)
+rb_io_getline_0(VALUE rs, long limit, rb_io_t *fptr)
 {
     VALUE str = Qnil;
-    rb_io_t *fptr;
     int nolimit = 0;
     rb_encoding *enc;
 
-    GetOpenFile(io, fptr);
     rb_io_check_char_readable(fptr);
     if (NIL_P(rs) && limit < 0) {
 	str = read_all(fptr, 0, Qnil);
@@ -3091,7 +3082,7 @@ rb_io_getline_1(VALUE rs, long limit, VA https://github.com/ruby/ruby/blob/trunk/io.c#L3082
     else if (rs == rb_default_rs && limit < 0 && !NEED_READCONV(fptr) &&
              rb_enc_asciicompat(enc = io_read_encoding(fptr))) {
 	NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
-	return rb_io_getline_fast(fptr, enc, io);
+	return rb_io_getline_fast(fptr, enc);
     }
     else {
 	int c, newline = -1;
@@ -3165,12 +3156,28 @@ rb_io_getline_1(VALUE rs, long limit, VA https://github.com/ruby/ruby/blob/trunk/io.c#L3156
 
     if (!NIL_P(str) && !nolimit) {
 	fptr->lineno++;
+    }
+
+    return str;
+}
+
+static VALUE
+rb_io_getline_1(VALUE rs, long limit, VALUE io)
+{
+    rb_io_t *fptr;
+    int old_lineno, new_lineno;
+    VALUE str;
+
+    GetOpenFile(io, fptr);
+    old_lineno = fptr->lineno;
+    str = rb_io_getline_0(rs, limit, fptr);
+    if (!NIL_P(str) && (new_lineno = fptr->lineno) != old_lineno) {
 	if (io == ARGF.current_file) {
-	    ARGF.lineno++;
+	    ARGF.lineno += new_lineno - old_lineno;
 	    ARGF.last_lineno = ARGF.lineno;
 	}
 	else {
-	    ARGF.last_lineno = fptr->lineno;
+	    ARGF.last_lineno = new_lineno;
 	}
     }
 
@@ -3193,6 +3200,14 @@ rb_io_gets(VALUE io) https://github.com/ruby/ruby/blob/trunk/io.c#L3200
     return rb_io_getline_1(rb_default_rs, -1, io);
 }
 
+VALUE
+rb_io_gets_internal(VALUE io)
+{
+    rb_io_t *fptr;
+    GetOpenFile(io, fptr);
+    return rb_io_getline_0(rb_default_rs, -1, fptr);
+}
+
 /*
  *  call-seq:
  *     ios.gets(sep=$/)     -> string or nil
Index: ruby.c
===================================================================
--- ruby.c	(revision 53912)
+++ ruby.c	(revision 53913)
@@ -1675,7 +1675,6 @@ struct load_file_arg { https://github.com/ruby/ruby/blob/trunk/ruby.c#L1675
     int xflag;
     struct cmdline_options *opt;
     VALUE f;
-    VALUE lineno;
 };
 
 static VALUE
@@ -1878,7 +1877,6 @@ restore_load_file(VALUE arg) https://github.com/ruby/ruby/blob/trunk/ruby.c#L1877
 {
     struct load_file_arg *argp = (struct load_file_arg *)arg;
     VALUE f = argp->f;
-    VALUE lineno = argp->lineno;
 
     if (argp->script) {
 	/*
@@ -1898,7 +1896,7 @@ restore_load_file(VALUE arg) https://github.com/ruby/ruby/blob/trunk/ruby.c#L1896
     else if (f != rb_stdin) {
 	rb_io_close(f);
     }
-    return rb_gv_set("$.", lineno);
+    return Qnil;
 }
 
 static NODE *
@@ -1910,7 +1908,6 @@ load_file(VALUE parser, VALUE fname, int https://github.com/ruby/ruby/blob/trunk/ruby.c#L1908
     arg.script = script;
     arg.opt = opt;
     arg.xflag = 0;
-    arg.lineno = rb_gv_get("$.");
     arg.f = open_load_file(rb_str_encode_ospath(fname), &arg.xflag);
     return (NODE *)rb_ensure(load_file_internal, (VALUE)&arg,
 			     restore_load_file, (VALUE)&arg);

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

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