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

* io.c (appendline): move RS comparison to rb_io_getline_1().

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2007-12-25 09:52:52 +00:00
parent 36df55c6e4
commit 3878b30881
4 changed files with 31 additions and 38 deletions

View file

@ -4,6 +4,10 @@ Tue Dec 25 18:40:46 2007 Koichi Sasada <ko1@atdot.net>
vm.c: comment out debug functions. vm.c: comment out debug functions.
Tue Dec 25 18:37:42 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* io.c (appendline): move RS comparison to rb_io_getline_1().
Tue Dec 25 18:27:51 2007 Tanaka Akira <akr@fsij.org> Tue Dec 25 18:27:51 2007 Tanaka Akira <akr@fsij.org>
* string.c (rb_str_each_line): don't call rb_enc_codepoint with empty * string.c (rb_str_each_line): don't call rb_enc_codepoint with empty

View file

@ -26,3 +26,13 @@ assert_finish 10, %q{
rescue LoadError rescue LoadError
end end
}, '[ruby-dev:32566]' }, '[ruby-dev:32566]'
assert_finish 1, %q{
r, w = IO.pipe
Thread.new {
w << "ab"
sleep 0.1
w << "ab"
}
p r.gets("abab")
}

View file

@ -3,16 +3,6 @@
# So all tests will cause failure. # So all tests will cause failure.
# #
assert_finish 1, %q{
r, w = IO.pipe
Thread.new {
w << "ab"
sleep 0.1
w << "ab"
}
p r.gets("abab")
}
assert_normal_exit %q{ assert_normal_exit %q{
begin begin
raise raise

45
io.c
View file

@ -1665,12 +1665,11 @@ rscheck(const char *rsptr, long rslen, VALUE rs)
} }
static int static int
appendline(rb_io_t *fptr, int delim, const char *rsptr, int rslen, VALUE rs, VALUE *strp, long *lp) appendline(rb_io_t *fptr, int delim, const char *rsptr, int rslen, VALUE *strp, long *lp)
{ {
VALUE str = *strp; VALUE str = *strp;
int c = EOF; int c = EOF;
long limit = *lp; long limit = *lp;
rb_encoding *enc = io_input_encoding(fptr);
if (rsptr == 0) if (rsptr == 0)
rslen = 1; rslen = 1;
@ -1678,34 +1677,14 @@ appendline(rb_io_t *fptr, int delim, const char *rsptr, int rslen, VALUE rs, VAL
do { do {
long pending = READ_DATA_PENDING_COUNT(fptr); long pending = READ_DATA_PENDING_COUNT(fptr);
if (pending > 0) { if (pending > 0) {
const char *s = READ_DATA_PENDING_PTR(fptr); const char *p = READ_DATA_PENDING_PTR(fptr);
const char *p, *e, *pp; const char *e;
long last = 0, len = (c != EOF); long last = 0, len = (c != EOF);
if (limit > 0 && pending > limit) pending = limit; if (limit > 0 && pending > limit) pending = limit;
pp = p = s;
again: again:
e = memchr(p, delim, pending); e = memchr(p, delim, pending);
if (e) { if (e) pending = e - p + 1;
const char *p0 = e - rslen + 1;
if (p0 < s) {
p = e + 1;
goto again;
}
pp = rb_enc_left_char_head(pp, p0, enc);
if (pp != p0) {
p = e + 1;
goto again;
}
if (rsptr) {
rscheck(rsptr, rslen, rs);
if (memcmp(p0, rsptr, rslen) != 0) {
p = e + 1;
goto again;
}
}
pending = e - s + 1;
}
len += pending; len += pending;
if (!NIL_P(str)) { if (!NIL_P(str)) {
last = RSTRING_LEN(str); last = RSTRING_LEN(str);
@ -1785,7 +1764,7 @@ rb_io_getline_fast(rb_io_t *fptr, unsigned char delim, long limit)
int c, nolimit = 0; int c, nolimit = 0;
for (;;) { for (;;) {
c = appendline(fptr, delim, 0, 0, 0, &str, &limit); c = appendline(fptr, delim, 0, 0, &str, &limit);
if (c == EOF || c == delim) break; if (c == EOF || c == delim) break;
if (limit == 0) { if (limit == 0) {
nolimit = 1; nolimit = 1;
@ -1853,9 +1832,11 @@ rb_io_getline_1(VALUE rs, long limit, VALUE io)
VALUE str = Qnil; VALUE str = Qnil;
rb_io_t *fptr; rb_io_t *fptr;
int nolimit = 0; int nolimit = 0;
rb_encoding *enc;
GetOpenFile(io, fptr); GetOpenFile(io, fptr);
rb_io_check_readable(fptr); rb_io_check_readable(fptr);
enc = io_input_encoding(fptr);
if (NIL_P(rs)) { if (NIL_P(rs)) {
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;
@ -1888,9 +1869,17 @@ rb_io_getline_1(VALUE rs, long limit, VALUE io)
} }
newline = rsptr[rslen - 1]; newline = rsptr[rslen - 1];
while ((c = appendline(fptr, newline, rsptr, rslen, rs, &str, &limit)) != EOF) { while ((c = appendline(fptr, newline, rsptr, rslen, &str, &limit)) != EOF) {
if (c == newline) { if (c == newline) {
break; const char *s, *p, *pp;
if (RSTRING_LEN(str) < rslen) continue;
s = RSTRING_PTR(str);
p = s + RSTRING_LEN(str) - rslen;
pp = rb_enc_left_char_head(s, p, enc);
if (pp != p) continue;
if (!rspara) rscheck(rsptr, rslen, rs);
if (memcmp(p, rsptr, rslen) == 0) break;
} }
if (limit == 0) { if (limit == 0) {
nolimit = 1; nolimit = 1;