From 2851cf706220be4629c4d9ff6a35ea1f05acae18 Mon Sep 17 00:00:00 2001 From: ewewukek Date: Tue, 5 Mar 2024 12:08:52 +0300 Subject: Use iterator returned by find() to avoid second search --- src/stringutils.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/stringutils.cpp b/src/stringutils.cpp index e7c8b5b..1ad598d 100644 --- a/src/stringutils.cpp +++ b/src/stringutils.cpp @@ -711,9 +711,10 @@ bool isMatch(const std::string &str, const std::string &exp) { static std::unordered_map re_cache; - if (re_cache.find(exp) == re_cache.end()) - re_cache.emplace(exp, exp); - return std::regex_match(str, re_cache[exp]); + auto it = re_cache.find(exp); + if (it == re_cache.end()) + it = re_cache.emplace(exp, exp).first; + return std::regex_match(str, it->second); } bool isMatch(const std::string &str, @@ -721,9 +722,10 @@ bool isMatch(const std::string &str, std::smatch &m) { static std::unordered_map re_cache; - if (re_cache.find(exp) == re_cache.end()) - re_cache.emplace(exp, exp); - return std::regex_match(str, m, re_cache[exp]); + auto it = re_cache.find(exp); + if (it == re_cache.end()) + it = re_cache.emplace(exp, exp).first; + return std::regex_match(str, m, it->second); } bool fileExists(const std::string &name) -- cgit v1.2.3-70-g09d2