From 77368ce762b7a0e332580cd091fbe063ad2702c1 Mon Sep 17 00:00:00 2001 From: dearblue Date: Sun, 15 Sep 2019 22:31:59 +0900 Subject: Revert part of 8c90b5fc6 `IO#readline` and `IO#readchar` process in character units. --- mrbgems/mruby-io/mrblib/io.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'mrbgems/mruby-io') diff --git a/mrbgems/mruby-io/mrblib/io.rb b/mrbgems/mruby-io/mrblib/io.rb index aaf22da2e..3f5216fcb 100644 --- a/mrbgems/mruby-io/mrblib/io.rb +++ b/mrbgems/mruby-io/mrblib/io.rb @@ -253,12 +253,14 @@ class IO break end - if limit && limit <= @buf.bytesize - array.push IO._bufread(@buf, limit) + 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.bytesize - array.push IO._bufread(@buf, len) + len = idx + rs.size + array.push @buf[0, len] + @buf = @buf[len, @buf.size - len] break else array.push @buf @@ -281,7 +283,9 @@ class IO def readchar _read_buf - IO._bufread(@buf, 1) + c = @buf[0] + @buf = @buf[1, @buf.size] + c end def getc -- cgit v1.2.3 From 52a0ad4108d7b5f025c7f3def04649ea4fb56c1d Mon Sep 17 00:00:00 2001 From: dearblue Date: Sun, 15 Sep 2019 22:51:22 +0900 Subject: Fix `IO#pos` --- mrbgems/mruby-io/mrblib/io.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mrbgems/mruby-io') diff --git a/mrbgems/mruby-io/mrblib/io.rb b/mrbgems/mruby-io/mrblib/io.rb index 3f5216fcb..eea6e923a 100644 --- a/mrbgems/mruby-io/mrblib/io.rb +++ b/mrbgems/mruby-io/mrblib/io.rb @@ -150,7 +150,7 @@ class IO def pos raise IOError if closed? - sysseek(0, SEEK_CUR) - @buf.length + sysseek(0, SEEK_CUR) - @buf.bytesize end alias_method :tell, :pos -- cgit v1.2.3 From 7cc8c7d2fff9b0dd629c8c614c4b066a4f490de4 Mon Sep 17 00:00:00 2001 From: dearblue Date: Sun, 15 Sep 2019 22:52:09 +0900 Subject: Small improvement for mruby-io --- mrbgems/mruby-io/mrblib/io.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'mrbgems/mruby-io') diff --git a/mrbgems/mruby-io/mrblib/io.rb b/mrbgems/mruby-io/mrblib/io.rb index eea6e923a..32bac1f0d 100644 --- a/mrbgems/mruby-io/mrblib/io.rb +++ b/mrbgems/mruby-io/mrblib/io.rb @@ -123,8 +123,8 @@ class IO def write(string) str = string.is_a?(String) ? string : string.to_s - return str.size unless str.size > 0 - if 0 < @buf.length + return 0 if str.empty? + unless @buf.empty? # reset real pos ignore buf seek(pos, SEEK_SET) end @@ -141,7 +141,7 @@ class IO _check_readable begin buf = _read_buf - return buf.size == 0 + return buf.empty? rescue EOFError return true end @@ -170,7 +170,7 @@ class IO end def _read_buf - return @buf if @buf && @buf.size > 0 + return @buf if @buf && @buf.bytesize > 0 @buf = sysread(BUF_SIZE) end @@ -255,12 +255,12 @@ class IO if limit && limit <= @buf.size array.push @buf[0, limit] - @buf = @buf[limit, @buf.size - limit] + @buf[0, limit] = "" break elsif idx = @buf.index(rs) len = idx + rs.size array.push @buf[0, len] - @buf = @buf[len, @buf.size - len] + @buf[0, len] = "" break else array.push @buf @@ -284,7 +284,7 @@ class IO def readchar _read_buf c = @buf[0] - @buf = @buf[1, @buf.size] + @buf[0] = "" c end -- cgit v1.2.3 From 992ba476a95136eaad5b9b208d4ca5a1ca31324d Mon Sep 17 00:00:00 2001 From: dearblue Date: Sun, 15 Sep 2019 23:50:24 +0900 Subject: Fix broken UTF-8 characters by `IO#getc` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Character (multi-byte UTF-8) is destroyed when character spanning `IO::BUF_SIZE` (4096 bytes) exist. - Prepare file: ```ruby File.open("sample", "wb") { |f| f << "●" * 1370 } ``` - Before patched: ```ruby File.open("sample") { |f| a = []; while ch = f.getc; a << ch; end; p a } # => ["●", "●", ..., "●", "\xe2", "\x97", "\x8f", "●", "●", "●", "●"] - After patched: ```ruby File.open("sample") { |f| a = []; while ch = f.getc; a << ch; end; p a } # => ["●", "●", ..., "●", "●", "●", "●", "●", "●"] --- mrbgems/mruby-io/mrblib/io.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'mrbgems/mruby-io') diff --git a/mrbgems/mruby-io/mrblib/io.rb b/mrbgems/mruby-io/mrblib/io.rb index 32bac1f0d..6b83644ef 100644 --- a/mrbgems/mruby-io/mrblib/io.rb +++ b/mrbgems/mruby-io/mrblib/io.rb @@ -170,8 +170,14 @@ class IO end def _read_buf - return @buf if @buf && @buf.bytesize > 0 - @buf = sysread(BUF_SIZE) + return @buf if @buf && @buf.bytesize >= 4 # maximum UTF-8 character is 4 bytes + @buf ||= "" + begin + @buf += sysread(BUF_SIZE) + rescue EOFError => e + raise e if @buf.empty? + end + @buf end def ungetc(substr) -- cgit v1.2.3 From 6de1c64a9cc374cf7c201183ad7e3145ecfccae2 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Mon, 16 Sep 2019 07:44:51 +0900 Subject: Add small fix over #4712 --- mrbgems/mruby-io/mrblib/io.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'mrbgems/mruby-io') diff --git a/mrbgems/mruby-io/mrblib/io.rb b/mrbgems/mruby-io/mrblib/io.rb index 6b83644ef..8297cb1b5 100644 --- a/mrbgems/mruby-io/mrblib/io.rb +++ b/mrbgems/mruby-io/mrblib/io.rb @@ -140,8 +140,8 @@ class IO def eof? _check_readable begin - buf = _read_buf - return buf.empty? + _read_buf + return @buf.empty? rescue EOFError return true end @@ -177,7 +177,6 @@ class IO rescue EOFError => e raise e if @buf.empty? end - @buf end def ungetc(substr) -- cgit v1.2.3