forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlike_matcher.cc
122 lines (103 loc) · 3.05 KB
/
like_matcher.cc
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
121
122
/*
* Copyright 2019-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#include "like_matcher.hh"
#include <boost/regex/icu.hpp>
#include <boost/locale/encoding.hpp>
#include <string>
namespace {
using std::wstring;
/// Processes a new pattern character, extending re with the equivalent regex pattern.
void process_char(wchar_t c, wstring& re, bool& escaping) {
if (c == L'\\' && !escaping) {
escaping = true;
return;
}
switch (c) {
case L'.':
case L'[':
case L'\\':
case L'*':
case L'^':
case L'$':
// These are meant to match verbatim in LIKE, but they'd be special characters in regex --
// must escape them.
re.push_back(L'\\');
re.push_back(c);
break;
case L'_':
case L'%':
if (escaping) {
re.push_back(c);
} else { // LIKE wildcard.
re.push_back(L'.');
if (c == L'%') {
re.push_back(L'*');
}
}
break;
default:
re.push_back(c);
break;
}
escaping = false;
}
/// Returns a regex string matching the given LIKE pattern.
wstring regex_from_pattern(bytes_view pattern) {
if (pattern.empty()) {
return L"^$"; // Like SQL, empty pattern matches only empty text.
}
using namespace boost::locale::conv;
wstring wpattern = utf_to_utf<wchar_t>(pattern.begin(), pattern.end(), stop);
if (wpattern.back() == L'\\') {
// Add an extra backslash, in case that last character is unescaped. (If it is escaped, the
// extra backslash will be ignored.)
wpattern += L'\\';
}
wstring re;
re.reserve(wpattern.size() * 2); // Worst case: every element is a special character and must be escaped.
bool escaping = false;
for (const wchar_t c : wpattern) {
process_char(c, re, escaping);
}
return re;
}
} // anonymous namespace
class like_matcher::impl {
bytes _pattern;
boost::u32regex _re; // Performs pattern matching.
public:
explicit impl(bytes_view pattern);
bool operator()(bytes_view text) const;
void reset(bytes_view pattern);
private:
void init_re() {
_re = boost::make_u32regex(regex_from_pattern(_pattern), boost::u32regex::basic | boost::u32regex::optimize);
}
};
like_matcher::impl::impl(bytes_view pattern) : _pattern(pattern) {
init_re();
}
bool like_matcher::impl::operator()(bytes_view text) const {
return boost::u32regex_match(text.begin(), text.end(), _re);
}
void like_matcher::impl::reset(bytes_view pattern) {
if (pattern != _pattern) {
_pattern = bytes(pattern);
init_re();
}
}
like_matcher::like_matcher(bytes_view pattern)
: _impl(std::make_unique<impl>(pattern)) {
}
like_matcher::~like_matcher() = default;
like_matcher::like_matcher(like_matcher&& that) noexcept = default;
bool like_matcher::operator()(bytes_view text) const {
return _impl->operator()(text);
}
void like_matcher::reset(bytes_view pattern) {
return _impl->reset(pattern);
}