e$B$J$+$@$G$9!#e(B
At Tue, 28 Oct 2008 01:10:42 +0900,
Yukihiro M. wrote in [ruby-dev:36959]:
e$B$H$J$k$H!"!V%9%l%C%I%m!<%+%k$K$9$k!W$H$$$&$N$O8_49@-LdBj$,H/e(B
e$B@8$7$K$/$$>e$K!“LdBj$b@8$8$K$/$$BE6(E@$+$b$7$l$^$;$s!#$,!”:#e(B
e$B$N$^$^$G$bJL$K:$$C$F$J$$$N$b;v<B$J$N$G!"e(Brb_loade$B$N=$@5$@$1$Ge(B
e$B==J,$H8@$&$N$b$b$C$H$b$J5$$,$7$^$9!#e(B
e$BB>$N%a%=%C%I$H$N43>D$rKI$2$k$H$$$&E@$G$Oe(Bsvare$B$K$9$k$H$$$&$N$b$$$$e(B
e$B$N$G$O$J$$$+$H;W$$$^$9$,!"%9%l%C%I%m!<%+%k$K$9$k%Q%C%A$G$9!#e(B
e$B$D$$$G$K!"e(Brewinde$B$N$H$-$Ne(B$.e$B$NF0:n$O>/$7$*$+$7$$$h$&$J5$$b$7$^$9!#e(B
Index: io.c
— io.c (revision 19983)
+++ io.c (working copy)
@@ -122,5 +122,5 @@ VALUE rb_default_rs;
static VALUE argf;
-static ID id_write, id_read, id_getc, id_flush, id_readpartial;
+static ID id_write, id_read, id_getc, id_flush, id_readpartial,
id_lineno;
static VALUE sym_mode, sym_perm, sym_extenc, sym_intenc, sym_encoding,
sym_open_args;
static VALUE sym_textmode, sym_binmode;
@@ -132,5 +132,4 @@ struct argf {
int gets_lineno;
int init_p, next_p;
- VALUE lineno;
VALUE argv;
char *inplace;
@@ -198,4 +197,18 @@ static int max_file_descriptor = NOFILE;
#define rb_sys_fail_path(path) rb_sys_fail(NIL_P(path) ? 0 :
RSTRING_PTR(path))
+static VALUE
+get_last_lineno(void)
+{
- VALUE n = rb_thread_local_aref(rb_thread_current(), id_lineno);
- if (NIL_P(n)) return INT2FIX(0);
- return n;
+}
-
+static VALUE
+set_last_lineno(int num)
+{
- return rb_thread_local_aset(rb_thread_current(), id_lineno,
INT2FIX(num));
+}
-
#if defined(_WIN32)
#define is_socket(fd, path) rb_w32_is_socket(fd)
@@ -1079,7 +1092,4 @@ rb_io_rewind(VALUE io)
GetOpenFile(io, fptr);
if (io_seek(fptr, 0L, 0) < 0) rb_sys_fail_path(fptr->pathv);
- if (io == ARGF.current_file) {
- ARGF.gets_lineno -= fptr->lineno;
- }
fptr->lineno = 0;
@@ -2085,5 +2095,5 @@ rb_io_getline_fast(rb_io_t *fptr, rb_enc
ENC_CODERANGE_SET(str, cr);
fptr->lineno++;
- ARGF.lineno = INT2FIX(fptr->lineno);
- set_last_lineno(fptr->lineno);
return str;
}
@@ -2220,5 +2230,5 @@ rb_io_getline_1(VALUE rs, long limit, VA
if (!nolimit) {
fptr->lineno++;
- rb_gc_mark(p->lineno);
rb_gc_mark(p->argv);
rb_gc_mark(p->encs.ecopts);
@@ -5822,5 +5831,5 @@ argf_init(struct argf *p, VALUE v)
p->filename = Qnil;
p->current_file = Qnil;
- p->lineno = INT2FIX(0);
- p->gets_lineno = 0;
p->argv = v;
}
@@ -5863,6 +5872,7 @@ static VALUE
argf_set_lineno(VALUE argf, VALUE val)
{
- ARGF.gets_lineno = NUM2INT(val);
- ARGF.lineno = INT2FIX(ARGF.gets_lineno);
- int n = NUM2INT(val);
- ARGF.gets_lineno = n;
- set_last_lineno(n);
return Qnil;
}
@@ -5871,5 +5881,5 @@ static VALUE
argf_lineno(VALUE argf)
{
- return INT2FIX(ARGF.gets_lineno);
}
@@ -6050,5 +6060,5 @@ argf_getline(int argc, VALUE *argv, VALU
if (!NIL_P(line)) {
ARGF.gets_lineno++;
- ARGF.lineno = INT2FIX(ARGF.gets_lineno);
- set_last_lineno(ARGF.gets_lineno);
}
return line;
@@ -6058,6 +6068,5 @@ static VALUE
argf_lineno_getter(ID id, VALUE *var)
{
- VALUE argf = *var;
- return ARGF.lineno;
- return get_last_lineno();
}
@@ -6068,5 +6077,5 @@ argf_lineno_setter(VALUE val, ID id, VAL
int n = NUM2INT(val);
ARGF.gets_lineno = n;
- ARGF.lineno = INT2FIX(n);
@@ -6145,5 +6154,5 @@ rb_gets(void)
if (!NIL_P(line)) {
ARGF.gets_lineno++;
- ARGF.lineno = INT2FIX(ARGF.gets_lineno);
- set_last_lineno(ARGF.gets_lineno);
}
@@ -7756,9 +7765,19 @@ static VALUE
argf_rewind(VALUE argf)
{
- rb_io_t *fptr;
- VALUE io, ret;
- int lineno = 0;
- if (!next_argv()) {
rb_raise(rb_eArgError, “no stream to rewind”);
}
ARGF_FORWARD(0, 0);
- return rb_io_rewind(ARGF.current_file);
- io = ARGF.current_file;
- GetOpenFile(io, fptr);
- lineno = fptr->lineno;
- ret = rb_io_rewind(io);
- ARGF.gets_lineno -= lineno;
- set_last_lineno(ARGF.gets_lineno);
- return ret;
}
@@ -8253,4 +8272,5 @@ Init_IO(void)
id_flush = rb_intern(“flush”);
id_readpartial = rb_intern(“readpartial”);
-
id_lineno = rb_intern("$.");
rb_define_global_function(“syscall”, rb_f_syscall, -1);
Index: test/ruby/test_argf.rb
===================================================================
— test/ruby/test_argf.rb (revision 19983)
+++ test/ruby/test_argf.rb (working copy)
@@ -63,13 +63,13 @@ class TestArgf < Test::Unit::TestCase
a.rewind
b.rewind
-
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["1", 1,
“1”, 3]
-
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["2", 2,
“2”, 4]
-
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["3", 3,
“3”, 5]
-
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["4", 4,
“4”, 6]
-
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5,
“5”, 7]
-
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["1", 1,
“1”, 1]
-
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["2", 2,
“2”, 2]
-
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["3", 3,
“3”, 3]
-
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["4", 4,
“4”, 4]
-
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5,
“5”, 5]
a.rewind
b.rewind
-
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5,
“5”, 8]
-
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["6", 6,
“6”, 9]
-
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5,
“5”, 5]
-
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["6", 6,
“6”, 6]
SRC
expected = src.scan(/#=> (.+)/).flatten
@@ -91,8 +91,8 @@ class TestArgf < Test::Unit::TestCase
a.gets; p $. #=> 2
a.gets; p $. #=> 3
@@ -114,12 +114,12 @@ class TestArgf < Test::Unit::TestCase
a.gets; p $. #=> 1
a.gets; p $. #=> 2