-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathRegex.cpp
More file actions
276 lines (216 loc) · 9.24 KB
/
Regex.cpp
File metadata and controls
276 lines (216 loc) · 9.24 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "Public/winget/Regex.h"
#include "Public/AppInstallerErrors.h"
#include "Public/AppInstallerLogging.h"
#include "Public/AppInstallerLanguageUtilities.h"
#define WINGET_THROW_REGEX_ERROR_IF_FAILED(_err_,_func_) \
if (U_FAILURE(_err_)) \
{ \
AICLI_LOG(Core, Error, << #_func_ " returned " << _err_); \
THROW_HR(APPINSTALLER_CLI_ERROR_ICU_REGEX_ERROR); \
}
namespace AppInstaller::Regex
{
struct Expression::impl
{
using uregex_ptr = wil::unique_any<URegularExpression*, decltype(uregex_close), uregex_close>;
using utext_ptr = wil::unique_any<UText*, decltype(utext_close), utext_close>;
// Create caches the original ICU regex objects in a static map and hands out copies of them
// when requested. Since we have a limited set, this is a very simple cache-all-forever pattern.
static std::unique_ptr<impl> Create(std::string_view pattern, Options options)
{
struct key
{
std::string pattern;
Options options = Options::None;
bool operator<(const key& other) const
{
if (pattern < other.pattern)
{
return true;
}
else if (pattern == other.pattern)
{
return ToIntegral(options) < ToIntegral(other.options);
}
else
{
return false;
}
}
};
struct statics
{
std::map<key, impl> map;
wil::srwlock lock;
};
static statics s_regex_cache;
key requested;
requested.pattern = pattern;
requested.options = options;
{
// Attempt to find in the cache
auto sharedLock = s_regex_cache.lock.lock_shared();
auto itr = s_regex_cache.map.find(requested);
if (itr != s_regex_cache.map.end())
{
return std::make_unique<impl>(itr->second);
}
}
auto exclusiveLock = s_regex_cache.lock.lock_exclusive();
// Check if another thread created it while we waited for the lock.
auto itr = s_regex_cache.map.find(requested);
if (itr != s_regex_cache.map.end())
{
return std::make_unique<impl>(itr->second);
}
else
{
return std::make_unique<impl>(s_regex_cache.map.emplace(std::move(requested), impl{ pattern, options }).first->second);
}
}
impl(std::string_view pattern, Options options)
{
UErrorCode uec = U_ZERO_ERROR;
utext_ptr patternUtext{ utext_openUTF8(nullptr, pattern.data(), pattern.length(), &uec) };
WINGET_THROW_REGEX_ERROR_IF_FAILED(uec, utext_openUTF8);
// For now, just handle the one option
uint32_t flags = 0;
if (options == Options::CaseInsensitive)
{
flags = UREGEX_CASE_INSENSITIVE;
}
UParseError parseError{};
m_regex.reset(uregex_openUText(patternUtext.get(), flags, &parseError, &uec));
if (U_FAILURE(uec))
{
AICLI_LOG(Core, Error, << "uregex_openUText failed with error [" << uec << "] at line " << parseError.line << ", position " << parseError.offset << '\n' << pattern);
THROW_HR(APPINSTALLER_CLI_ERROR_ICU_REGEX_ERROR);
}
}
impl(const impl& other)
{
UErrorCode uec = U_ZERO_ERROR;
m_regex.reset(uregex_clone(other.m_regex.get(), &uec));
WINGET_THROW_REGEX_ERROR_IF_FAILED(uec, uregex_clone);
}
impl& operator=(const impl& other)
{
*this = impl{ other };
return *this;
}
impl(impl&&) = default;
impl& operator=(impl&&) = default;
~impl() = default;
bool IsMatch(std::wstring_view input) const
{
UErrorCode uec = U_ZERO_ERROR;
SetText(input);
UBool result = uregex_matches(m_regex.get(), -1, &uec);
WINGET_THROW_REGEX_ERROR_IF_FAILED(uec, uregex_matches);
return !!result;
}
std::wstring Replace(std::wstring_view input, std::wstring_view replacement) const
{
UErrorCode uec = U_ZERO_ERROR;
SetText(input);
std::u16string_view u16replacement = Convert(replacement);
utext_ptr replacementUtext{ utext_openUChars(nullptr, u16replacement.data(), u16replacement.length(), &uec) };
WINGET_THROW_REGEX_ERROR_IF_FAILED(uec, utext_openUTF8);
utext_ptr resultUText{ uregex_replaceAllUText(m_regex.get(), replacementUtext.get(), nullptr, &uec) };
WINGET_THROW_REGEX_ERROR_IF_FAILED(uec, uregex_replaceAllUText);
int64_t cch = utext_nativeLength(resultUText.get());
std::wstring result(static_cast<size_t>(cch), '\0');
utext_extract(resultUText.get(), 0, std::numeric_limits<int64_t>::max(), reinterpret_cast<char16_t*>(&result[0]), static_cast<int32_t>(result.size()), &uec);
WINGET_THROW_REGEX_ERROR_IF_FAILED(uec, utext_extract);
return result;
}
void ForEach(std::wstring_view input, const std::function<bool(bool, std::wstring_view)>&f) const
{
UErrorCode uec = U_ZERO_ERROR;
SetText(input);
int32_t startPos = 0;
while (uregex_findNext(m_regex.get(), &uec))
{
WINGET_THROW_REGEX_ERROR_IF_FAILED(uec, uregex_findNext);
int32_t pos = uregex_start(m_regex.get(), 0, &uec);
WINGET_THROW_REGEX_ERROR_IF_FAILED(uec, uregex_start);
THROW_HR_IF(E_UNEXPECTED, pos == -1);
// First, send off the unmatched part before the match
if (pos > startPos)
{
if (!f(false, input.substr(startPos, static_cast<size_t>(pos) - startPos)))
{
return;
}
}
// Now send the matched part
int32_t end = uregex_end(m_regex.get(), 0, &uec);
WINGET_THROW_REGEX_ERROR_IF_FAILED(uec, uregex_end);
THROW_HR_IF(E_UNEXPECTED, end == -1);
if (!f(true, input.substr(pos, static_cast<size_t>(end) - pos)))
{
return;
}
startPos = end;
}
WINGET_THROW_REGEX_ERROR_IF_FAILED(uec, uregex_findNext);
// Finally, send any remaining part
if (input.length() > static_cast<size_t>(startPos))
{
f(false, input.substr(startPos));
}
}
private:
static std::u16string_view Convert(std::wstring_view input)
{
static_assert(sizeof(wchar_t) == sizeof(char16_t), "wchar_t and char16_t must be the same size");
return { reinterpret_cast<const char16_t*>(input.data()), input.size() };
}
void SetText(std::wstring_view input) const
{
UErrorCode uec = U_ZERO_ERROR;
std::u16string_view u16 = Convert(input);
uregex_setText(m_regex.get(), u16.data(), static_cast<int32_t>(u16.length()), &uec);
WINGET_THROW_REGEX_ERROR_IF_FAILED(uec, uregex_setText);
}
uregex_ptr m_regex;
};
Expression::Expression() = default;
Expression::Expression(std::string_view pattern, Options options) : pImpl(impl::Create(pattern, options)) {}
Expression::Expression(const Expression& other)
{
if (other.pImpl)
{
pImpl = std::make_unique<impl>(*other.pImpl);
}
}
Expression& Expression::operator=(const Expression& other)
{
return *this = Expression{ other };
}
Expression::Expression(Expression&&) noexcept = default;
Expression& Expression::operator=(Expression&&) noexcept = default;
Expression::~Expression() = default;
Expression::operator bool() const
{
return static_cast<bool>(pImpl);
}
bool Expression::IsMatch(std::wstring_view input) const
{
THROW_HR_IF(E_NOT_VALID_STATE, !pImpl);
return pImpl->IsMatch(input);
}
std::wstring Expression::Replace(std::wstring_view input, std::wstring_view replacement) const
{
THROW_HR_IF(E_NOT_VALID_STATE, !pImpl);
return pImpl->Replace(input, replacement);
}
void Expression::ForEach(std::wstring_view input, const std::function<bool(bool, std::wstring_view)>& f) const
{
THROW_HR_IF(E_NOT_VALID_STATE, !pImpl);
return pImpl->ForEach(input, f);
}
}