From bf9dd24536aa9761a9b27e5a9cb0b791e1b2da82 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 4 Oct 2023 17:30:28 +0200 Subject: [PATCH] lib/pcre/UniqueRegex: wrap Compile() options in struct --- src/lib/pcre/UniqueRegex.cxx | 15 ++------------- src/lib/pcre/UniqueRegex.hxx | 37 +++++++++++++++++++++++++++++++----- src/song/Filter.cxx | 3 +-- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/lib/pcre/UniqueRegex.cxx b/src/lib/pcre/UniqueRegex.cxx index 48e4ceb3b..386ddc58d 100644 --- a/src/lib/pcre/UniqueRegex.cxx +++ b/src/lib/pcre/UniqueRegex.cxx @@ -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; } diff --git a/src/lib/pcre/UniqueRegex.hxx b/src/lib/pcre/UniqueRegex.hxx index 927eb022d..af2b22674 100644 --- a/src/lib/pcre/UniqueRegex.hxx +++ b/src/lib/pcre/UniqueRegex.hxx @@ -8,13 +8,37 @@ #include +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); + } }; diff --git a/src/song/Filter.cxx b/src/song/Filter.cxx index 869fdf7c3..29ef2beec 100644 --- a/src/song/Filter.cxx +++ b/src/song/Filter.cxx @@ -247,8 +247,7 @@ ParseStringFilter(const char *&s, bool fold_case) negated, }; f.SetRegex(std::make_shared(f.GetValue().c_str(), - false, false, - fold_case)); + Pcre::CompileOptions{.caseless=fold_case})); return f; } #endif