Reworked files to conform to style guidelines.
This commit is contained in:
parent
488ebba7e4
commit
a9dd790321
|
@ -1,16 +1,20 @@
|
||||||
|
|
||||||
-- So that in addition to regex.gsub(pattern, string), we can also do pattern:gsub(string).
|
-- So that in addition to regex.gsub(pattern, string), we can also do
|
||||||
|
-- pattern:gsub(string).
|
||||||
regex.__index = function(table, key) return regex[key]; end
|
regex.__index = function(table, key) return regex[key]; end
|
||||||
|
|
||||||
regex.match = function(pattern_string, string)
|
regex.match = function(pattern_string, string)
|
||||||
local pattern = type(pattern_string) == "userdata" and pattern_string or regex.compile(pattern_string)
|
local pattern = type(pattern_string) == "userdata" and
|
||||||
|
pattern_string or regex.compile(pattern_string)
|
||||||
return regex.cmatch(pattern, string)
|
return regex.cmatch(pattern, string)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Build off matching. For now, only support basic replacements, but capture groupings should be doable.
|
-- Build off matching. For now, only support basic replacements, but capture
|
||||||
-- We can even have custom group replacements and transformations and stuff in lua.
|
-- groupings should be doable. We can even have custom group replacements and
|
||||||
|
-- transformations and stuff in lua.
|
||||||
regex.gsub = function(pattern_string, string, replacement)
|
regex.gsub = function(pattern_string, string, replacement)
|
||||||
local pattern = type(pattern_string) == "userdata" and pattern_string or regex.compile(pattern_string)
|
local pattern = type(pattern_string) == "userdata" and
|
||||||
|
pattern_string or regex.compile(pattern_string)
|
||||||
local offset, result, str, indices = 0, "", string
|
local offset, result, str, indices = 0, "", string
|
||||||
repeat
|
repeat
|
||||||
str = str:sub(offset)
|
str = str:sub(offset)
|
||||||
|
|
|
@ -18,47 +18,57 @@ static int f_pcre_compile(lua_State *L) {
|
||||||
PCRE2_SIZE errorOffset;
|
PCRE2_SIZE errorOffset;
|
||||||
int errorNumber;
|
int errorNumber;
|
||||||
const char* str = luaL_checklstring(L, -1, &len);
|
const char* str = luaL_checklstring(L, -1, &len);
|
||||||
pcre2_code* re = pcre2_compile((PCRE2_SPTR)str, len, 0, &errorNumber, &errorOffset, NULL);
|
pcre2_code* re = pcre2_compile(
|
||||||
|
(PCRE2_SPTR)str,
|
||||||
|
len,
|
||||||
|
0,
|
||||||
|
&errorNumber,
|
||||||
|
&errorOffset,
|
||||||
|
NULL
|
||||||
|
);
|
||||||
if (re) {
|
if (re) {
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
lua_pushlightuserdata(L, re);
|
lua_pushlightuserdata(L, re);
|
||||||
lua_rawseti(L, -2, 1);
|
lua_rawseti(L, -2, 1);
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, "regex");
|
luaL_setmetatable(L, "regex");
|
||||||
lua_setmetatable(L, -2);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
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", (int)errorOffset, buffer);
|
luaL_error(L, "regex compilation failed at offset %d: %s",
|
||||||
|
(int)errorOffset, buffer);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes string, compiled regex, returns list of indices of matched groups (including the whole match), if a match was found.
|
// Takes string, compiled regex, returns list of indices of matched groups
|
||||||
|
// (including the whole match), if a match was found.
|
||||||
static int f_pcre_match(lua_State *L) {
|
static int f_pcre_match(lua_State *L) {
|
||||||
size_t len;
|
size_t len;
|
||||||
const char* str = luaL_checklstring(L, -1, &len);
|
const char* str = luaL_checklstring(L, -1, &len);
|
||||||
luaL_checktype(L, -2, LUA_TTABLE);
|
luaL_checktype(L, -2, LUA_TTABLE);
|
||||||
lua_rawgeti(L, -2, 1);
|
lua_rawgeti(L, -2, 1);
|
||||||
pcre2_code* re = (pcre2_code*)lua_touserdata(L, -1);
|
pcre2_code* re = (pcre2_code*)lua_touserdata(L, -1);
|
||||||
pcre2_match_data* match_data = pcre2_match_data_create_from_pattern(re, NULL);
|
pcre2_match_data* md = pcre2_match_data_create_from_pattern(re, NULL);
|
||||||
int rc = pcre2_match(re, (PCRE2_SPTR)str, len, 0, 0, match_data, NULL);
|
int rc = pcre2_match(re, (PCRE2_SPTR)str, len, 0, 0, md, NULL);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
pcre2_match_data_free(match_data);
|
pcre2_match_data_free(md);
|
||||||
if (rc != PCRE2_ERROR_NOMATCH)
|
if (rc != PCRE2_ERROR_NOMATCH)
|
||||||
luaL_error(L, "regex matching error %d", rc);
|
luaL_error(L, "regex matching error %d", rc);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
PCRE2_SIZE* ovector = pcre2_get_ovector_pointer(match_data);
|
PCRE2_SIZE* ovector = pcre2_get_ovector_pointer(md);
|
||||||
if (ovector[0] > ovector[1]) {
|
if (ovector[0] > ovector[1]) {
|
||||||
/* We must guard against patterns such as /(?=.\K)/ that use \K in an assertion
|
/* We must guard against patterns such as /(?=.\K)/ that use \K in an
|
||||||
to set the start of a match later than its end. In the editor, we just detect this case and give up. */
|
assertion to set the start of a match later than its end. In the editor,
|
||||||
luaL_error(L, "regex matching error: \\K was used in an assertion to set the match start after its end");
|
we just detect this case and give up. */
|
||||||
pcre2_match_data_free(match_data);
|
luaL_error(L, "regex matching error: \\K was used in an assertion to "
|
||||||
|
" set the match start after its end");
|
||||||
|
pcre2_match_data_free(md);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < rc*2; i++)
|
for (int i = 0; i < rc*2; i++)
|
||||||
lua_pushnumber(L, ovector[i]+1);
|
lua_pushnumber(L, ovector[i]+1);
|
||||||
pcre2_match_data_free(match_data);
|
pcre2_match_data_free(md);
|
||||||
return rc*2;
|
return rc*2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue