regex_engine.h 544 B

1234567891011121314151617181920212223
  1. #pragma once
  2. #include <regex>
  3. #include <string>
  4. #include <unordered_map>
  5. namespace jvalidate::detail {
  6. class RegexEngine {
  7. public:
  8. virtual ~RegexEngine() = default;
  9. virtual bool operator()(std::string const & regex, std::string const & text) = 0;
  10. };
  11. class StdRegexEngine final : public RegexEngine {
  12. private:
  13. std::unordered_map<std::string, std::regex> cache_;
  14. public:
  15. bool operator()(std::string const & regex, std::string const & text) {
  16. return std::regex_match(text, cache_.try_emplace(regex, regex).first->second);
  17. }
  18. };
  19. }