Skip to content

Commit ae3f960

Browse files
committed
(broken) switch to URegularExpression
1 parent 24850c8 commit ae3f960

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

Diff for: src/cascadia/TerminalSettingsModel/MatchProfilesEntry.cpp

+28-2
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,40 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
5353
return !(_invalidName || _invalidCommandline || _invalidSource);
5454
}
5555

56+
#define DEFINE_VALIDATE_FUNCTION(name) \
57+
void MatchProfilesEntry::_validate##name() noexcept \
58+
{ \
59+
_invalid##name = false; \
60+
if (_##name.empty()) \
61+
{ \
62+
/* empty field is valid*/ \
63+
_invalid##name = true; \
64+
return; \
65+
} \
66+
UErrorCode status = U_ZERO_ERROR; \
67+
_##name##Regex = ::Microsoft::Console::ICU::CreateRegex(_##name, 0, &status); \
68+
if (U_FAILURE(status)) \
69+
{ \
70+
_invalid##name = true; \
71+
_##name##Regex.reset(); \
72+
} \
73+
}
74+
75+
DEFINE_VALIDATE_FUNCTION(Name);
76+
DEFINE_VALIDATE_FUNCTION(Commandline);
77+
DEFINE_VALIDATE_FUNCTION(Source);
78+
5679
bool MatchProfilesEntry::MatchesProfile(const Model::Profile& profile)
5780
{
58-
auto isMatch = [](const std::wregex& regex, std::wstring_view text) {
81+
auto isMatch = [](const ::Microsoft::Console::ICU::unique_uregex& regex, std::wstring_view text) {
5982
if (text.empty())
6083
{
6184
return false;
6285
}
63-
return std::regex_match(text.cbegin(), text.cend(), regex);
86+
UErrorCode status = U_ZERO_ERROR;
87+
uregex_setText(regex.get(), reinterpret_cast<const UChar*>(text.data()), static_cast<int32_t>(text.size()), &status);
88+
const auto match = uregex_matches(regex.get(), 0, &status);
89+
return status == U_ZERO_ERROR && match;
6490
};
6591

6692
if (!_Name.empty() && isMatch(_NameRegex, profile.Name()))

Diff for: src/cascadia/TerminalSettingsModel/MatchProfilesEntry.h

+21-31
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,29 @@ Author(s):
1717

1818
#include "ProfileCollectionEntry.h"
1919
#include "MatchProfilesEntry.g.h"
20+
#include "..\buffer\out\UTextAdapter.h"
2021

2122
// This macro defines the getter and setter for a regex property.
2223
// The setter tries to instantiate the regex immediately and caches
2324
// it if successful. If it fails, it sets a boolean flag to track that
2425
// it failed.
25-
#define MATCH_PROFILE_REGEX_PROPERTY(name) \
26-
public: \
27-
hstring name() const noexcept \
28-
{ \
29-
return _##name; \
30-
} \
31-
void name(const hstring& value) noexcept \
32-
{ \
33-
_##name = value; \
34-
_validate##name(); \
35-
} \
36-
\
37-
private: \
38-
void _validate##name() noexcept \
39-
{ \
40-
_invalid##name = false; \
41-
try \
42-
{ \
43-
_##name##Regex = { _##name.cbegin(), _##name.cend() }; \
44-
} \
45-
catch (std::regex_error) \
46-
{ \
47-
_invalid##name = true; \
48-
} \
49-
} \
50-
\
51-
hstring _##name; \
52-
std::wregex _##name##Regex; \
26+
#define DEFINE_MATCH_PROFILE_REGEX_PROPERTY(name) \
27+
public: \
28+
hstring name() const noexcept \
29+
{ \
30+
return _##name; \
31+
} \
32+
void name(const hstring& value) noexcept \
33+
{ \
34+
_##name = value; \
35+
_validate##name(); \
36+
} \
37+
\
38+
private: \
39+
void _validate##name() noexcept; \
40+
\
41+
hstring _##name; \
42+
::Microsoft::Console::ICU::unique_uregex _##name##Regex; \
5343
bool _invalid##name{ false };
5444

5545
namespace winrt::Microsoft::Terminal::Settings::Model::implementation
@@ -67,9 +57,9 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
6757
bool ValidateRegexes() const;
6858
bool MatchesProfile(const Model::Profile& profile);
6959

70-
MATCH_PROFILE_REGEX_PROPERTY(Name);
71-
MATCH_PROFILE_REGEX_PROPERTY(Commandline);
72-
MATCH_PROFILE_REGEX_PROPERTY(Source);
60+
DEFINE_MATCH_PROFILE_REGEX_PROPERTY(Name)
61+
DEFINE_MATCH_PROFILE_REGEX_PROPERTY(Commandline)
62+
DEFINE_MATCH_PROFILE_REGEX_PROPERTY(Source)
7363
};
7464
}
7565

0 commit comments

Comments
 (0)