Changed regex error handling, so that errors can be handled gracefully in lua, and made it so gsub returns the exact matches and replacements.
This commit is contained in:
parent
cd5c64fe8c
commit
3b816a2b4a
|
@ -6,7 +6,7 @@ regex.__index = function(table, key) return regex[key]; end
|
||||||
regex.match = function(pattern_string, string, offset, options)
|
regex.match = function(pattern_string, string, offset, options)
|
||||||
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)
|
||||||
return regex.cmatch(pattern, string, offset, options)
|
return regex.cmatch(pattern, string, offset or 1, options or 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Will iterate back through any UTF-8 bytes so that we don't replace bits
|
-- Will iterate back through any UTF-8 bytes so that we don't replace bits
|
||||||
|
@ -39,11 +39,11 @@ 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 n = 0
|
local matches, replacements = {}, {}
|
||||||
repeat
|
repeat
|
||||||
indices = { regex.cmatch(pattern, str) }
|
indices = { regex.cmatch(pattern, str) }
|
||||||
if #indices > 0 then
|
if #indices > 0 then
|
||||||
n = n + 1
|
table.insert(matches, indices)
|
||||||
local currentReplacement = replacement
|
local currentReplacement = replacement
|
||||||
if #indices > 2 then
|
if #indices > 2 then
|
||||||
for i = 1, (#indices/2 - 1) do
|
for i = 1, (#indices/2 - 1) do
|
||||||
|
@ -55,6 +55,7 @@ regex.gsub = function(pattern_string, str, replacement)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
currentReplacement = string.gsub(currentReplacement, "\\%d", "")
|
currentReplacement = string.gsub(currentReplacement, "\\%d", "")
|
||||||
|
table.insert(replacements, { indices[1], #currentReplacement+indices[1] })
|
||||||
if indices[1] > 1 then
|
if indices[1] > 1 then
|
||||||
result = result ..
|
result = result ..
|
||||||
str:sub(1, previous_character(str, indices[1])) .. currentReplacement
|
str:sub(1, previous_character(str, indices[1])) .. currentReplacement
|
||||||
|
@ -64,6 +65,6 @@ regex.gsub = function(pattern_string, str, replacement)
|
||||||
str = str:sub(indices[2])
|
str = str:sub(indices[2])
|
||||||
end
|
end
|
||||||
until #indices == 0 or indices[1] == indices[2]
|
until #indices == 0 or indices[1] == indices[2]
|
||||||
return result .. str, n
|
return result .. str, matches, replacements
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -45,9 +45,11 @@ static int f_pcre_compile(lua_State *L) {
|
||||||
}
|
}
|
||||||
PCRE2_UCHAR buffer[256];
|
PCRE2_UCHAR buffer[256];
|
||||||
pcre2_get_error_message(errorNumber, buffer, sizeof(buffer));
|
pcre2_get_error_message(errorNumber, buffer, sizeof(buffer));
|
||||||
luaL_error(L, "regex compilation failed at offset %d: %s",
|
lua_pushnil(L);
|
||||||
(int)errorOffset, buffer);
|
char message[1024];
|
||||||
return 0;
|
len = snprintf(message, sizeof(message), "regex compilation failed at offset %d: %s", (int)errorOffset, buffer);
|
||||||
|
lua_pushlstring(L, message, len);
|
||||||
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes string, compiled regex, returns list of indices of matched groups
|
// Takes string, compiled regex, returns list of indices of matched groups
|
||||||
|
|
Loading…
Reference in New Issue