Skip to content

Commit

Permalink
Allow partial match option
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerkaraszewski committed Jul 25, 2024
1 parent 9a52942 commit b2c7ecd
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions libstuff/libstuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2805,13 +2805,13 @@ bool SIsValidSQLiteDateModifier(const string& modifier) {
return true;
}

bool SREMatch(const string& regExp, const string& input, bool caseSensitive) {
bool SREMatch(const string& regExp, const string& input, bool caseSensitive, bool partialMatch) {
int errornumber = 0;
PCRE2_SIZE erroroffset = 0;
uint32_t matchFlags = 0;

// These require full-string matches as that's the historical way this function works.
uint32_t compileFlags = PCRE2_ANCHORED | PCRE2_ENDANCHORED;
uint32_t compileFlags = partialMatch ? 0 : PCRE2_ANCHORED | PCRE2_ENDANCHORED;
if (!caseSensitive) {
compileFlags |= PCRE2_CASELESS;
}
Expand Down
4 changes: 2 additions & 2 deletions libstuff/libstuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,8 @@ bool SEndsWith(const string& haystack, const string& needle);
bool SConstantTimeEquals(const string& secret, const string& userInput);
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& input, bool caseSensitive = true);
// Unless `partialMatch` is specified, perform a full regex match (the '^' and '$' symbols are implicit).
bool SREMatch(const string& regExp, const string& input, bool caseSensitive = true, bool partialMatch = false);
string SREReplace(const string& regExp, const string& input, const string& replacement, bool caseSensitive = true);

// Redact values that should not be logged.
Expand Down
3 changes: 3 additions & 0 deletions test/tests/LibStuffTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,9 @@ struct LibStuff : tpunit::TestFixture {

// Partial matches aren't counted.
ASSERT_FALSE(SREMatch("cat", "this contains cat"));

// Now try with partial specified, should work.
ASSERT_TRUE(SREMatch("cat", "this contains cat", true, true));
}

void SREReplaceTest() {
Expand Down

0 comments on commit b2c7ecd

Please sign in to comment.