To accurate `pos`
pos shouldn't cache because it's not controllable
MODIFY
mrblib/io.rb
mrblib/io.rb
@@ -124,7 +124,6 @@ class IO
str = string.is_a?(String) ? string : string.to_s
return str.size unless str.size > 0
len = syswrite(str)
- @pos += len
len
end
@@ -146,7 +145,7 @@ class IO
def pos
raise IOError if closed?
- @pos
+ sysseek(0, SEEK_CUR) - @buf.length
end
alias_method :tell, :pos
@@ -160,7 +159,7 @@ class IO
def seek(i, whence = SEEK_SET)
raise IOError if closed?
- @pos = sysseek(i, whence)
+ sysseek(i, whence)
@buf = ''
0
end
@@ -172,7 +171,6 @@ class IO
def ungetc(substr)
raise TypeError.new "expect String, got #{substr.class}" unless substr.is_a?(String)
- @pos -= substr.size
if @buf.empty?
@buf = substr.dup
else
@@ -195,7 +193,6 @@ class IO
end
array = []
- start_pos = @pos
while 1
begin
_read_buf
@@ -204,15 +201,12 @@ class IO
break
end
- if length && (@pos - start_pos + @buf.size) >= length
- len = length - (@pos - start_pos)
- array.push @buf[0, len]
- @pos += len
- @buf = @buf[len, @buf.size - len]
+ if length && length <= @buf.size
+ array.push @buf[0, length]
+ @buf = @buf[length, @buf.size - length]
break
else
array.push @buf
- @pos += @buf.size
@buf = ''
end
end
@@ -240,7 +234,6 @@ class IO
end
array = []
- start_pos = @pos
while 1
begin
_read_buf
@@ -249,21 +242,17 @@ class IO
break
end
- if limit && (@pos - start_pos + @buf.size) >= limit
- len = limit - (@pos - start_pos)
- array.push @buf[0, len]
- @pos += len
- @buf = @buf[len, @buf.size - len]
+ if limit && limit <= @buf.size
+ array.push @buf[0, limit]
+ @buf = @buf[limit, @buf.size - limit]
break
elsif idx = @buf.index(rs)
len = idx + rs.size
array.push @buf[0, len]
- @pos += len
@buf = @buf[len, @buf.size - len]
break
else
array.push @buf
- @pos += @buf.size
@buf = ''
end
end
@@ -285,7 +274,6 @@ class IO
_read_buf
c = @buf[0]
@buf = @buf[1, @buf.size]
- @pos += 1
c
end

MODIFY
src/io.c
src/io.c
@@ -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, "@pos"), mrb_fixnum_value(0));
fptr = mrb_io_alloc(mrb);
fptr->fd = fd;
@@ -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));
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);
if (fptr != NULL) {
@@ -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));
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->fd = pipes[0];
fptr_r->readable = 1;
@@ -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));
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->fd = pipes[1];
fptr_w->readable = 0;

To accurate `pos`

pos shouldn't cache because it's not controllable