Improve `regex.gsub` performance (#1220)
This commit is contained in:
parent
5db5512663
commit
f7ad8753eb
|
@ -39,10 +39,11 @@ end
|
||||||
regex.gsub = function(pattern_string, str, replacement)
|
regex.gsub = function(pattern_string, str, replacement)
|
||||||
local pattern = type(pattern_string) == "table" and
|
local pattern = type(pattern_string) == "table" and
|
||||||
pattern_string or regex.compile(pattern_string)
|
pattern_string or regex.compile(pattern_string)
|
||||||
local result, indices = ""
|
local result, indices = {}
|
||||||
local matches, replacements = {}, {}
|
local matches, replacements = {}, {}
|
||||||
|
local offset = 0
|
||||||
repeat
|
repeat
|
||||||
indices = { regex.cmatch(pattern, str) }
|
indices = { regex.cmatch(pattern, str, offset) }
|
||||||
if #indices > 0 then
|
if #indices > 0 then
|
||||||
table.insert(matches, indices)
|
table.insert(matches, indices)
|
||||||
local currentReplacement = replacement
|
local currentReplacement = replacement
|
||||||
|
@ -58,14 +59,13 @@ regex.gsub = function(pattern_string, str, replacement)
|
||||||
currentReplacement = string.gsub(currentReplacement, "\\%d", "")
|
currentReplacement = string.gsub(currentReplacement, "\\%d", "")
|
||||||
table.insert(replacements, { indices[1], #currentReplacement+indices[1] })
|
table.insert(replacements, { indices[1], #currentReplacement+indices[1] })
|
||||||
if indices[1] > 1 then
|
if indices[1] > 1 then
|
||||||
result = result ..
|
table.insert(result, str:sub(offset, previous_character(str, indices[1])) .. currentReplacement)
|
||||||
str:sub(1, previous_character(str, indices[1])) .. currentReplacement
|
|
||||||
else
|
else
|
||||||
result = result .. currentReplacement
|
table.insert(result, currentReplacement)
|
||||||
end
|
end
|
||||||
str = str:sub(indices[2])
|
offset = indices[2]
|
||||||
end
|
end
|
||||||
until #indices == 0 or indices[1] == indices[2]
|
until #indices == 0 or indices[1] == indices[2]
|
||||||
return result .. str, matches, replacements
|
return table.concat(result) .. str:sub(offset), matches, replacements
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue