diff options
-rw-r--r-- | src/stringutils.cpp | 35 |
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(); |