Skip to content

Commit c14ac80

Browse files
author
Diogo Sampaio
committed
[FIX][libc++][Regex] Using regex_constants match_prev_avail | match_not_bol | match_not_bow
Summary: pr42199 When using regex_constants::match_prev_avail, it is defined that --first is valid, and match_not_bol and match_not_bow should be ignored. At the moment these flags are not ignored. This fixis that. Reviewers: ldionne, miyuki, EricWF, mclow.lists, #libc Reviewed By: ldionne, miyuki, #libc Subscribers: broadwaylamb, dexonsmith, libcxx-commits Tags: #libc Differential Revision: https://reviews.llvm.org/D75622
1 parent 0ae6282 commit c14ac80

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

libcxx/include/regex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5940,6 +5940,9 @@ basic_regex<_CharT, _Traits>::__search(
59405940
match_results<const _CharT*, _Allocator>& __m,
59415941
regex_constants::match_flag_type __flags) const
59425942
{
5943+
if (__flags & regex_constants::match_prev_avail)
5944+
__flags &= ~(regex_constants::match_not_bol | regex_constants::match_not_bow);
5945+
59435946
__m.__init(1 + mark_count(), __first, __last,
59445947
__flags & regex_constants::__no_update_pos);
59455948
if (__match_at_start(__first, __last, __m, __flags,
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 <cassert>
17+
#include <regex>
18+
using namespace std;
19+
20+
int main() {
21+
char str1[] = "\na";
22+
auto str1_scnd = str1 + 1;
23+
// Assert that match_prev_avail disables match_not_bol and this matches
24+
assert(regex_match(str1 + 1, str1 + 2, regex("^a"),
25+
regex_constants::match_not_bol |
26+
regex_constants::match_prev_avail));
27+
// Manually passing match_prev_avail defines that --str1 is a valid position
28+
assert(regex_match(str1_scnd, regex("a"),
29+
regex_constants::match_not_bol |
30+
regex_constants::match_prev_avail));
31+
32+
//Assert that match_prev_avail disables match_not_bow and this matches
33+
assert(regex_search(str1, regex("\\ba")));
34+
assert(regex_match(str1 + 1, str1 + 2, regex("\\ba\\b"),
35+
regex_constants::match_not_bow |
36+
regex_constants::match_prev_avail));
37+
assert(regex_search(str1, regex("\\ba"),
38+
regex_constants::match_not_bow |
39+
regex_constants::match_prev_avail));
40+
41+
//Assert that match_prev_avail disables both match_not_bow and match_not_bol
42+
assert(regex_match(str1 + 1, str1 + 2, regex("^a"),
43+
regex_constants::match_not_bol |
44+
regex_constants::match_not_bow |
45+
regex_constants::match_prev_avail));
46+
assert(regex_match(str1_scnd, regex("\\ba"),
47+
regex_constants::match_not_bol |
48+
regex_constants::match_not_bow |
49+
regex_constants::match_prev_avail));
50+
51+
// pr 42199
52+
string S = " cd";
53+
string::iterator Start = S.begin() + 1;
54+
string::iterator End = S.end();
55+
assert(regex_search(Start, End, regex("^cd")));
56+
57+
assert(
58+
!regex_search(Start, End, regex("^cd"), regex_constants::match_not_bol));
59+
assert(!regex_search(Start, End, regex(".*\\bcd\\b"),
60+
regex_constants::match_not_bow));
61+
assert(!regex_search(Start, End, regex("^cd"),
62+
regex_constants::match_not_bol |
63+
regex_constants::match_not_bow));
64+
assert(!regex_search(Start, End, regex(".*\\bcd\\b"),
65+
regex_constants::match_not_bol |
66+
regex_constants::match_not_bow));
67+
68+
assert(regex_search(Start, End, regex("^cd"),
69+
regex_constants::match_prev_avail));
70+
71+
assert(regex_search(Start, End, regex("^cd"),
72+
regex_constants::match_not_bol |
73+
regex_constants::match_prev_avail));
74+
assert(regex_search(Start, End, regex("^cd"),
75+
regex_constants::match_not_bow |
76+
regex_constants::match_prev_avail));
77+
assert(regex_match(Start, End, regex("\\bcd\\b"),
78+
regex_constants::match_not_bol |
79+
regex_constants::match_not_bow |
80+
regex_constants::match_prev_avail));
81+
return 0;
82+
}

0 commit comments

Comments
 (0)