summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorewewukek <ewewukek@gmail.com>2024-02-25 03:03:24 +0300
committerewewukek <ewewukek@gmail.com>2024-02-25 03:03:24 +0300
commit50106619871195532609cf89b0794744aca151d7 (patch)
treefd21f4b12a7d6d11a9aebaf333bf18bc6f84a954
parent4cb6078e879e13ba91933123a99c9edf553e1249 (diff)
downloadmplint-50106619871195532609cf89b0794744aca151d7.tar.gz
mplint-50106619871195532609cf89b0794744aca151d7.tar.bz2
mplint-50106619871195532609cf89b0794744aca151d7.tar.xz
mplint-50106619871195532609cf89b0794744aca151d7.zip
Implement simple caching for compiled regular expressions
-rw-r--r--src/stringutils.cpp35
1 files changed, 23 insertions, 12 deletions
diff --git a/src/stringutils.cpp b/src/stringutils.cpp
index 4f6ff6a..74cb0e7 100644
--- a/src/stringutils.cpp
+++ b/src/stringutils.cpp
@@ -28,6 +28,7 @@
#include <cstdio>
#include <ctime>
#include <list>
+#include <unordered_map>
#include <sys/stat.h>
@@ -709,29 +710,39 @@ void secureChatCommand(std::string &str)
bool isMatch(const std::string &str,
const std::string &exp)
{
- jp::Regex re(exp, "x");
- if (!re)
+ static std::unordered_map<std::string, jp::Regex> re_cache;
+ if (re_cache.find(exp) == re_cache.end())
{
- printf("Invalid regular expression '%s': %s at offset %d\n", exp.c_str(),
- re.getErrorMessage().c_str(), re.getErrorOffset());
- exit(1);
+ jp::Regex re(exp, "x");
+ if (!re)
+ {
+ printf("Invalid regular expression '%s': %s at offset %d\n", exp.c_str(),
+ re.getErrorMessage().c_str(), re.getErrorOffset());
+ exit(1);
+ }
+ re_cache[exp] = re;
}
- return re.match(str);
+ return re_cache[exp].match(str);
}
bool isMatch(const std::string &str,
const std::string &exp,
jp::VecNum &m)
{
- jp::Regex re(exp, "x");
- if (!re)
+ static std::unordered_map<std::string, jp::Regex> re_cache;
+ if (re_cache.find(exp) == re_cache.end())
{
- printf("Invalid regular expression '%s': %s at offset %d\n", exp.c_str(),
- re.getErrorMessage().c_str(), re.getErrorOffset());
- exit(1);
+ jp::Regex re(exp, "x");
+ if (!re)
+ {
+ printf("Invalid regular expression '%s': %s at offset %d\n", exp.c_str(),
+ re.getErrorMessage().c_str(), re.getErrorOffset());
+ exit(1);
+ }
+ re_cache[exp] = re;
}
jp::RegexMatch rm;
- size_t count = rm.setRegexObject(&re)
+ size_t count = rm.setRegexObject(&re_cache[exp])
.setSubject(&str)
.setNumberedSubstringVector(&m)
.match();