I have
- A list of pattern/replacement pairs.
- A long string to do the replacements on (several kByte or even MBytes).
All occurrences of all patterns have to be replaced by their corresponding replacement texts. Each pattern may exist more than once in the string.
Currently, I do this by iterating over the list of pattern/replacement pairs and using string.gsub
every time:
for _, pattern, replace in iter(replace_patterns) do
body = body:gsub(pattern, replace)
end
(iter
is a helper function to better iterate through the patterns.)
Question: Is this the best way to do it? I fear this to be inefficient as every call to gsub
will scan the whole long string.
P.S. I read https://stackoverflow.com/a/12865406/5005936 (helped me to reduce string usage and others) and https://stackoverflow.com/a/38422229/5005936 (but I don't want to write native code in this context...)