2021-04-28 06:22:26 +02:00
|
|
|
|
2021-04-29 00:18:30 +02:00
|
|
|
-- So that in addition to regex.gsub(pattern, string), we can also do
|
|
|
|
-- pattern:gsub(string).
|
2021-04-28 06:22:26 +02:00
|
|
|
regex.__index = function(table, key) return regex[key]; end
|
|
|
|
|
2021-05-07 01:22:36 +02:00
|
|
|
regex.match = function(pattern_string, string, offset)
|
2021-05-01 07:31:49 +02:00
|
|
|
local pattern = type(pattern_string) == "table" and
|
2021-04-29 00:18:30 +02:00
|
|
|
pattern_string or regex.compile(pattern_string)
|
2021-05-07 01:22:36 +02:00
|
|
|
return regex.cmatch(pattern, string, offset)
|
2021-04-28 06:22:26 +02:00
|
|
|
end
|
|
|
|
|
2021-05-01 07:46:06 +02:00
|
|
|
-- Will iterate back through any UTF-8 bytes so that we don't replace bits
|
|
|
|
-- mid character.
|
2021-05-01 07:31:49 +02:00
|
|
|
local function previous_character(str, index)
|
|
|
|
local byte
|
|
|
|
repeat
|
|
|
|
index = index - 1
|
|
|
|
byte = string.byte(str, index)
|
|
|
|
until byte < 128 or byte >= 192
|
|
|
|
return index
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Moves to the end of the identified character.
|
|
|
|
local function end_character(str, index)
|
|
|
|
local byte = string.byte(str, index + 1)
|
|
|
|
while byte >= 128 and byte < 192 do
|
|
|
|
index = index + 1
|
|
|
|
byte = string.byte(str, index + 1)
|
|
|
|
end
|
|
|
|
return index
|
|
|
|
end
|
|
|
|
|
2021-04-29 00:18:30 +02:00
|
|
|
-- Build off matching. For now, only support basic replacements, but capture
|
|
|
|
-- groupings should be doable. We can even have custom group replacements and
|
2021-05-01 07:31:49 +02:00
|
|
|
-- transformations and stuff in lua. Currently, this takes group replacements
|
|
|
|
-- as \1 - \9.
|
|
|
|
-- Should work on UTF-8 text.
|
2021-04-28 06:22:26 +02:00
|
|
|
regex.gsub = function(pattern_string, string, replacement)
|
2021-05-01 07:31:49 +02:00
|
|
|
local pattern = type(pattern_string) == "table" and
|
2021-04-29 00:18:30 +02:00
|
|
|
pattern_string or regex.compile(pattern_string)
|
2021-05-05 00:31:46 +02:00
|
|
|
local result, str, indices, n = "", string
|
2021-04-28 06:22:26 +02:00
|
|
|
repeat
|
|
|
|
indices = { regex.cmatch(pattern, str) }
|
|
|
|
if #indices > 0 then
|
2021-05-05 00:31:46 +02:00
|
|
|
n = n + 1
|
2021-05-01 07:31:49 +02:00
|
|
|
local currentReplacement = replacement
|
|
|
|
if #indices > 2 then
|
|
|
|
for i = 1, (#indices/2 - 1) do
|
2021-05-01 07:46:06 +02:00
|
|
|
currentReplacement = string.gsub(
|
|
|
|
currentReplacement,
|
|
|
|
"\\" .. i,
|
|
|
|
str:sub(indices[i*2+1], end_character(str,indices[i*2+2]-1))
|
|
|
|
)
|
2021-05-01 07:31:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
currentReplacement = string.gsub(currentReplacement, "\\%d", "")
|
|
|
|
if indices[1] > 1 then
|
2021-05-01 07:46:06 +02:00
|
|
|
result = result ..
|
|
|
|
str:sub(1, previous_character(str, indices[1])) .. currentReplacement
|
2021-05-01 07:31:49 +02:00
|
|
|
else
|
|
|
|
result = result .. currentReplacement
|
|
|
|
end
|
|
|
|
str = str:sub(indices[2])
|
2021-04-28 06:22:26 +02:00
|
|
|
end
|
|
|
|
until #indices == 0 or indices[1] == indices[2]
|
2021-05-05 00:31:46 +02:00
|
|
|
return result .. str, n
|
2021-04-28 06:22:26 +02:00
|
|
|
end
|
|
|
|
|