Skip to content

Commit

Permalink
Add case-sensitive option
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerkaraszewski committed Jul 25, 2024
1 parent dd9a5ec commit 3b50360
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 3 additions & 3 deletions libstuff/libstuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2809,15 +2809,15 @@ bool SREMatch(const string& regExp, const string& s) {
return pcrecpp::RE(regExp, pcrecpp::RE_Options().set_match_limit_recursion(1000)).FullMatch(s);
}

string SREReplace(const string& regExp, const string& s, const string& r) {
string SREReplace(const string& regExp, const string& input, const string& replacement, bool caseSensitive) {
char* output = nullptr;
size_t outSize = 0;
int errornumber = 0;
PCRE2_SIZE erroroffset = 0;
int flags = PCRE2_SUBSTITUTE_GLOBAL | PCRE2_SUBSTITUTE_EXTENDED | PCRE2_SUBSTITUTE_OVERFLOW_LENGTH;
pcre2_code* re = pcre2_compile((PCRE2_SPTR8)regExp.c_str(), PCRE2_ZERO_TERMINATED, 0, &errornumber, &erroroffset, 0);
pcre2_code* re = pcre2_compile((PCRE2_SPTR8)regExp.c_str(), PCRE2_ZERO_TERMINATED, caseSensitive ? 0 : PCRE2_CASELESS, &errornumber, &erroroffset, 0);
for (int i = 0; i < 2; i++) {
int result = pcre2_substitute(re, (PCRE2_SPTR8)s.c_str(), s.size(), 0, flags, 0, 0, (PCRE2_SPTR8)r.c_str(), r.size(), (PCRE2_UCHAR*)output, &outSize);
int result = pcre2_substitute(re, (PCRE2_SPTR8)input.c_str(), input.size(), 0, flags, 0, 0, (PCRE2_SPTR8)replacement.c_str(), replacement.size(), (PCRE2_UCHAR*)output, &outSize);
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);
Expand Down
2 changes: 1 addition & 1 deletion libstuff/libstuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ bool SConstantTimeIEquals(const string& secret, const string& userInput);

// Perform a full regex match. The '^' and '$' symbols are implicit.
bool SREMatch(const string& regExp, const string& s);
string SREReplace(const string& regExp, const string& s, const string& r);
string SREReplace(const string& regExp, const string& input, const string& replacement, bool caseSensitive = true);

// Redact values that should not be logged.
void SRedactSensitiveValues(string& s);
Expand Down
8 changes: 8 additions & 0 deletions test/tests/LibStuffTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,14 @@ struct LibStuff : tpunit::TestFixture {
string expected = "a dinosaur is not a dog it is a dinosaur";
string result = SREReplace("cat", from, "dinosaur");
ASSERT_EQUAL(result, expected);

// And test case sensitivity (disabled)
string result2 = SREReplace("CAT", from, "dinosaur");
ASSERT_EQUAL(result2, from);

// And test case sensitivity (enabled)
string result3 = SREReplace("CAT", from, "dinosaur", false);
ASSERT_EQUAL(result3, expected);
}

void SQResultTest() {
Expand Down

0 comments on commit 3b50360

Please sign in to comment.