forked from MusicPlayerDaemon/MPD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringFilter.hxx
120 lines (92 loc) · 2.07 KB
/
StringFilter.hxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
#ifndef MPD_STRING_FILTER_HXX
#define MPD_STRING_FILTER_HXX
#include "lib/icu/Compare.hxx"
#include "config.h"
#ifdef HAVE_PCRE
#include "lib/pcre/UniqueRegex.hxx"
#endif
#include <cstdint>
#include <string>
#include <memory>
class StringFilter {
public:
enum class Position : uint_least8_t {
/** compare the whole haystack */
FULL,
/** find the phrase anywhere in the haystack */
ANYWHERE,
/** check if the haystack starts with the given prefix */
PREFIX,
};
private:
std::string value;
/**
* This value is only set if case folding is enabled.
*/
IcuCompare fold_case;
#ifdef HAVE_PCRE
std::shared_ptr<UniqueRegex> regex;
#endif
Position position;
bool negated;
public:
template<typename V>
StringFilter(V &&_value, bool _fold_case, Position _position, bool _negated)
:value(std::forward<V>(_value)),
fold_case(_fold_case
? IcuCompare(value)
: IcuCompare()),
position(_position),
negated(_negated) {}
bool empty() const noexcept {
return value.empty();
}
bool IsRegex() const noexcept {
#ifdef HAVE_PCRE
return !!regex;
#else
return false;
#endif
}
#ifdef HAVE_PCRE
template<typename R>
void SetRegex(R &&_regex) noexcept {
regex = std::forward<R>(_regex);
}
#endif
const auto &GetValue() const noexcept {
return value;
}
bool GetFoldCase() const noexcept {
return fold_case;
}
bool IsNegated() const noexcept {
return negated;
}
void ToggleNegated() noexcept {
negated = !negated;
}
const char *GetOperator() const noexcept {
if (IsRegex())
return negated ? "!~" : "=~";
switch (position) {
case Position::FULL:
break;
case Position::ANYWHERE:
return negated ? "!contains" : "contains";
case Position::PREFIX:
return negated ? "!starts_with" : "starts_with";
}
return negated ? "!=" : "==";
}
[[gnu::pure]]
bool Match(const char *s) const noexcept;
/**
* Like Match(), but ignore the "negated" flag.
*/
[[gnu::pure]]
bool MatchWithoutNegation(const char *s) const noexcept;
};
#endif