Merge pull request #42 from ChemiCalChems/compile-time-regex

Compile time regex (related to issue #37)
This commit is contained in:
liuzicheng1987
2023-12-28 21:50:21 +01:00
committed by GitHub
4 changed files with 5805 additions and 38 deletions
+2
View File
@@ -512,3 +512,5 @@ reflect-cpp has been developed by [scaleML](https://www.scaleml.de), a company s
reflect-cpp is released under the MIT License. Refer to the LICENSE file for details.
reflect-cpp includes [YYJSON](https://github.com/ibireme/yyjson), the fastest JSON library currently in existence. YYJSON is written by YaoYuan and also released under the MIT License.
reflect-cpp includes [compile-time-regular-expressions](https://github.com/hanickadot/compile-time-regular-expressions). CTRE is written by Hana Dusíková and released under the Apache-2.0 License with LLVM exceptions.
+5797
View File
File diff suppressed because it is too large Load Diff
+5 -37
View File
@@ -1,16 +1,10 @@
#ifndef RFL_PATTERNVALIDATOR_HPP_
#define RFL_PATTERNVALIDATOR_HPP_
#include <ctime>
#include <iterator>
#include <optional>
#include <regex>
#include <stdexcept>
#include <string>
#include "Literal.hpp"
#include "../ctre.hpp"
#include "Result.hpp"
#include "internal/Memoization.hpp"
#include "internal/StringLiteral.hpp"
namespace rfl {
@@ -18,38 +12,12 @@ namespace rfl {
template <internal::StringLiteral _regex, internal::StringLiteral _name>
struct PatternValidator {
static Result<std::string> validate(const std::string& _str) noexcept {
const auto check_pattern =
[&](const auto _regex_pattern) -> rfl::Result<std::string> {
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<const std::regex*> regex_pattern() noexcept {
const auto compile_pattern = [](std::optional<std::regex>* _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<std::optional<std::regex>> regex_pattern_;
};
} // namespace rfl
+1 -1
View File
@@ -15,7 +15,7 @@ using Base64Encoded = Pattern<R"(^[A-Za-z0-9+/]*(={0,2})?$)", "Base64Encoded">;
/// This pattern will match email addresses.
using Email =
Pattern<R"(^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$)", "Email">;
Pattern<R"(^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$)", "Email">;
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}$)",