xref: /llvm-project/libcxx/test/std/re/re.const/re.matchflag/match_prev_avail.pass.cpp (revision ef2cdfe393d01cd4935c806387ac912b5a2c8ced)
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 // <regex>
10 
11 // match_prev_avail:
12 //     --first is a valid iterator position. When this flag is set the flags
13 //     match_not_bol and match_not_bow shall be ignored by the regular
14 //     expression algorithms (30.11) and iterators (30.12)
15 
16 #include <regex>
17 
18 #include <cassert>
19 #include <string>
20 
main(int,char **)21 int main(int, char**) {
22   char str1[] = "\na";
23   auto str1_scnd = str1 + 1;
24 
25   // Assert that match_prev_avail disables match_not_bol and this matches
26   assert(std::regex_match(str1 + 1, str1 + 2, std::regex("^a"),
27                      std::regex_constants::match_not_bol |
28                          std::regex_constants::match_prev_avail));
29   // Manually passing match_prev_avail defines that --str1 is a valid position
30   assert(std::regex_match(str1_scnd, std::regex("a"),
31                      std::regex_constants::match_not_bol |
32                          std::regex_constants::match_prev_avail));
33 
34   //Assert that match_prev_avail disables match_not_bow and this matches
35   assert(std::regex_search(str1, std::regex("\\ba")));
36   assert(std::regex_match(str1 + 1, str1 + 2, std::regex("\\ba\\b"),
37                      std::regex_constants::match_not_bow |
38                          std::regex_constants::match_prev_avail));
39   assert(std::regex_search(str1_scnd, std::regex("\\ba"),
40                       std::regex_constants::match_not_bow |
41                           std::regex_constants::match_prev_avail));
42 
43   //Assert that match_prev_avail disables both match_not_bow and match_not_bol
44   assert(std::regex_match(str1 + 1, str1 + 2, std::regex("^a"),
45                      std::regex_constants::match_not_bol |
46                          std::regex_constants::match_not_bow |
47                          std::regex_constants::match_prev_avail));
48   assert(std::regex_match(str1_scnd, std::regex("\\ba"),
49                      std::regex_constants::match_not_bol |
50                          std::regex_constants::match_not_bow |
51                          std::regex_constants::match_prev_avail));
52 
53   // pr 42199
54   std::string S = " cd";
55   std::string::iterator Start = S.begin() + 1;
56   std::string::iterator End = S.end();
57   assert(std::regex_search(Start, End, std::regex("^cd")));
58 
59   assert(!std::regex_search(Start, End, std::regex("^cd"),
60             std::regex_constants::match_not_bol));
61   assert(!std::regex_search(Start, End, std::regex(".*\\bcd\\b"),
62             std::regex_constants::match_not_bow));
63   assert(!std::regex_search(Start, End, std::regex("^cd"),
64             std::regex_constants::match_not_bol |
65             std::regex_constants::match_not_bow));
66   assert(!std::regex_search(Start, End, std::regex(".*\\bcd\\b"),
67             std::regex_constants::match_not_bol |
68             std::regex_constants::match_not_bow));
69 
70   assert(std::regex_search(Start, End, std::regex("^cd"),
71             std::regex_constants::match_prev_avail));
72 
73   assert(std::regex_search(Start, End, std::regex("^cd"),
74             std::regex_constants::match_not_bol |
75             std::regex_constants::match_prev_avail));
76   assert(std::regex_search(Start, End, std::regex("^cd"),
77             std::regex_constants::match_not_bow |
78             std::regex_constants::match_prev_avail));
79   assert(std::regex_match(Start, End, std::regex("\\bcd\\b"),
80             std::regex_constants::match_not_bol |
81             std::regex_constants::match_not_bow |
82             std::regex_constants::match_prev_avail));
83   return 0;
84 }
85