From 46b420f7ee5361819b11dff50e6ffbb038f85696 Mon Sep 17 00:00:00 2001 From: Sofox Date: Tue, 12 Dec 2023 00:07:07 +0000 Subject: [PATCH] Fix for RegEx.sub truncating string when 'end' is used --- modules/regex/regex.cpp | 2 +- modules/regex/tests/test_regex.h | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/regex/regex.cpp b/modules/regex/regex.cpp index d49578d2a9bf..4a1037431a51 100644 --- a/modules/regex/regex.cpp +++ b/modules/regex/regex.cpp @@ -334,7 +334,7 @@ String RegEx::sub(const String &p_subject, const String &p_replacement, bool p_a return String(); } - return String(output.ptr(), olength); + return String(output.ptr(), olength) + p_subject.substr(length); } bool RegEx::is_valid() const { diff --git a/modules/regex/tests/test_regex.h b/modules/regex/tests/test_regex.h index 0b401da831c4..1c305f70f7e0 100644 --- a/modules/regex/tests/test_regex.h +++ b/modules/regex/tests/test_regex.h @@ -133,6 +133,18 @@ TEST_CASE("[RegEx] Substitution") { RegEx re4("(a)(b){0}(c)"); REQUIRE(re4.is_valid()); CHECK(re4.sub(s4, "${1}.${3}.", true) == "a.c.a.c.a.c."); + + const String s5 = "aaaa"; + + RegEx re5("a"); + REQUIRE(re5.is_valid()); + CHECK(re5.sub(s5, "b", true, 0, 2) == "bbaa"); + CHECK(re5.sub(s5, "b", true, 1, 3) == "abba"); + CHECK(re5.sub(s5, "b", true, 0, 0) == "aaaa"); + CHECK(re5.sub(s5, "b", true, 1, 1) == "aaaa"); + CHECK(re5.sub(s5, "cc", true, 0, 2) == "ccccaa"); + CHECK(re5.sub(s5, "cc", true, 1, 3) == "acccca"); + CHECK(re5.sub(s5, "", true, 0, 2) == "aa"); } TEST_CASE("[RegEx] Substitution with empty input and/or replacement") {