Skip to content

Commit

Permalink
Fix for RegEx.sub truncating string when 'end' is used
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSofox committed Feb 20, 2024
1 parent b714563 commit 46b420f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion modules/regex/regex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions modules/regex/tests/test_regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down

0 comments on commit 46b420f

Please sign in to comment.