lib/pcre/UniqueRegex: wrap Compile() options in struct

This commit is contained in:
Max Kellermann 2023-10-04 17:30:28 +02:00
parent e5680c712b
commit bf9dd24536
3 changed files with 35 additions and 20 deletions

View File

@ -7,19 +7,8 @@
#include "lib/fmt/ToBuffer.hxx"
void
UniqueRegex::Compile(const char *pattern, bool anchored, bool capture,
bool caseless)
UniqueRegex::Compile(const char *pattern, const int options)
{
constexpr int default_options = PCRE2_DOTALL|PCRE2_NO_AUTO_CAPTURE;
uint32_t options = default_options;
if (anchored)
options |= PCRE2_ANCHORED;
if (capture)
options &= ~PCRE2_NO_AUTO_CAPTURE;
if (caseless)
options |= PCRE2_CASELESS;
int error_number;
PCRE2_SIZE error_offset;
re = pcre2_compile_8(PCRE2_SPTR8(pattern),
@ -34,7 +23,7 @@ UniqueRegex::Compile(const char *pattern, bool anchored, bool capture,
pcre2_jit_compile_8(re, PCRE2_JIT_COMPLETE);
if (int n; capture &&
if (int n; (options & PCRE2_NO_AUTO_CAPTURE) == 0 &&
pcre2_pattern_info_8(re, PCRE2_INFO_CAPTURECOUNT, &n) == 0)
n_capture = n;
}

View File

@ -8,13 +8,37 @@
#include <utility>
namespace Pcre {
struct CompileOptions {
bool anchored = false;
bool caseless = false;
bool capture = false;
explicit constexpr operator int() const noexcept {
int options = PCRE2_DOTALL|PCRE2_NO_AUTO_CAPTURE;
if (anchored)
options |= PCRE2_ANCHORED;
if (caseless)
options |= PCRE2_CASELESS;
if (capture)
options &= ~PCRE2_NO_AUTO_CAPTURE;
return options;
}
};
} // namespace Pcre
class UniqueRegex : public RegexPointer {
public:
UniqueRegex() = default;
UniqueRegex(const char *pattern, bool anchored, bool capture,
bool caseless) {
Compile(pattern, anchored, capture, caseless);
UniqueRegex(const char *pattern, Pcre::CompileOptions options) {
Compile(pattern, options);
}
UniqueRegex(UniqueRegex &&src) noexcept:RegexPointer(src) {
@ -35,6 +59,9 @@ public:
/**
* Throws Pcre::Error on error.
*/
void Compile(const char *pattern, bool anchored, bool capture,
bool caseless);
void Compile(const char *pattern, int options);
void Compile(const char *pattern, Pcre::CompileOptions options={}) {
Compile(pattern, (int)options);
}
};

View File

@ -247,8 +247,7 @@ ParseStringFilter(const char *&s, bool fold_case)
negated,
};
f.SetRegex(std::make_shared<UniqueRegex>(f.GetValue().c_str(),
false, false,
fold_case));
Pcre::CompileOptions{.caseless=fold_case}));
return f;
}
#endif