From 9d63d3368f9a0546078f6e7125040d184e402af4 Mon Sep 17 00:00:00 2001 From: Tyler Karaszewski Date: Mon, 29 Jul 2024 13:52:50 -0700 Subject: [PATCH 1/2] Add new test and remove warning line for sucessful matches --- libstuff/libstuff.cpp | 2 +- test/tests/LibStuffTest.cpp | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/libstuff/libstuff.cpp b/libstuff/libstuff.cpp index 6345b4094..2e990b60e 100644 --- a/libstuff/libstuff.cpp +++ b/libstuff/libstuff.cpp @@ -2865,7 +2865,7 @@ string SREReplace(const string& regExp, const string& input, const string& repla if (i == 0 && result == PCRE2_ERROR_NOMEMORY) { // This is the expected case on the first run, there's not enough space to store the result, so we allocate the space and do it again. output = (char*)malloc(outSize); - } else if (result) { + } else if (result < 0) { SWARN("Regex replacement failed with result " << result << ", returning nothing."); break; } diff --git a/test/tests/LibStuffTest.cpp b/test/tests/LibStuffTest.cpp index 9c3e85a96..816872762 100644 --- a/test/tests/LibStuffTest.cpp +++ b/test/tests/LibStuffTest.cpp @@ -688,6 +688,11 @@ struct LibStuff : tpunit::TestFixture { // And test case sensitivity (enabled) string result3 = SREReplace("CAT", from, "dinosaur", false); ASSERT_EQUAL(result3, expected); + + // Test match groups. + string from2 = "a cat did something to a dog"; + string result4 = SREReplace("cat(.*)dog", from2, "chicken$1horse"); + ASSERT_EQUAL(result4, "a chicken did something to a horse"); } void SQResultTest() { From d62aeb30fdf368e7f502f2771b23f5d80a7ca72d Mon Sep 17 00:00:00 2001 From: Tyler Karaszewski Date: Mon, 29 Jul 2024 13:55:41 -0700 Subject: [PATCH 2/2] Return an empty string, not null --- libstuff/libstuff.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libstuff/libstuff.cpp b/libstuff/libstuff.cpp index 2e990b60e..6f2e7fb6b 100644 --- a/libstuff/libstuff.cpp +++ b/libstuff/libstuff.cpp @@ -2867,6 +2867,8 @@ string SREReplace(const string& regExp, const string& input, const string& repla output = (char*)malloc(outSize); } else if (result < 0) { SWARN("Regex replacement failed with result " << result << ", returning nothing."); + output = (char*)malloc(1); + *output = 0; break; } }