From f8b9464118e584230ec3c76d39a19cc551fe8a5e Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 17 May 2022 07:18:44 +0900 Subject: [PATCH] string.rb (each_line): use byteindex for performance; fix #4522 Character-wise String#index method calculate UTF-8 character width numerous times if the string is long. --- mrblib/string.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mrblib/string.rb b/mrblib/string.rb index 84e7530c9..94a593f48 100644 --- a/mrblib/string.rb +++ b/mrblib/string.rb @@ -30,10 +30,10 @@ class String self_len = length sep_len = separator.length - while (pointer = string.index(separator, start)) + while (pointer = string.byteindex(separator, start)) pointer += sep_len - pointer += 1 while paragraph_mode && string[pointer] == "\n" - block.call(string[start, pointer - start]) + pointer += 1 while paragraph_mode && string.getbyte(pointer) == 10 # 10 == \n + block.call(string.byteslice(start, pointer - start)) start = pointer end return self if start == self_len