1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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 $.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-02-24 08:21:04 +00:00
parent 43f7eacdac
commit d4819da611
3 changed files with 33 additions and 19 deletions

43
io.c
View file

@ -2975,7 +2975,7 @@ swallow(rb_io_t *fptr, int term)
}
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_encoding *enc, VALUE io)
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 *argv, VALUE *rsp, long *limit, VALUE io)
}
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, VALUE io)
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, VALUE io)
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)
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

View file

@ -5680,10 +5680,12 @@ rb_parser_compile_cstr(VALUE vparser, const char *f, const char *s, int len, int
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*

5
ruby.c
View file

@ -1675,7 +1675,6 @@ struct load_file_arg {
int xflag;
struct cmdline_options *opt;
VALUE f;
VALUE lineno;
};
static VALUE
@ -1878,7 +1877,6 @@ restore_load_file(VALUE arg)
{
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)
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 script, struct cmdline_options *opt)
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);