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