From 038aa6beb21d1c6ceb345f9abbae4d1aab09087f Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 17 May 2022 18:20:06 +0900 Subject: [PATCH] strinc.rb (sub, gsub): use byte index instead of char index. --- mrblib/string.rb | 14 +++++++------- src/string.c | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/mrblib/string.rb b/mrblib/string.rb index ee3d857f7..47fe59026 100644 --- a/mrblib/string.rb +++ b/mrblib/string.rb @@ -60,8 +60,8 @@ class String end offset = 0 result = [] - while found = index(pattern, offset) - result << self[offset, found - offset] + while found = self.byteindex(pattern, offset) + result << self.byteslice(offset, found - offset) offset = found + plen result << if block block.call(pattern).to_s @@ -69,11 +69,11 @@ class String self.__sub_replace(replace, pattern, found) end if plen == 0 - result << self[offset, 1] + result << self.byteslice(offset, 1) offset += 1 end end - result << self[offset..-1] if offset < length + result << self.byteslice(offset..-1) if offset < length result.join end @@ -119,16 +119,16 @@ class String block = nil end result = [] - found = index(pattern) + found = self.index(pattern) return self.dup unless found - result << self[0, found] + result << self.byteslice(0, found) offset = found + pattern.length result << if block block.call(pattern).to_s else self.__sub_replace(replace, pattern, found) end - result << self[offset..-1] if offset < length + result << self.byteslice(offset..-1) if offset < length result.join end diff --git a/src/string.c b/src/string.c index 897e077e3..9dce703e2 100644 --- a/src/string.c +++ b/src/string.c @@ -2831,8 +2831,8 @@ mrb_str_setbyte(mrb_state *mrb, mrb_value str) /* * call-seq: * str.byteslice(integer) -> new_str or nil - * str.byteslice(integer, integer) -> new_str or nil - * str.byteslice(range) -> new_str or nil + * str.byteslice(integer, integer) -> new_str or nil + * str.byteslice(range) -> new_str or nil * * Byte Reference---If passed a single Integer, returns a * substring of one byte at that position. If passed two Integer @@ -2910,13 +2910,13 @@ sub_replace(mrb_state *mrb, mrb_value self) mrb_str_cat(mrb, result, "\\", 1); break; case '`': - mrb_str_cat(mrb, result, RSTRING_PTR(self), chars2bytes(self, 0, found)); + mrb_str_cat(mrb, result, RSTRING_PTR(self), found); break; case '&': case '0': mrb_str_cat(mrb, result, match, mlen); break; case '\'': - offset = chars2bytes(self, 0, found) + mlen; + offset = found + mlen; if (RSTRING_LEN(self) > offset) { mrb_str_cat(mrb, result, RSTRING_PTR(self)+offset, RSTRING_LEN(self)-offset); }