1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | // See regex_base.h for more information. |
6 | // |
7 | // This header creates some concrete instantiations of RegExBase for commonly used scenarios. In |
8 | // particular, basic regular expression matching base on the regular expression language described in |
9 | // clr::regex::ItemTraitsBase (found in regex_base.h) is instantiated for use with SString, ASCII and |
10 | // UNICODE strings (clr::regex::SStringRegex, clr::regex::WSTRRegEx, and clr::regex::STRRegEx |
11 | // respectively). Each type definition includes an example of its use. |
12 | // |
13 | |
14 | // |
15 | |
16 | #ifndef _REGEX_UTIL_H_ |
17 | #define _REGEX_UTIL_H_ |
18 | |
19 | #ifndef MODE_ANY |
20 | #define MODE_ANY |
21 | #endif |
22 | |
23 | #include "regex_base.h" |
24 | |
25 | #ifdef _DEBUG |
26 | |
27 | namespace clr |
28 | { |
29 | namespace regex |
30 | { |
31 | |
32 | //======================================================================================================= |
33 | // Derives from Group to provide two additional convenience methods (GetSString variants). |
34 | |
35 | class SStringGroup : public Group<SString::CIterator> |
36 | { |
37 | public: |
38 | SStringGroup() |
39 | : Group<SString::CIterator>() |
40 | { WRAPPER_NO_CONTRACT; } |
41 | |
42 | SStringGroup(const InputIterator& _start, const InputIterator& _end, bool _isClosed = false) |
43 | : Group<SString::CIterator>(_start, _end, _isClosed) |
44 | { WRAPPER_NO_CONTRACT; } |
45 | |
46 | // Since SStrings constructed from ranges require the original source string, this is a required |
47 | // input argument. Returns the input substring that matches the corresponding grouping. |
48 | SString GetSString(const SString& src) |
49 | { WRAPPER_NO_CONTRACT; return SString(src, Begin(), End()); } |
50 | |
51 | // Since SStrings constructed from ranges require the original source string, this is a required |
52 | // input argument. This version takes a target SString as a buffer, and also returns this buffer |
53 | // as a reference. Returns the input substring that matches the corresponding grouping. |
54 | SString& GetSString(const SString& src, SString& tgt) |
55 | { WRAPPER_NO_CONTRACT; tgt.Set(src, Begin(), End()); return tgt; } |
56 | }; |
57 | |
58 | //======================================================================================================= |
59 | typedef WCHARItemTraits<SString::CIterator> SStringItemTraits; |
60 | |
61 | //======================================================================================================= |
62 | // Regular expression matching with SStrings. |
63 | // |
64 | // Here is an example of how to use SStringRegEx with grouping enabled. |
65 | // |
66 | // using namespace clr::regex; |
67 | // SString input(SL"Simmons"); // usually this is derived from some variable source |
68 | // SStringRegEx::GroupingContainer container; |
69 | // if (SStringRegEx::Match(SL"(Sim+on)", input, container)) { |
70 | // printf("%S", container[1].GetSString(input).GetUnicode()); |
71 | // } |
72 | // |
73 | // This sample should result in "Simmon" being printed. |
74 | |
75 | |
76 | class SStringRegEx : public RegExBase<SStringItemTraits> |
77 | { |
78 | typedef RegExBase<SStringItemTraits> PARENT_TYPE; |
79 | |
80 | public: |
81 | using PARENT_TYPE::Match; |
82 | using PARENT_TYPE::Matches; |
83 | |
84 | typedef PARENT_TYPE::InputIterator InputIterator; |
85 | |
86 | typedef GroupContainer<InputIterator, SStringGroup > GroupingContainer; |
87 | |
88 | static bool Match( |
89 | const SString& regex, |
90 | const SString& input, |
91 | GroupingContainer& groups, |
92 | MatchFlags flags = DefaultMatchFlags) |
93 | { |
94 | WRAPPER_NO_CONTRACT; |
95 | return Match(regex.Begin(), regex.End(), input.Begin(), input.End(), groups, flags); |
96 | } |
97 | |
98 | static bool Matches( |
99 | const SString& regex, |
100 | const SString& input, |
101 | MatchFlags flags = DefaultMatchFlags) |
102 | { |
103 | WRAPPER_NO_CONTRACT; |
104 | return Matches(regex.Begin(), regex.End(), input.Begin(), input.End(), flags); |
105 | } |
106 | |
107 | }; |
108 | |
109 | //======================================================================================================= |
110 | // Regular expression matching with UNICODE strings. |
111 | // |
112 | // Here is an example of how to use WSTRRegEx to match against a null-terminated string without grouping. |
113 | // |
114 | // using namespace clr::regex; |
115 | // LPCWSTR input = L"Simmons"; |
116 | // if (WSTRRegEx::Matches(L"Sim+on", input)) |
117 | // printf("Match succeeded"); |
118 | // else |
119 | // printf("Match failed"); |
120 | // |
121 | // This sample should result in "Match succeeded" being printed. |
122 | |
123 | class WSTRRegEx : public RegExBase<WCHARItemTraits<LPCWSTR> > |
124 | { |
125 | typedef RegExBase<WCHARItemTraits<LPCWSTR> > PARENT_TYPE; |
126 | |
127 | public: |
128 | using PARENT_TYPE::Match; |
129 | using PARENT_TYPE::Matches; |
130 | |
131 | static bool Match( |
132 | LPCWSTR regex, |
133 | LPCWSTR input, |
134 | GroupingContainer& groups, |
135 | MatchFlags flags = DefaultMatchFlags) |
136 | { |
137 | WRAPPER_NO_CONTRACT; |
138 | return Match(regex, regex + wcslen(regex), input, input + wcslen(input), groups, flags); |
139 | } |
140 | |
141 | static bool Matches( |
142 | LPCWSTR regex, |
143 | LPCWSTR input, |
144 | MatchFlags flags = DefaultMatchFlags) |
145 | { |
146 | CONTRACTL { |
147 | NOTHROW; |
148 | GC_NOTRIGGER; |
149 | MODE_ANY; |
150 | } CONTRACTL_END; |
151 | |
152 | return Matches(regex, regex + wcslen(regex), input, input + wcslen(input), flags); |
153 | } |
154 | }; |
155 | |
156 | //======================================================================================================= |
157 | // Regular expression matching with ASCII strings. |
158 | // |
159 | // Here is an example of how to use STRRegEx to match against a substring based on begin and end range |
160 | // pointers, with grouping disabled and case insensitivity enabled. |
161 | // |
162 | // using namespace clr::regex; |
163 | // LPCSTR input = "123Simmons456"; |
164 | // if (STRRegEx::Matches("Sim+on", input+3, input+10, STRRegEx::MF_CASE_INSENSITIVE)) |
165 | // printf("Match succeeded"); |
166 | // else |
167 | // printf("Match failed"); |
168 | // |
169 | // This sample should result in "Match succeeded" being printed. |
170 | |
171 | class STRRegEx : public RegExBase<CHARItemTraits<LPCSTR> > |
172 | { |
173 | typedef RegExBase<CHARItemTraits<LPCSTR> > PARENT_TYPE; |
174 | |
175 | public: |
176 | using PARENT_TYPE::Match; |
177 | using PARENT_TYPE::Matches; |
178 | |
179 | static bool Match( |
180 | LPCSTR regex, |
181 | LPCSTR input, |
182 | GroupingContainer& groups, |
183 | MatchFlags flags = DefaultMatchFlags) |
184 | { |
185 | WRAPPER_NO_CONTRACT; |
186 | return Match(regex, regex + strlen(regex), input, input + strlen(input), groups, flags); |
187 | } |
188 | |
189 | static bool Matches( |
190 | LPCSTR regex, |
191 | LPCSTR input, |
192 | MatchFlags flags = DefaultMatchFlags) |
193 | { |
194 | CONTRACTL { |
195 | NOTHROW; |
196 | GC_NOTRIGGER; |
197 | MODE_ANY; |
198 | } CONTRACTL_END; |
199 | |
200 | return Matches(regex, regex + strlen(regex), input, input + strlen(input), flags); |
201 | } |
202 | }; |
203 | |
204 | } // namespace regex |
205 | } // namespace clr |
206 | |
207 | #endif // _DEBUG |
208 | |
209 | #endif // _REGEX_UTIL_H_ |
210 | |