diff options
author | ewewukek <ewewukek@gmail.com> | 2024-03-05 12:15:00 +0300 |
---|---|---|
committer | ewewukek <ewewukek@gmail.com> | 2024-03-05 12:15:00 +0300 |
commit | 2524de8da1e9d7e2f387268463af760d8db80bf6 (patch) | |
tree | 5610a5380cd5cb0ee570c03c6293f6e515d9f8b0 | |
parent | 50106619871195532609cf89b0794744aca151d7 (diff) | |
download | mplint-pcre2.tar.gz mplint-pcre2.tar.bz2 mplint-pcre2.tar.xz mplint-pcre2.zip |
Use iterator returned by find() to avoid second searchpcre2
-rw-r--r-- | src/stringutils.cpp | 14 |
1 files 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<std::string, jp::Regex> 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<std::string, jp::Regex> 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(); |