| 36 | } // namespace |
| 37 | |
| 38 | Regex::Regex(const std::string_view pattern) { |
| 39 | int errorCode = 0; |
| 40 | std::size_t errorOffset = 0; |
| 41 | |
| 42 | static_assert(sizeof(std::string_view::value_type) == sizeof(std::remove_pointer_t<PCRE2_SPTR8>), |
| 43 | "PCRE2 char type size mismatch"); |
| 44 | |
| 45 | code_ = pcre2_compile( |
| 46 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 47 | reinterpret_cast<PCRE2_SPTR8>(pattern.data()), pattern.size(), |
| 48 | PCRE2_CASELESS | PCRE2_DOLLAR_ENDONLY | |
| 49 | PCRE2_DOTALL, /* case-insensitive; `$` matches end of string; `.` matches newlines */ |
| 50 | &errorCode, /* error number */ |
| 51 | &errorOffset, /* error offset */ |
| 52 | nullptr); /* use default compile context */ |
| 53 | |
| 54 | if (code_ == nullptr) { |
| 55 | throw errors::InputValidationException{fmt::format( |
| 56 | "Error {} compiling regex '{}': {}", errorCode, pattern, errorCodeToMessage(errorCode))}; |
| 57 | } |
| 58 | |
| 59 | // Extra performance JIT compile. We don't care about partial |
| 60 | // matches. |
| 61 | errorCode = pcre2_jit_compile(code_, PCRE2_JIT_COMPLETE); |
| 62 | |
| 63 | if (errorCode != 0) { |
| 64 | throw errors::InputValidationException{fmt::format( |
| 65 | "Error {} JIT compiling '{}': {}", errorCode, pattern, errorCodeToMessage(errorCode))}; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | Regex::~Regex() { pcre2_code_free(code_); } |
| 70 |
nothing calls this directly
no test coverage detected