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.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)
|
||||
end
|
||||
|
||||
-- Build off matching. For now, only support basic replacements, but capture groupings should be doable.
|
||||
-- We can even have custom group replacements and transformations and stuff in lua.
|
||||
-- Build off matching. For now, only support basic replacements, but capture
|
||||
-- groupings should be doable. We can even have custom group replacements and
|
||||
-- transformations and stuff in lua.
|
||||
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
|
||||
repeat
|
||||
str = str:sub(offset)
|
||||
|
|
|
@ -18,47 +18,57 @@ static int f_pcre_compile(lua_State *L) {
|
|||
PCRE2_SIZE errorOffset;
|
||||
int errorNumber;
|
||||
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) {
|
||||
lua_newtable(L);
|
||||
lua_pushlightuserdata(L, re);
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "regex");
|
||||
lua_setmetatable(L, -2);
|
||||
luaL_setmetatable(L, "regex");
|
||||
return 1;
|
||||
}
|
||||
PCRE2_UCHAR buffer[256];
|
||||
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;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
size_t len;
|
||||
const char* str = luaL_checklstring(L, -1, &len);
|
||||
luaL_checktype(L, -2, LUA_TTABLE);
|
||||
lua_rawgeti(L, -2, 1);
|
||||
pcre2_code* re = (pcre2_code*)lua_touserdata(L, -1);
|
||||
pcre2_match_data* match_data = pcre2_match_data_create_from_pattern(re, NULL);
|
||||
int rc = pcre2_match(re, (PCRE2_SPTR)str, len, 0, 0, match_data, NULL);
|
||||
pcre2_match_data* md = pcre2_match_data_create_from_pattern(re, NULL);
|
||||
int rc = pcre2_match(re, (PCRE2_SPTR)str, len, 0, 0, md, NULL);
|
||||
if (rc < 0) {
|
||||
pcre2_match_data_free(match_data);
|
||||
pcre2_match_data_free(md);
|
||||
if (rc != PCRE2_ERROR_NOMATCH)
|
||||
luaL_error(L, "regex matching error %d", rc);
|
||||
return 0;
|
||||
}
|
||||
PCRE2_SIZE* ovector = pcre2_get_ovector_pointer(match_data);
|
||||
PCRE2_SIZE* ovector = pcre2_get_ovector_pointer(md);
|
||||
if (ovector[0] > ovector[1]) {
|
||||
/* We must guard against patterns such as /(?=.\K)/ that use \K in an assertion
|
||||
to set the start of a match later than its end. In the editor, we just detect this case and give up. */
|
||||
luaL_error(L, "regex matching error: \\K was used in an assertion to set the match start after its end");
|
||||
pcre2_match_data_free(match_data);
|
||||
/* We must guard against patterns such as /(?=.\K)/ that use \K in an
|
||||
assertion to set the start of a match later than its end. In the editor,
|
||||
we just detect this case and give up. */
|
||||
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;
|
||||
}
|
||||
for (int i = 0; i < rc*2; i++)
|
||||
lua_pushnumber(L, ovector[i]+1);
|
||||
pcre2_match_data_free(match_data);
|
||||
pcre2_match_data_free(md);
|
||||
return rc*2;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue