Skip to content

Commit 0a4aee4

Browse files
committed
[libc++][regex] Correctly adjust match prefix for zero-length matches.
1 parent dbc3e26 commit 0a4aee4

File tree

2 files changed

+161
-97
lines changed

2 files changed

+161
-97
lines changed

libcxx/include/regex

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4700,6 +4700,9 @@ private:
47004700

47014701
template <class, class>
47024702
friend class __lookahead;
4703+
4704+
template <class, class, class>
4705+
friend class regex_iterator;
47034706
};
47044707

47054708
template <class _BidirectionalIterator, class _Allocator>
@@ -5410,7 +5413,9 @@ template <class _BidirectionalIterator, class _CharT, class _Traits>
54105413
regex_iterator<_BidirectionalIterator, _CharT, _Traits>&
54115414
regex_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++() {
54125415
__flags_ |= regex_constants::__no_update_pos;
5413-
_BidirectionalIterator __start = __match_[0].second;
5416+
_BidirectionalIterator __start = __match_[0].second;
5417+
_BidirectionalIterator __prefix_start = __start;
5418+
54145419
if (__match_[0].first == __match_[0].second) {
54155420
if (__start == __end_) {
54165421
__match_ = value_type();
@@ -5424,9 +5429,21 @@ regex_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++() {
54245429
else
54255430
++__start;
54265431
}
5432+
54275433
__flags_ |= regex_constants::match_prev_avail;
5428-
if (!std::regex_search(__start, __end_, __match_, *__pregex_, __flags_))
5434+
if (!std::regex_search(__start, __end_, __match_, *__pregex_, __flags_)) {
54295435
__match_ = value_type();
5436+
5437+
} else {
5438+
// The Standard mandates that if `regex_search` returns true ([re.regiter.incr]), "`match.prefix().first` shall be
5439+
// equal to the previous value of `match[0].second`... It is unspecified how the implementation makes these
5440+
// adjustments." The adjustment is necessary if we incremented `__start` above (the branch that deals with
5441+
// zero-length matches).
5442+
auto& __prefix = __match_.__prefix_;
5443+
__prefix.first = __prefix_start;
5444+
__prefix.matched = __prefix.first != __prefix.second;
5445+
}
5446+
54305447
return *this;
54315448
}
54325449

libcxx/test/std/re/re.iter/re.regiter/re.regiter.incr/post.pass.cpp

Lines changed: 142 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -17,102 +17,149 @@
1717
#include <iterator>
1818
#include "test_macros.h"
1919

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";
101100
// 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+
}
116163

117164
return 0;
118165
}

0 commit comments

Comments
 (0)