From 2524de8da1e9d7e2f387268463af760d8db80bf6 Mon Sep 17 00:00:00 2001 From: ewewukek Date: Tue, 5 Mar 2024 12:15:00 +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 74cb0e7..e6f3b80 100644 --- a/src/stringutils.cpp +++ b/src/stringutils.cpp @@ -711,7 +711,8 @@ bool isMatch(const std::string &str, const std::string &exp) { static std::unordered_map re_cache; - if (re_cache.find(exp) == re_cache.end()) + auto it = re_cache.find(exp); + if (it == re_cache.end()) { jp::Regex re(exp, "x"); if (!re) @@ -720,9 +721,9 @@ bool isMatch(const std::string &str, re.getErrorMessage().c_str(), re.getErrorOffset()); exit(1); } - re_cache[exp] = re; + it = re_cache.insert({ exp, re }).first; } - return re_cache[exp].match(str); + return it->second.match(str); } bool isMatch(const std::string &str, @@ -730,7 +731,8 @@ bool isMatch(const std::string &str, jp::VecNum &m) { static std::unordered_map re_cache; - if (re_cache.find(exp) == re_cache.end()) + auto it = re_cache.find(exp); + if (it == re_cache.end()) { jp::Regex re(exp, "x"); if (!re) @@ -739,10 +741,10 @@ bool isMatch(const std::string &str, re.getErrorMessage().c_str(), re.getErrorOffset()); exit(1); } - re_cache[exp] = re; + it = re_cache.insert({ exp, re }).first; } jp::RegexMatch rm; - size_t count = rm.setRegexObject(&re_cache[exp]) + size_t count = rm.setRegexObject(&it->second) .setSubject(&str) .setNumberedSubstringVector(&m) .match(); -- cgit v1.2.3-60-g2f50