Unverified Commit 27f42288 authored by ksss's avatar ksss

To accurate `pos`

pos shouldn't cache because it's not controllable
parent 3a73a584
...@@ -124,7 +124,6 @@ class IO ...@@ -124,7 +124,6 @@ class IO
str = string.is_a?(String) ? string : string.to_s str = string.is_a?(String) ? string : string.to_s
return str.size unless str.size > 0 return str.size unless str.size > 0
len = syswrite(str) len = syswrite(str)
@pos += len
len len
end end
...@@ -146,7 +145,7 @@ class IO ...@@ -146,7 +145,7 @@ class IO
def pos def pos
raise IOError if closed? raise IOError if closed?
@pos sysseek(0, SEEK_CUR) - @buf.length
end end
alias_method :tell, :pos alias_method :tell, :pos
...@@ -160,7 +159,7 @@ class IO ...@@ -160,7 +159,7 @@ class IO
def seek(i, whence = SEEK_SET) def seek(i, whence = SEEK_SET)
raise IOError if closed? raise IOError if closed?
@pos = sysseek(i, whence) sysseek(i, whence)
@buf = '' @buf = ''
0 0
end end
...@@ -172,7 +171,6 @@ class IO ...@@ -172,7 +171,6 @@ class IO
def ungetc(substr) def ungetc(substr)
raise TypeError.new "expect String, got #{substr.class}" unless substr.is_a?(String) raise TypeError.new "expect String, got #{substr.class}" unless substr.is_a?(String)
@pos -= substr.size
if @buf.empty? if @buf.empty?
@buf = substr.dup @buf = substr.dup
else else
...@@ -195,7 +193,6 @@ class IO ...@@ -195,7 +193,6 @@ class IO
end end
array = [] array = []
start_pos = @pos
while 1 while 1
begin begin
_read_buf _read_buf
...@@ -204,15 +201,12 @@ class IO ...@@ -204,15 +201,12 @@ class IO
break break
end end
if length && (@pos - start_pos + @buf.size) >= length if length && length <= @buf.size
len = length - (@pos - start_pos) array.push @buf[0, length]
array.push @buf[0, len] @buf = @buf[length, @buf.size - length]
@pos += len
@buf = @buf[len, @buf.size - len]
break break
else else
array.push @buf array.push @buf
@pos += @buf.size
@buf = '' @buf = ''
end end
end end
...@@ -240,7 +234,6 @@ class IO ...@@ -240,7 +234,6 @@ class IO
end end
array = [] array = []
start_pos = @pos
while 1 while 1
begin begin
_read_buf _read_buf
...@@ -249,21 +242,17 @@ class IO ...@@ -249,21 +242,17 @@ class IO
break break
end end
if limit && (@pos - start_pos + @buf.size) >= limit if limit && limit <= @buf.size
len = limit - (@pos - start_pos) array.push @buf[0, limit]
array.push @buf[0, len] @buf = @buf[limit, @buf.size - limit]
@pos += len
@buf = @buf[len, @buf.size - len]
break break
elsif idx = @buf.index(rs) elsif idx = @buf.index(rs)
len = idx + rs.size len = idx + rs.size
array.push @buf[0, len] array.push @buf[0, len]
@pos += len
@buf = @buf[len, @buf.size - len] @buf = @buf[len, @buf.size - len]
break break
else else
array.push @buf array.push @buf
@pos += @buf.size
@buf = '' @buf = ''
end end
end end
...@@ -285,7 +274,6 @@ class IO ...@@ -285,7 +274,6 @@ class IO
_read_buf _read_buf
c = @buf[0] c = @buf[0]
@buf = @buf[1, @buf.size] @buf = @buf[1, @buf.size]
@pos += 1
c c
end end
......
...@@ -389,7 +389,6 @@ mrb_io_s_popen(mrb_state *mrb, mrb_value klass) ...@@ -389,7 +389,6 @@ mrb_io_s_popen(mrb_state *mrb, mrb_value klass)
} }
mrb_iv_set(mrb, io, mrb_intern_cstr(mrb, "@buf"), mrb_str_new_cstr(mrb, "")); mrb_iv_set(mrb, io, mrb_intern_cstr(mrb, "@buf"), mrb_str_new_cstr(mrb, ""));
mrb_iv_set(mrb, io, mrb_intern_cstr(mrb, "@pos"), mrb_fixnum_value(0));
fptr = mrb_io_alloc(mrb); fptr = mrb_io_alloc(mrb);
fptr->fd = fd; fptr->fd = fd;
...@@ -443,7 +442,6 @@ mrb_io_initialize(mrb_state *mrb, mrb_value io) ...@@ -443,7 +442,6 @@ mrb_io_initialize(mrb_state *mrb, mrb_value io)
flags = mrb_io_modestr_to_flags(mrb, mrb_string_value_cstr(mrb, &mode)); flags = mrb_io_modestr_to_flags(mrb, mrb_string_value_cstr(mrb, &mode));
mrb_iv_set(mrb, io, mrb_intern_cstr(mrb, "@buf"), mrb_str_new_cstr(mrb, "")); mrb_iv_set(mrb, io, mrb_intern_cstr(mrb, "@buf"), mrb_str_new_cstr(mrb, ""));
mrb_iv_set(mrb, io, mrb_intern_cstr(mrb, "@pos"), mrb_fixnum_value(0));
fptr = (struct mrb_io *)DATA_PTR(io); fptr = (struct mrb_io *)DATA_PTR(io);
if (fptr != NULL) { if (fptr != NULL) {
...@@ -801,7 +799,6 @@ mrb_io_s_pipe(mrb_state *mrb, mrb_value klass) ...@@ -801,7 +799,6 @@ mrb_io_s_pipe(mrb_state *mrb, mrb_value klass)
r = mrb_obj_value(mrb_data_object_alloc(mrb, mrb_class_ptr(klass), NULL, &mrb_io_type)); r = mrb_obj_value(mrb_data_object_alloc(mrb, mrb_class_ptr(klass), NULL, &mrb_io_type));
mrb_iv_set(mrb, r, mrb_intern_cstr(mrb, "@buf"), mrb_str_new_cstr(mrb, "")); mrb_iv_set(mrb, r, mrb_intern_cstr(mrb, "@buf"), mrb_str_new_cstr(mrb, ""));
mrb_iv_set(mrb, r, mrb_intern_cstr(mrb, "@pos"), mrb_fixnum_value(0));
fptr_r = mrb_io_alloc(mrb); fptr_r = mrb_io_alloc(mrb);
fptr_r->fd = pipes[0]; fptr_r->fd = pipes[0];
fptr_r->readable = 1; fptr_r->readable = 1;
...@@ -812,7 +809,6 @@ mrb_io_s_pipe(mrb_state *mrb, mrb_value klass) ...@@ -812,7 +809,6 @@ mrb_io_s_pipe(mrb_state *mrb, mrb_value klass)
w = mrb_obj_value(mrb_data_object_alloc(mrb, mrb_class_ptr(klass), NULL, &mrb_io_type)); w = mrb_obj_value(mrb_data_object_alloc(mrb, mrb_class_ptr(klass), NULL, &mrb_io_type));
mrb_iv_set(mrb, w, mrb_intern_cstr(mrb, "@buf"), mrb_str_new_cstr(mrb, "")); mrb_iv_set(mrb, w, mrb_intern_cstr(mrb, "@buf"), mrb_str_new_cstr(mrb, ""));
mrb_iv_set(mrb, w, mrb_intern_cstr(mrb, "@pos"), mrb_fixnum_value(0));
fptr_w = mrb_io_alloc(mrb); fptr_w = mrb_io_alloc(mrb);
fptr_w->fd = pipes[1]; fptr_w->fd = pipes[1];
fptr_w->readable = 0; fptr_w->readable = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment