[pcrepp] handle defined capture groups

Fixes #1013
This commit is contained in:
Tim Stack 2022-08-02 13:38:43 -07:00
parent 3cafcf3c77
commit 363eb35da3
2 changed files with 28 additions and 1 deletions

View File

@ -144,6 +144,9 @@ pcrepp::find_captures(const char* pattern)
break;
case ')': {
if (!cap_in_progress.empty()) {
static const auto DEFINE_SF
= string_fragment::from_const("(?(DEFINE)");
auto& cap = cap_in_progress.back();
char first = '\0', second = '\0', third = '\0';
bool is_cap = false;
@ -158,6 +161,14 @@ pcrepp::find_captures(const char* pattern)
if (cap.length() >= 4) {
third = pattern[cap.c_begin + 3];
}
if (cap.c_begin >= 2) {
auto poss_define = string_fragment::from_byte_range(
pattern, cap.c_begin - 2, cap.c_end);
if (poss_define == DEFINE_SF) {
cap_in_progress.pop_back();
continue;
}
}
if (first == '?') {
if (second == '\'') {
is_cap = true;

View File

@ -42,6 +42,21 @@ main(int argc, char* argv[])
pcre_context_static<30> context;
int retval = EXIT_SUCCESS;
{
pcrepp ipmatcher(
R"((?(DEFINE)(?<byte>2[0-4]\d|25[0-5]|1\d\d|[1-9]?\d))\b(?&byte)(\.(?&byte)){3}\b)");
pcre_input pi("192.168.1.1");
assert(ipmatcher.match(context, pi));
assert(context.all()->c_begin == 0);
}
{
pcrepp ipmatcher(R"((DEFINE))");
assert(ipmatcher.get_capture_count() == 1);
}
{
pcrepp nomatch("nothing-to-match");
pcre_input pi("dummy");
@ -85,7 +100,8 @@ main(int argc, char* argv[])
int index = 0;
for (iter = match3.named_begin(); iter != match3.named_end();
++iter, index++) {
++iter, index++)
{
assert(strcmp(iter->pnc_name, expected_names[index]) == 0);
}