From 382db0898e36c0e5b92d63126c87c71389a9a00f Mon Sep 17 00:00:00 2001 From: Zher Huei Lee Date: Mon, 26 Jun 2017 15:16:33 +0800 Subject: Fixes RegEx capture grabbing too much #9382 Incorrect behaviour was caused when next->test was throwing off the results. --- modules/regex/regex.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/modules/regex/regex.cpp b/modules/regex/regex.cpp index eb9f1d2ab1..c095291aea 100644 --- a/modules/regex/regex.cpp +++ b/modules/regex/regex.cpp @@ -590,6 +590,11 @@ struct RegExNodeGroup : public RegExNode { memdelete(childset[i]); } + virtual void test_success(RegExSearch &s, int pos) const { + + return; + } + virtual int test(RegExSearch &s, int pos) const { for (int i = 0; i < childset.size(); ++i) { @@ -614,6 +619,7 @@ struct RegExNodeGroup : public RegExNode { if (res >= 0) { if (reset_pos) res = pos; + this->test_success(s, res); return next ? next->test(s, res) : res; } } @@ -668,6 +674,12 @@ struct RegExNodeCapturing : public RegExNodeGroup { id = p_id; } + virtual void test_success(RegExSearch &s, int pos) const { + + RegExMatch::Group &ref = s.match->captures[id]; + ref.length = pos - ref.start; + } + virtual int test(RegExSearch &s, int pos) const { RegExMatch::Group &ref = s.match->captures[id]; @@ -676,13 +688,8 @@ struct RegExNodeCapturing : public RegExNodeGroup { int res = RegExNodeGroup::test(s, pos); - if (res >= 0) { - if (!s.complete) - ref.length = res - pos; - } else { + if (res < 0) ref.start = old_start; - } - return res; } -- cgit v1.2.3 From f01ce3276e1d5ade07107d25ddab04117ad40be8 Mon Sep 17 00:00:00 2001 From: Zher Huei Lee Date: Mon, 26 Jun 2017 15:25:10 +0800 Subject: Fixed inverted group thrown off by quantifiers --- modules/regex/regex.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/modules/regex/regex.cpp b/modules/regex/regex.cpp index c095291aea..c728657d6b 100644 --- a/modules/regex/regex.cpp +++ b/modules/regex/regex.cpp @@ -603,10 +603,8 @@ struct RegExNodeGroup : public RegExNode { int res = childset[i]->test(s, pos); - if (s.complete) - return res; - if (inverse) { + s.complete = false; if (res < 0) res = pos + 1; else @@ -616,6 +614,9 @@ struct RegExNodeGroup : public RegExNode { continue; } + if (s.complete) + return res; + if (res >= 0) { if (reset_pos) res = pos; -- cgit v1.2.3