Skip to content

Commit

Permalink
feat: Do not use regex for simple pattern-replacements to improve per…
Browse files Browse the repository at this point in the history
…formance (#252)
  • Loading branch information
cbaecker authored Dec 10, 2024
1 parent 99509ea commit 680401e
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions casbin/util/built_in_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,24 @@ namespace {
static const std::regex capturingColonNonSlashRegex("(.*?):[^/]+(.*?)");
static const std::regex enclosedPlaceHolderRegex("(.*?)\\{[^/]+?\\}(.*?)");

std::string ReplacePattern(std::string const& in, std::string const& pattern, std::string const& replacement) {
std::string result = in;
std::size_t pos = 0;
// Replace all occurrences of pattern with replacement
while ((pos = result.find(pattern, pos)) != std::string::npos) {
result.replace(pos, pattern.length(), replacement);
pos += replacement.length(); // Move over change
}
return result;
}

std::string PrepareWildCardMatching(const std::string& value) {
static const std::regex pattern("/\\*");
return std::regex_replace(value, pattern, "/.*");
return ReplacePattern(value, "/*", "/.*");
}

std::string EscapeCurlyBraces(const std::string& value) {
static const std::regex curlyBraceOpenPattern("\\{");
static const std::regex curlyBraceClosePattern("\\}");

std::string intermediate = std::regex_replace(value, curlyBraceOpenPattern, "\\{");
return std::regex_replace(intermediate, curlyBraceClosePattern, "\\}");
std::string intermediate = ReplacePattern(value, "{", "\\{");
return ReplacePattern(intermediate, "}", "\\}");
}
}

Expand Down

0 comments on commit 680401e

Please sign in to comment.