Skip to content

Commit

Permalink
lib/pcre/UniqueRegex: wrap Compile() options in struct
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Oct 4, 2023
1 parent e5680c7 commit bf9dd24
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
15 changes: 2 additions & 13 deletions src/lib/pcre/UniqueRegex.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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;
}
37 changes: 32 additions & 5 deletions src/lib/pcre/UniqueRegex.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
};
3 changes: 1 addition & 2 deletions src/song/Filter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bf9dd24

Please sign in to comment.