summaryrefslogtreecommitdiffhomepage
path: root/mrblib/string.rb
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2021-08-31 13:00:15 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2021-09-01 07:00:55 +0900
commit3622f2c4b51d4b1f9eb0a2470ea7cd2ee24fe3a5 (patch)
tree257435cdb71a3431e77df7620a04a77007d3cda1 /mrblib/string.rb
parent2c41739b66e34ee20f614e4793dfb1dba4fd7274 (diff)
downloadmruby-3622f2c4b51d4b1f9eb0a2470ea7cd2ee24fe3a5.tar.gz
mruby-3622f2c4b51d4b1f9eb0a2470ea7cd2ee24fe3a5.zip
string.c: implement `__sub_replace()` in C.
To reduce number of string allocation.
Diffstat (limited to 'mrblib/string.rb')
-rw-r--r--mrblib/string.rb37
1 files changed, 5 insertions, 32 deletions
diff --git a/mrblib/string.rb b/mrblib/string.rb
index 7c90303ae..2b3178688 100644
--- a/mrblib/string.rb
+++ b/mrblib/string.rb
@@ -42,32 +42,6 @@ class String
self
end
- # private method for gsub/sub
- def __sub_replace(rep, pre, m, post)
- s = ""
- i = 0
- while j = rep.index("\\", i)
- break if j == rep.length-1
- t = case rep[j+1]
- when "\\"
- "\\"
- when "`"
- pre
- when "&", "0"
- m
- when "'"
- post
- when "1", "2", "3", "4", "5", "6", "7", "8", "9"
- ""
- else
- rep[j, 2]
- end
- s += rep[i, j-i] + t
- i = j + 2
- end
- s + rep[i, rep.length-i]
- end
-
##
# Replace all matches of +pattern+ with +replacement+.
# Call block (if given) for each match and replace
@@ -92,7 +66,7 @@ class String
result << if block
block.call(pattern).to_s
else
- __sub_replace(replace, self[0, found], pattern, self[offset..-1] || "")
+ self.__sub_replace(replace, pattern, found)
end
if plen == 0
result << self[offset, 1]
@@ -145,17 +119,16 @@ class String
block = nil
end
result = []
- this = dup
found = index(pattern)
- return this unless found
- result << this[0, found]
+ return self.dup unless found
+ result << self[0, found]
offset = found + pattern.length
result << if block
block.call(pattern).to_s
else
- __sub_replace(replace, this[0, found], pattern, this[offset..-1] || "")
+ self.__sub_replace(replace, pattern, found)
end
- result << this[offset..-1] if offset < length
+ result << self[offset..-1] if offset < length
result.join
end