* io.c (rb_io_getline_1): enables limit even if rs is given.

[ruby-core:22434]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22610 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2009-02-25 03:06:12 +00:00
parent 9594c893f7
commit ac9d09ba3c
3 changed files with 24 additions and 16 deletions

View file

@ -1,3 +1,8 @@
Wed Feb 25 12:06:09 2009 <nobu@ruby-lang.org>
* io.c (rb_io_getline_1): enables limit even if rs is given.
[ruby-core:22434]
Wed Feb 25 02:28:51 2009 Nobuyoshi Nakada <nobu@ruby-lang.org> Wed Feb 25 02:28:51 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* string.c (rb_str_chomp_bang): coderange may change. * string.c (rb_str_chomp_bang): coderange may change.

32
io.c
View file

@ -2246,7 +2246,7 @@ rb_io_getline_1(VALUE rs, long limit, VALUE io)
GetOpenFile(io, fptr); GetOpenFile(io, fptr);
rb_io_check_readable(fptr); rb_io_check_readable(fptr);
if (NIL_P(rs)) { if (NIL_P(rs) && limit < 0) {
str = read_all(fptr, 0, Qnil); str = read_all(fptr, 0, Qnil);
if (RSTRING_LEN(str) == 0) return Qnil; if (RSTRING_LEN(str) == 0) return Qnil;
} }
@ -2258,24 +2258,26 @@ rb_io_getline_1(VALUE rs, long limit, VALUE io)
return rb_io_getline_fast(fptr, enc); return rb_io_getline_fast(fptr, enc);
} }
else { else {
int c, newline; int c, newline = -1;
const char *rsptr; const char *rsptr = 0;
long rslen; long rslen = 0;
int rspara = 0; int rspara = 0;
int extra_limit = 16; int extra_limit = 16;
rslen = RSTRING_LEN(rs); if (!NIL_P(rs)) {
if (rslen == 0) { rslen = RSTRING_LEN(rs);
rsptr = "\n\n"; if (rslen == 0) {
rslen = 2; rsptr = "\n\n";
rspara = 1; rslen = 2;
swallow(fptr, '\n'); rspara = 1;
rs = 0; swallow(fptr, '\n');
rs = 0;
}
else {
rsptr = RSTRING_PTR(rs);
}
newline = (unsigned char)rsptr[rslen - 1];
} }
else {
rsptr = RSTRING_PTR(rs);
}
newline = (unsigned char)rsptr[rslen - 1];
/* MS - Optimisation */ /* MS - Optimisation */
enc = io_read_encoding(fptr); enc = io_read_encoding(fptr);

View file

@ -102,8 +102,9 @@ class TestIO < Test::Unit::TestCase
def test_gets_limit_extra_arg def test_gets_limit_extra_arg
with_pipe {|r, w| with_pipe {|r, w|
r, w = IO.pipe r, w = IO.pipe
w << "0123456789" w << "0123456789\n0123456789"
w.close w.close
assert_equal("0123456789\n0", r.gets(nil, 12))
assert_raise(TypeError) { r.gets(3,nil) } assert_raise(TypeError) { r.gets(3,nil) }
} }
end end