utils.lua.escape_string_regex rewritten with single string.gsub call.

This commit is contained in:
Augusto Goulart
2020-03-28 17:50:15 +01:00
committed by Antonin Décimo
parent 8dbfb3ceca
commit cad47b8535

View File

@@ -128,11 +128,13 @@ end
--- Escapes a string to use as a regex.
-- @string string to escape.
function utils.lua.escape_string_regex(string)
-- ^$()%.[]*+-?
return string:gsub('%%', '%%%%'):gsub('^%^', '%%^'):gsub('%$$', '%%$')
:gsub('%(', '%%('):gsub('%)', '%%)'):gsub('%.', '%%.')
:gsub('%[', '%%['):gsub('%]', '%%]'):gsub('%*', '%%*')
:gsub('%+', '%%+'):gsub('%-', '%%-'):gsub('%?', '%%?')
return string:gsub('[%^%$%(%)%%%.%[%]%*%+%-%?%z]', {
['^'] = '%^'; ['$'] = '%$'; ['('] = '%(';
[')'] = '%)'; ['%'] = '%%'; ['.'] = '%.';
['['] = '%['; [']'] = '%]'; ['*'] = '%*';
['+'] = '%+'; ['-'] = '%-'; ['?'] = '%?';
['\0'] = '%z';
})
end