1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03
10 
11 // <regex>
12 
13 // multiline:
14 //     Specifies that ^ shall match the beginning of a line and $ shall match
15 //     the end of a line, if the ECMAScript engine is selected.
16 
17 #include <regex>
18 #include <cassert>
19 #include "test_macros.h"
20 
search(const char * pat,std::regex_constants::syntax_option_type f,const char * target,bool expected)21 static void search(const char* pat, std::regex_constants::syntax_option_type f,
22                    const char* target, bool expected)
23 {
24     std::regex re(pat, f);
25     std::cmatch m;
26     assert(std::regex_search(target, m, re) == expected);
27 
28     if(expected) {
29         assert(m.size() == 1);
30         assert(m.length(0) == 3);
31         assert(m.str(0) == "foo");
32     }
33     else
34     {
35         assert(m.size() == 0);
36     }
37 }
38 
main(int,char **)39 int main(int, char**)
40 {
41     using std::regex_constants::ECMAScript;
42     using std::regex_constants::basic;
43     using std::regex_constants::extended;
44     using std::regex_constants::awk;
45     using std::regex_constants::grep;
46     using std::regex_constants::egrep;
47     using std::regex_constants::multiline;
48 
49     {
50         const char* pat = "^foo";
51         const char* target = "foo";
52 
53         search(pat, ECMAScript, target, true);
54         search(pat, basic, target, true);
55         search(pat, extended, target, true);
56         search(pat, awk, target, true);
57         search(pat, grep, target, true);
58         search(pat, egrep, target, true);
59 
60         search(pat, ECMAScript | multiline, target, true);
61         search(pat, basic | multiline, target, true);
62         search(pat, extended | multiline, target, true);
63         search(pat, awk | multiline, target, true);
64         search(pat, grep | multiline, target, true);
65         search(pat, egrep | multiline, target, true);
66     }
67     {
68         const char* pat = "^foo";
69         const char* target = "\nfoo";
70 
71         search(pat, ECMAScript, target, false);
72         search(pat, basic, target, false);
73         search(pat, extended, target, false);
74         search(pat, awk, target, false);
75         search(pat, grep, target, false);
76         search(pat, egrep, target, false);
77 
78         search(pat, ECMAScript | multiline, target, true);
79         search(pat, basic | multiline, target, false);
80         search(pat, extended | multiline, target, false);
81         search(pat, awk | multiline, target, false);
82         search(pat, grep | multiline, target, false);
83         search(pat, egrep | multiline, target, false);
84     }
85     {
86         const char* pat = "^foo";
87         const char* target = "bar\nfoo";
88 
89         search(pat, ECMAScript, target, false);
90         search(pat, basic, target, false);
91         search(pat, extended, target, false);
92         search(pat, awk, target, false);
93         search(pat, grep, target, false);
94         search(pat, egrep, target, false);
95 
96         search(pat, ECMAScript | multiline, target, true);
97         search(pat, basic | multiline, target, false);
98         search(pat, extended | multiline, target, false);
99         search(pat, awk | multiline, target, false);
100         search(pat, grep | multiline, target, false);
101         search(pat, egrep | multiline, target, false);
102     }
103 
104     {
105         const char* pat = "foo$";
106         const char* target = "foo";
107 
108         search(pat, ECMAScript, target, true);
109         search(pat, basic, target, true);
110         search(pat, extended, target, true);
111         search(pat, awk, target, true);
112         search(pat, grep, target, true);
113         search(pat, egrep, target, true);
114 
115         search(pat, ECMAScript | multiline, target, true);
116         search(pat, basic | multiline, target, true);
117         search(pat, extended | multiline, target, true);
118         search(pat, awk | multiline, target, true);
119         search(pat, grep | multiline, target, true);
120         search(pat, egrep | multiline, target, true);
121     }
122     {
123         const char* pat = "foo$";
124         const char* target = "foo\n";
125 
126         search(pat, ECMAScript, target, false);
127         search(pat, basic, target, false);
128         search(pat, extended, target, false);
129         search(pat, awk, target, false);
130         search(pat, grep, target, false);
131         search(pat, egrep, target, false);
132 
133         search(pat, ECMAScript | multiline, target, true);
134         search(pat, basic | multiline, target, false);
135         search(pat, extended | multiline, target, false);
136         search(pat, awk | multiline, target, false);
137         search(pat, grep | multiline, target, false);
138         search(pat, egrep | multiline, target, false);
139     }
140     {
141         const char* pat = "foo$";
142         const char* target = "foo\nbar";
143 
144         search(pat, ECMAScript, target, false);
145         search(pat, basic, target, false);
146         search(pat, extended, target, false);
147         search(pat, awk, target, false);
148         search(pat, grep, target, false);
149         search(pat, egrep, target, false);
150 
151         search(pat, ECMAScript | multiline, target, true);
152         search(pat, basic | multiline, target, false);
153         search(pat, extended | multiline, target, false);
154         search(pat, awk | multiline, target, false);
155         search(pat, grep | multiline, target, false);
156         search(pat, egrep | multiline, target, false);
157     }
158 
159 
160     {
161         const char* pat = "^foo";
162         const char* target = "foo";
163 
164         search(pat, ECMAScript, target, true);
165         search(pat, basic, target, true);
166         search(pat, extended, target, true);
167         search(pat, awk, target, true);
168         search(pat, grep, target, true);
169         search(pat, egrep, target, true);
170 
171         search(pat, ECMAScript | multiline, target, true);
172         search(pat, basic | multiline, target, true);
173         search(pat, extended | multiline, target, true);
174         search(pat, awk | multiline, target, true);
175         search(pat, grep | multiline, target, true);
176         search(pat, egrep | multiline, target, true);
177     }
178     {
179         const char* pat = "^foo";
180         const char* target = "\rfoo";
181 
182         search(pat, ECMAScript, target, false);
183         search(pat, basic, target, false);
184         search(pat, extended, target, false);
185         search(pat, awk, target, false);
186         search(pat, grep, target, false);
187         search(pat, egrep, target, false);
188 
189         search(pat, ECMAScript | multiline, target, true);
190         search(pat, basic | multiline, target, false);
191         search(pat, extended | multiline, target, false);
192         search(pat, awk | multiline, target, false);
193         search(pat, grep | multiline, target, false);
194         search(pat, egrep | multiline, target, false);
195     }
196     {
197         const char* pat = "^foo";
198         const char* target = "bar\rfoo";
199 
200         search(pat, ECMAScript, target, false);
201         search(pat, basic, target, false);
202         search(pat, extended, target, false);
203         search(pat, awk, target, false);
204         search(pat, grep, target, false);
205         search(pat, egrep, target, false);
206 
207         search(pat, ECMAScript | multiline, target, true);
208         search(pat, basic | multiline, target, false);
209         search(pat, extended | multiline, target, false);
210         search(pat, awk | multiline, target, false);
211         search(pat, grep | multiline, target, false);
212         search(pat, egrep | multiline, target, false);
213     }
214 
215     {
216         const char* pat = "foo$";
217         const char* target = "foo";
218 
219         search(pat, ECMAScript, target, true);
220         search(pat, basic, target, true);
221         search(pat, extended, target, true);
222         search(pat, awk, target, true);
223         search(pat, grep, target, true);
224         search(pat, egrep, target, true);
225 
226         search(pat, ECMAScript | multiline, target, true);
227         search(pat, basic | multiline, target, true);
228         search(pat, extended | multiline, target, true);
229         search(pat, awk | multiline, target, true);
230         search(pat, grep | multiline, target, true);
231         search(pat, egrep | multiline, target, true);
232     }
233     {
234         const char* pat = "foo$";
235         const char* target = "foo\r";
236 
237         search(pat, ECMAScript, target, false);
238         search(pat, basic, target, false);
239         search(pat, extended, target, false);
240         search(pat, awk, target, false);
241         search(pat, grep, target, false);
242         search(pat, egrep, target, false);
243 
244         search(pat, ECMAScript | multiline, target, true);
245         search(pat, basic | multiline, target, false);
246         search(pat, extended | multiline, target, false);
247         search(pat, awk | multiline, target, false);
248         search(pat, grep | multiline, target, false);
249         search(pat, egrep | multiline, target, false);
250     }
251     {
252         const char* pat = "foo$";
253         const char* target = "foo\rbar";
254 
255         search(pat, ECMAScript, target, false);
256         search(pat, basic, target, false);
257         search(pat, extended, target, false);
258         search(pat, awk, target, false);
259         search(pat, grep, target, false);
260         search(pat, egrep, target, false);
261 
262         search(pat, ECMAScript | multiline, target, true);
263         search(pat, basic | multiline, target, false);
264         search(pat, extended | multiline, target, false);
265         search(pat, awk | multiline, target, false);
266         search(pat, grep | multiline, target, false);
267         search(pat, egrep | multiline, target, false);
268     }
269 
270     return 0;
271 }
272