Skip to content

feat: Do not use regex for simple pattern-replacements to improve performance #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading