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