From f89913f1a3f6513618519bd0a10af889e2c88caa Mon Sep 17 00:00:00 2001 From: Txema Ynzenga Date: Thu, 28 Dec 2023 13:46:36 +0100 Subject: [PATCH] Use ctre::match in rfl/PatternValidator.hpp instead of std::regex --- include/rfl/PatternValidator.hpp | 42 +++--------------------- include/rfl/parsing/NamedTupleParser.hpp | 1 + include/rfl/patterns.hpp | 2 +- 3 files changed, 7 insertions(+), 38 deletions(-) diff --git a/include/rfl/PatternValidator.hpp b/include/rfl/PatternValidator.hpp index 6b6fbb36..a5487299 100644 --- a/include/rfl/PatternValidator.hpp +++ b/include/rfl/PatternValidator.hpp @@ -1,16 +1,10 @@ #ifndef RFL_PATTERNVALIDATOR_HPP_ #define RFL_PATTERNVALIDATOR_HPP_ -#include -#include -#include -#include -#include #include -#include "rfl/Literal.hpp" +#include "../ctre.hpp" #include "rfl/Result.hpp" -#include "rfl/internal/Memoization.hpp" #include "rfl/internal/StringLiteral.hpp" namespace rfl { @@ -18,38 +12,12 @@ namespace rfl { template struct PatternValidator { static Result validate(const std::string& _str) noexcept { - const auto check_pattern = - [&](const auto _regex_pattern) -> rfl::Result { - if (!std::regex_match(_str, *_regex_pattern)) { - return rfl::Error("String '" + _str + "' did not match format '" + - _name.str() + "': '" + _regex.str() + "'."); - } + if (ctre::match<_regex.arr_>(_str)) return _str; - }; - - return regex_pattern().and_then(check_pattern); + else + return rfl::Error("String '" + _str + "' did not match format '" + + _name.str() + "': '" + _regex.str() + "'."); } - - private: - /// Uses a memoization pattern to init the pattern. - static rfl::Result regex_pattern() noexcept { - const auto compile_pattern = [](std::optional* _regex_pattern) { - try { - *_regex_pattern = std::regex(_regex.str()); - } catch (std::exception& e) { - return; - } - }; - const auto& r = regex_pattern_.value(compile_pattern); - if (!r) { - return Error("Regex pattern '" + _regex.str() + "' failed to compile."); - } - return &(*r); - } - - private: - /// The underlying regex pattern. - static inline internal::Memoization> regex_pattern_; }; } // namespace rfl diff --git a/include/rfl/parsing/NamedTupleParser.hpp b/include/rfl/parsing/NamedTupleParser.hpp index ca0f4c4b..4d4b47d2 100644 --- a/include/rfl/parsing/NamedTupleParser.hpp +++ b/include/rfl/parsing/NamedTupleParser.hpp @@ -11,6 +11,7 @@ #include "rfl/internal/is_basic_type.hpp" #include "rfl/internal/strings/replace_all.hpp" #include "rfl/parsing/is_required.hpp" +#include "../internal/Memoization.hpp" namespace rfl { namespace parsing { diff --git a/include/rfl/patterns.hpp b/include/rfl/patterns.hpp index 8dcbff1b..edfa0174 100644 --- a/include/rfl/patterns.hpp +++ b/include/rfl/patterns.hpp @@ -15,7 +15,7 @@ using Base64Encoded = Pattern; /// This pattern will match email addresses. using Email = - Pattern; + Pattern; using UUIDv1 = Pattern< R"(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-1[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)",