diff options
| author | Ryan Lopopolo <[email protected]> | 2019-06-25 00:01:51 +0200 |
|---|---|---|
| committer | Ryan Lopopolo <[email protected]> | 2019-06-25 00:01:51 +0200 |
| commit | 46c972f746f37a26104b1e9f317ce5a02daa9f40 (patch) | |
| tree | 3082da42c2b1719fe79e939c228b43b15f48bd1d /mrblib | |
| parent | 6b92acf361c8f1979dcda95c41324cccc8d767a9 (diff) | |
| download | mruby-46c972f746f37a26104b1e9f317ce5a02daa9f40.tar.gz mruby-46c972f746f37a26104b1e9f317ce5a02daa9f40.zip | |
Unify loops to minimize bytecode size
Diffstat (limited to 'mrblib')
| -rw-r--r-- | mrblib/string.rb | 42 |
1 files changed, 13 insertions, 29 deletions
diff --git a/mrblib/string.rb b/mrblib/string.rb index f1c7f3ba4..d72002209 100644 --- a/mrblib/string.rb +++ b/mrblib/string.rb @@ -20,42 +20,26 @@ class String end raise TypeError unless separator.is_a?(String) - pointer = 0 + paragraph_mode = false + if separator.empty? + paragraph_mode = true + separator = "\n\n" + end 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 < self_len - c = string[pointer] - if c == "\n" - matched_newlines += 1 - elsif matched_newlines > 1 && should_yield_subclass_instances - block.call(self.class.new(string[start, pointer - start])) - matched_newlines = 0 - start = pointer - elsif matched_newlines > 1 - block.call(string[start, pointer - start]) - matched_newlines = 0 - start = pointer - else - matched_newlines = 0 - end - pointer += 1 - end - else - while (pointer = string.index(separator, start)) - pointer += sep_len - if should_yield_subclass_instances - block.call(self.class.new(string[start, pointer - start])) - else - block.call(string[start, pointer - start]) - end - start = pointer + while (pointer = string.index(separator, start)) + pointer += sep_len + pointer += 1 while paragraph_mode && string[pointer] == "\n" + if should_yield_subclass_instances + block.call(self.class.new(string[start, pointer - start])) + else + block.call(string[start, pointer - start]) end + start = pointer end return self if start == self_len |
