|
17 | 17 | #include <iterator> |
18 | 18 | #include "test_macros.h" |
19 | 19 |
|
20 | | -int main(int, char**) |
21 | | -{ |
22 | | - { |
23 | | - std::regex phone_numbers("\\d{3}-\\d{4}"); |
24 | | - const char phone_book[] = "555-1234, 555-2345, 555-3456"; |
25 | | - std::cregex_iterator i(std::begin(phone_book), std::end(phone_book), phone_numbers); |
26 | | - std::cregex_iterator i2 = i; |
27 | | - assert(i != std::cregex_iterator()); |
28 | | - assert(i2!= std::cregex_iterator()); |
29 | | - assert((*i).size() == 1); |
30 | | - assert((*i).position() == 0); |
31 | | - assert((*i).str() == "555-1234"); |
32 | | - assert((*i2).size() == 1); |
33 | | - assert((*i2).position() == 0); |
34 | | - assert((*i2).str() == "555-1234"); |
35 | | - i++; |
36 | | - assert(i != std::cregex_iterator()); |
37 | | - assert(i2!= std::cregex_iterator()); |
38 | | - assert((*i).size() == 1); |
39 | | - assert((*i).position() == 10); |
40 | | - assert((*i).str() == "555-2345"); |
41 | | - assert((*i2).size() == 1); |
42 | | - assert((*i2).position() == 0); |
43 | | - assert((*i2).str() == "555-1234"); |
44 | | - i++; |
45 | | - assert(i != std::cregex_iterator()); |
46 | | - assert(i2!= std::cregex_iterator()); |
47 | | - assert((*i).size() == 1); |
48 | | - assert((*i).position() == 20); |
49 | | - assert((*i).str() == "555-3456"); |
50 | | - assert((*i2).size() == 1); |
51 | | - assert((*i2).position() == 0); |
52 | | - assert((*i2).str() == "555-1234"); |
53 | | - i++; |
54 | | - assert(i == std::cregex_iterator()); |
55 | | - assert(i2!= std::cregex_iterator()); |
56 | | - assert((*i2).size() == 1); |
57 | | - assert((*i2).position() == 0); |
58 | | - assert((*i2).str() == "555-1234"); |
59 | | - } |
60 | | - { |
61 | | - std::regex phone_numbers("\\d{3}-\\d{4}"); |
62 | | - const char phone_book[] = "555-1234, 555-2345, 555-3456"; |
63 | | - std::cregex_iterator i(std::begin(phone_book), std::end(phone_book), phone_numbers); |
64 | | - std::cregex_iterator i2 = i; |
65 | | - assert(i != std::cregex_iterator()); |
66 | | - assert(i2!= std::cregex_iterator()); |
67 | | - assert((*i).size() == 1); |
68 | | - assert((*i).position() == 0); |
69 | | - assert((*i).str() == "555-1234"); |
70 | | - assert((*i2).size() == 1); |
71 | | - assert((*i2).position() == 0); |
72 | | - assert((*i2).str() == "555-1234"); |
73 | | - ++i; |
74 | | - assert(i != std::cregex_iterator()); |
75 | | - assert(i2!= std::cregex_iterator()); |
76 | | - assert((*i).size() == 1); |
77 | | - assert((*i).position() == 10); |
78 | | - assert((*i).str() == "555-2345"); |
79 | | - assert((*i2).size() == 1); |
80 | | - assert((*i2).position() == 0); |
81 | | - assert((*i2).str() == "555-1234"); |
82 | | - ++i; |
83 | | - assert(i != std::cregex_iterator()); |
84 | | - assert(i2!= std::cregex_iterator()); |
85 | | - assert((*i).size() == 1); |
86 | | - assert((*i).position() == 20); |
87 | | - assert((*i).str() == "555-3456"); |
88 | | - assert((*i2).size() == 1); |
89 | | - assert((*i2).position() == 0); |
90 | | - assert((*i2).str() == "555-1234"); |
91 | | - ++i; |
92 | | - assert(i == std::cregex_iterator()); |
93 | | - assert(i2!= std::cregex_iterator()); |
94 | | - assert((*i2).size() == 1); |
95 | | - assert((*i2).position() == 0); |
96 | | - assert((*i2).str() == "555-1234"); |
97 | | - } |
98 | | - { // https://llvm.org/PR33681 |
99 | | - std::regex rex(".*"); |
100 | | - const char foo[] = "foo"; |
| 20 | +void validate_prefixes(const std::regex& empty_matching_pattern) { |
| 21 | + const char source[] = "abc"; |
| 22 | + |
| 23 | + std::cregex_iterator i(source, source + 3, empty_matching_pattern); |
| 24 | + assert(!i->prefix().matched); |
| 25 | + assert(i->prefix().length() == 0); |
| 26 | + assert(i->prefix().first == source); |
| 27 | + assert(i->prefix().second == source); |
| 28 | + |
| 29 | + ++i; |
| 30 | + assert(i->prefix().matched); |
| 31 | + assert(i->prefix().length() == 1); |
| 32 | + assert(i->prefix().first == source); |
| 33 | + assert(i->prefix().second == source + 1); |
| 34 | + assert(i->prefix().str() == "a"); |
| 35 | + |
| 36 | + ++i; |
| 37 | + assert(i->prefix().matched); |
| 38 | + assert(i->prefix().length() == 1); |
| 39 | + assert(i->prefix().first == source + 1); |
| 40 | + assert(i->prefix().second == source + 2); |
| 41 | + assert(i->prefix().str() == "b"); |
| 42 | + |
| 43 | + ++i; |
| 44 | + assert(i->prefix().matched); |
| 45 | + assert(i->prefix().length() == 1); |
| 46 | + assert(i->prefix().first == source + 2); |
| 47 | + assert(i->prefix().second == source + 3); |
| 48 | + assert(i->prefix().str() == "c"); |
| 49 | + |
| 50 | + ++i; |
| 51 | + assert(i == std::cregex_iterator()); |
| 52 | +} |
| 53 | + |
| 54 | +void test_prefix_adjustment() { |
| 55 | + // Check that we correctly adjust the match prefix when dealing with zero-length matches -- this is explicitly |
| 56 | + // required by the Standard ([re.regiter.incr]: "In all cases in which the call to `regex_search` returns true, |
| 57 | + // `match.prefix().first` shall be equal to the previous value of `match[0].second`"). For a pattern that matches |
| 58 | + // empty sequences, there is an implicit zero-length match between every character in a string -- make sure the |
| 59 | + // prefix of each of these matches (except the first one) is the preceding character. |
| 60 | + |
| 61 | + // An empty pattern produces zero-length matches. |
| 62 | + validate_prefixes(std::regex("")); |
| 63 | + // Any character repeated zero or more times can produce zero-length matches. |
| 64 | + validate_prefixes(std::regex("X*")); |
| 65 | + validate_prefixes(std::regex("X{0,3}")); |
| 66 | +} |
| 67 | + |
| 68 | +int main(int, char**) { |
| 69 | + { |
| 70 | + std::regex phone_numbers("\\d{3}-\\d{4}"); |
| 71 | + const char phone_book[] = "555-1234, 555-2345, 555-3456"; |
| 72 | + std::cregex_iterator i(std::begin(phone_book), std::end(phone_book), phone_numbers); |
| 73 | + std::cregex_iterator i2 = i; |
| 74 | + assert(i != std::cregex_iterator()); |
| 75 | + assert(i2 != std::cregex_iterator()); |
| 76 | + assert((*i).size() == 1); |
| 77 | + assert((*i).position() == 0); |
| 78 | + assert((*i).str() == "555-1234"); |
| 79 | + assert((*i2).size() == 1); |
| 80 | + assert((*i2).position() == 0); |
| 81 | + assert((*i2).str() == "555-1234"); |
| 82 | + i++; |
| 83 | + assert(i != std::cregex_iterator()); |
| 84 | + assert(i2 != std::cregex_iterator()); |
| 85 | + assert((*i).size() == 1); |
| 86 | + assert((*i).position() == 10); |
| 87 | + assert((*i).str() == "555-2345"); |
| 88 | + assert((*i2).size() == 1); |
| 89 | + assert((*i2).position() == 0); |
| 90 | + assert((*i2).str() == "555-1234"); |
| 91 | + i++; |
| 92 | + assert(i != std::cregex_iterator()); |
| 93 | + assert(i2 != std::cregex_iterator()); |
| 94 | + assert((*i).size() == 1); |
| 95 | + assert((*i).position() == 20); |
| 96 | + assert((*i).str() == "555-3456"); |
| 97 | + assert((*i2).size() == 1); |
| 98 | + assert((*i2).position() == 0); |
| 99 | + assert((*i2).str() == "555-1234"); |
| 100 | + i++; |
| 101 | + assert(i == std::cregex_iterator()); |
| 102 | + assert(i2 != std::cregex_iterator()); |
| 103 | + assert((*i2).size() == 1); |
| 104 | + assert((*i2).position() == 0); |
| 105 | + assert((*i2).str() == "555-1234"); |
| 106 | + } |
| 107 | + { |
| 108 | + std::regex phone_numbers("\\d{3}-\\d{4}"); |
| 109 | + const char phone_book[] = "555-1234, 555-2345, 555-3456"; |
| 110 | + std::cregex_iterator i(std::begin(phone_book), std::end(phone_book), phone_numbers); |
| 111 | + std::cregex_iterator i2 = i; |
| 112 | + assert(i != std::cregex_iterator()); |
| 113 | + assert(i2 != std::cregex_iterator()); |
| 114 | + assert((*i).size() == 1); |
| 115 | + assert((*i).position() == 0); |
| 116 | + assert((*i).str() == "555-1234"); |
| 117 | + assert((*i2).size() == 1); |
| 118 | + assert((*i2).position() == 0); |
| 119 | + assert((*i2).str() == "555-1234"); |
| 120 | + ++i; |
| 121 | + assert(i != std::cregex_iterator()); |
| 122 | + assert(i2 != std::cregex_iterator()); |
| 123 | + assert((*i).size() == 1); |
| 124 | + assert((*i).position() == 10); |
| 125 | + assert((*i).str() == "555-2345"); |
| 126 | + assert((*i2).size() == 1); |
| 127 | + assert((*i2).position() == 0); |
| 128 | + assert((*i2).str() == "555-1234"); |
| 129 | + ++i; |
| 130 | + assert(i != std::cregex_iterator()); |
| 131 | + assert(i2 != std::cregex_iterator()); |
| 132 | + assert((*i).size() == 1); |
| 133 | + assert((*i).position() == 20); |
| 134 | + assert((*i).str() == "555-3456"); |
| 135 | + assert((*i2).size() == 1); |
| 136 | + assert((*i2).position() == 0); |
| 137 | + assert((*i2).str() == "555-1234"); |
| 138 | + ++i; |
| 139 | + assert(i == std::cregex_iterator()); |
| 140 | + assert(i2 != std::cregex_iterator()); |
| 141 | + assert((*i2).size() == 1); |
| 142 | + assert((*i2).position() == 0); |
| 143 | + assert((*i2).str() == "555-1234"); |
| 144 | + } |
| 145 | + { // https://llvm.org/PR33681 |
| 146 | + std::regex rex(".*"); |
| 147 | + const char foo[] = "foo"; |
101 | 148 | // The -1 is because we don't want the implicit null from the array. |
102 | | - std::cregex_iterator i(std::begin(foo), std::end(foo) - 1, rex); |
103 | | - std::cregex_iterator e; |
104 | | - assert(i != e); |
105 | | - assert((*i).size() == 1); |
106 | | - assert((*i).str() == "foo"); |
107 | | - |
108 | | - ++i; |
109 | | - assert(i != e); |
110 | | - assert((*i).size() == 1); |
111 | | - assert((*i).str() == ""); |
112 | | - |
113 | | - ++i; |
114 | | - assert(i == e); |
115 | | - } |
| 149 | + std::cregex_iterator i(std::begin(foo), std::end(foo) - 1, rex); |
| 150 | + std::cregex_iterator e; |
| 151 | + assert(i != e); |
| 152 | + assert((*i).size() == 1); |
| 153 | + assert((*i).str() == "foo"); |
| 154 | + |
| 155 | + ++i; |
| 156 | + assert(i != e); |
| 157 | + assert((*i).size() == 1); |
| 158 | + assert((*i).str() == ""); |
| 159 | + |
| 160 | + ++i; |
| 161 | + assert(i == e); |
| 162 | + } |
| 163 | + |
| 164 | + test_prefix_adjustment(); |
116 | 165 |
|
117 | 166 | return 0; |
118 | 167 | } |
0 commit comments