From 8ea45b862aa9ff358b3642ac14b03304fcd6846e Mon Sep 17 00:00:00 2001 From: Ryan Lopopolo Date: Sun, 23 Jun 2019 03:29:47 +0100 Subject: Optimize String#each_line --- mrblib/string.rb | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'mrblib/string.rb') diff --git a/mrblib/string.rb b/mrblib/string.rb index 0bac52a20..60129a845 100644 --- a/mrblib/string.rb +++ b/mrblib/string.rb @@ -11,7 +11,7 @@ class String # and pass the respective line. # # ISO 15.2.10.5.15 - def each_line(separator = "\n", getline_args = nil) + def each_line(separator = "\n") return to_enum(:each_line, separator, getline_args) unless block_given? if separator.nil? @@ -20,22 +20,25 @@ class String end raise TypeError unless separator.is_a?(String) - start = 0 pointer = 0 + start = 0 string = dup + self_len = length + sep_len = separator.length + should_yield_subclass_instances = self.class != String if separator.empty? matched_newlines = 0 - while pointer < string.length + while pointer < self_len c = string[pointer] if c == "\n" matched_newlines += 1 - elsif matched_newlines > 1 && self.class == String - yield string[start...pointer] + elsif matched_newlines > 1 && should_yield_subclass_instances + yield self.class.new(string[start, pointer - start]) matched_newlines = 0 start = pointer elsif matched_newlines > 1 - yield self.class.new(string[start...pointer]) + yield string[start, pointer - start] matched_newlines = 0 start = pointer else @@ -45,21 +48,21 @@ class String end else while (pointer = string.index(separator, start)) - pointer += separator.length - if self.class == String - yield string[start...pointer] + pointer += sep_len + if should_yield_subclass_instances + yield self.class.new(string[start, pointer - start]) else - yield self.class.new(string[start...pointer]) + yield string[start, pointer - start] end start = pointer end end - return self if start == string.length + return self if start == self_len - if self.class == String - yield string[start..-1] + if should_yield_subclass_instances + yield self.class.new(string[start, self_len - start]) else - yield self.class.new(string[start..-1]) + yield string[start, self_len - start] end self end -- cgit v1.2.3