From 6808f70304a03e267f69346f0133fa7061ca9039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carsten=20B=C3=A4cker?= <72093439+cbaecker@users.noreply.github.com> Date: Tue, 10 Dec 2024 12:06:05 +0100 Subject: [PATCH] perf: Do not use regex for simple pattern-replacements --- casbin/util/built_in_functions.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/casbin/util/built_in_functions.cpp b/casbin/util/built_in_functions.cpp index 03f2abc2..6ca4cb6b 100644 --- a/casbin/util/built_in_functions.cpp +++ b/casbin/util/built_in_functions.cpp @@ -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, "}", "\\}"); } }