-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex_literals.h
221 lines (211 loc) · 7.46 KB
/
regex_literals.h
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
#pragma once
#ifndef REGEX_LITERAL_FLAGS
#define REGEX_LITERAL_FLAGS 1
#endif
#define STRINGIZE(x) STRINGIZE2(x)
#define STRINGIZE2(x) #x
#define LINE_STRING STRINGIZE(__LINE__)
#include <regex>
#include <string>
#define DEFAULT_REGEX_ENGINE std::regex_constants::ECMAScript
// Match result with string inside
template <typename CharT>
struct basic_smatch_string: public std::match_results<typename std::basic_string<CharT>::const_iterator>
{
public:
using _string = std::basic_string<CharT>;
basic_smatch_string() : std::match_results<typename _string::const_iterator>() {}
basic_smatch_string(_string _s) : std::match_results<typename _string::const_iterator>(), s(_s) {}
_string& str()
{return s;}
const _string& str() const
{return s;}
protected:
_string s;
};
using smatch_string = basic_smatch_string<char>;
using wsmatch_string = basic_smatch_string<wchar_t>;
using u16smatch_string = basic_smatch_string<char16_t>;
using u32smatch_string = basic_smatch_string<char32_t>;
template <typename CharT>
struct struct_rm : public std::basic_regex<CharT>
{
using _string = std::basic_string<CharT>;
using _string_view = std::basic_string_view<CharT>;
typedef typename _string::const_iterator const_iterator;
typedef std::match_results<const CharT *> _cmatch;
typedef std::match_results<const_iterator > _smatch;
typedef basic_smatch_string<CharT> _smatch_ws;
struct_rm() = default;
struct_rm(_string str)
{
#if REGEX_LITERAL_FLAGS
std::regex_constants::syntax_option_type flags;
if (check_regex_flags(str, flags))
this->assign(str, flags);
else
#endif
this->assign(str);
}
struct_rm(const CharT *_Str) : struct_rm(_string(_Str))
{
}
struct_rm(const CharT *_Str, size_t _Len) : struct_rm(_string(_Str, _Len))
{
}
// Deleted method
// To get the match results, we have to get the string reference !
// Use match_results_ws instead to get the string into it
inline bool match(_string&& str, _smatch& sm) = delete;
// Deleted method
// r-value string not allowed with smatch ! Use match_results_ws instead to save r-value string.
inline bool search(_string&& str, _smatch& sm) = delete;
inline bool match(_string&& str, _smatch_ws& sm)
{
sm.str().swap(str);
return std::regex_match(sm.str(), sm, *this);
}
inline bool search(_string&& str, _smatch_ws& sm)
{
sm.str().swap(str);
return std::regex_search(sm.str(), sm, *this);
}
inline bool match(const _string_view& str, _cmatch& sm)
{
return std::regex_match(str.data(), sm, *this);
}
inline bool match(const CharT* str, _cmatch& sm)
{
return std::regex_match(str, sm, *this);
}
inline bool match(const _string& str, _smatch& sm)
{
return std::regex_match(str, sm, *this);
}
inline bool match(const CharT* str)
{
return std::regex_match(str, *this);
}
inline bool match(const _string& str)
{
return std::regex_match(str, *this);
}
inline bool match(_string&& str)
{
return std::regex_match(str, *this);
}
inline bool match(_string_view str)
{
return std::regex_match(str.data(), *this);
}
inline bool search(const CharT* str, _cmatch& sm)
{
return std::regex_search(str, sm, *this);
}
inline bool search(const _string& str, _smatch& sm)
{
return std::regex_search(str, sm, *this);
}
inline bool search(const _string_view& str, _cmatch& sm)
{
return std::regex_search(str.data(), sm, *this);
}
inline bool search(const CharT* str)
{
return std::regex_search(str, *this);
}
inline bool search(const _string& str)
{
return std::regex_search(str, *this);
}
inline bool search(_string&& str)
{
return std::regex_search(str, *this);
}
inline bool search(_string_view str)
{
return std::regex_search(str.data(), *this);
}
private:
#if REGEX_LITERAL_FLAGS
inline const CharT* RegexFlagsDetector();
//static CharT const *RegexFlagsDetector;
bool check_regex_flags(_string& _str, std::regex_constants::syntax_option_type& _flags)
{
using namespace std;
using namespace regex_constants;
static const std::basic_regex<CharT> reg(RegexFlagsDetector());
_smatch sm;
syntax_option_type flags = static_cast<syntax_option_type>(0);
syntax_option_type engine = DEFAULT_REGEX_ENGINE;
if (regex_match(_str,sm,reg))
{
const auto& beg = sm[2].first, &end = sm[2].second;
for (auto itL=beg; itL != end; itL++)
{
CharT l = *itL;
switch (l)
{
case 'i': flags |= icase; break;
case 'b': flags |= nosubs; break;
case 'o': flags |= optimize; break;
case 'c': flags |= regex_constants::collate; break;
//#if __cplusplus >= 201703L
// case 'm': flags |= regex_constants::multiline; break;
//#endif
case 'E': engine = ECMAScript; break;
case 'B': engine = basic; break;
case 'X': engine = extended; break;
case 'A': engine = awk; break;
case 'G': engine = grep; break;
case 'P': engine = egrep; break;
}
}
_str = sm[1];
_flags = flags | engine;
return true;
}
return false;
}
std::regex_constants::syntax_option_type check_regex_flags(const CharT *_Str, size_t _Len)
{
return check_regex_flags(_string(_Str, _Len));
}
#endif
};
inline namespace literals
{
inline namespace regex_literals
{
inline struct_rm<char> operator "" _rg(const char *_Str, size_t _Len)
{ // construct literal from [_Str, _Str + _Len)
return (struct_rm<char>(_Str, _Len));
}
inline struct_rm<wchar_t> operator "" _rg(const wchar_t *_Str, size_t _Len)
{ // construct literal from [_Str, _Str + _Len)
return (struct_rm<wchar_t>(_Str, _Len));
}
/// regex_trait<char16_t> and regex_trait<char32_t> don't exist. So impossible to define
// inline struct_rm<char16_t> operator "" _rg(const char16_t *_Str, size_t _Len)
// { // construct literal from [_Str, _Str + _Len)
// return (struct_rm<char16_t>(_Str, _Len));
// }
// inline struct_rm<char32_t> operator "" _rg(const char32_t *_Str, size_t _Len)
// { // construct literal from [_Str, _Str + _Len)
// return (struct_rm<char32_t>(_Str, _Len));
// }
}
}
#if REGEX_LITERAL_FLAGS
template <>
inline const char* struct_rm<char>::RegexFlagsDetector() { return R"(^\/(.*)\/([ibocmEXBAGP]*)$)"; };
template <>
inline const wchar_t* struct_rm<wchar_t>::RegexFlagsDetector() { return LR"(^\/(.*)\/([ibocmEXBAGP]*)$)"; };
// template <>
// inline const char16_t* struct_rm<char16_t>::RegexFlagsDetector() { return uR"(^\/(.*)\/([ibocmEXBAGP]*)$)"; };
// template <>
// inline const char32_t* struct_rm<char32_t>::RegexFlagsDetector() { return UR"(^\/(.*)\/([ibocmEXBAGP]*)$)"; };
#endif
#undef STRINGIZE
#undef STRINGIZE2
#undef LINE_STRING