1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 // template <class OutputIterator, class BidirectionalIterator,
12 // class traits, class charT, class ST, class SA>
14 // regex_replace(OutputIterator out,
15 // BidirectionalIterator first, BidirectionalIterator last,
16 // const basic_regex<charT, traits>& e,
18 // regex_constants::match_flag_type flags =
19 // regex_constants::match_default);
24 #include "test_macros.h"
25 #include "test_iterators.h"
30 std::regex
phone_numbers("\\d{3}-\\d{4}");
31 const char phone_book
[] = "555-1234, 555-2345, 555-3456";
32 typedef cpp17_output_iterator
<char*> Out
;
33 typedef bidirectional_iterator
<const char*> Bi
;
35 Out r
= std::regex_replace(Out(buf
), Bi(std::begin(phone_book
)),
36 Bi(std::end(phone_book
)-1), phone_numbers
,
38 assert(base(r
) == buf
+40);
39 assert(buf
== std::string("123-555-1234, 123-555-2345, 123-555-3456"));
42 std::regex
phone_numbers("\\d{3}-\\d{4}");
43 const char phone_book
[] = "555-1234, 555-2345, 555-3456";
44 typedef cpp17_output_iterator
<char*> Out
;
45 typedef bidirectional_iterator
<const char*> Bi
;
47 Out r
= std::regex_replace(Out(buf
), Bi(std::begin(phone_book
)),
48 Bi(std::end(phone_book
)-1), phone_numbers
,
50 std::regex_constants::format_sed
);
51 assert(base(r
) == buf
+43);
52 assert(buf
== std::string("123-$555-1234, 123-$555-2345, 123-$555-3456"));
55 std::regex
phone_numbers("\\d{3}-\\d{4}");
56 const char phone_book
[] = "555-1234, 555-2345, 555-3456";
57 typedef cpp17_output_iterator
<char*> Out
;
58 typedef bidirectional_iterator
<const char*> Bi
;
60 Out r
= std::regex_replace(Out(buf
), Bi(std::begin(phone_book
)),
61 Bi(std::end(phone_book
)-1), phone_numbers
,
63 std::regex_constants::format_sed
);
64 assert(base(r
) == buf
+40);
65 assert(buf
== std::string("123-555-1234, 123-555-2345, 123-555-3456"));
68 std::regex
phone_numbers("\\d{3}-\\d{4}");
69 const char phone_book
[] = "555-1234, 555-2345, 555-3456";
70 typedef cpp17_output_iterator
<char*> Out
;
71 typedef bidirectional_iterator
<const char*> Bi
;
73 Out r
= std::regex_replace(Out(buf
), Bi(std::begin(phone_book
)),
74 Bi(std::end(phone_book
)-1), phone_numbers
,
76 std::regex_constants::format_no_copy
);
77 assert(base(r
) == buf
+36);
78 assert(buf
== std::string("123-555-1234123-555-2345123-555-3456"));
81 std::regex
phone_numbers("\\d{3}-\\d{4}");
82 const char phone_book
[] = "555-1234, 555-2345, 555-3456";
83 typedef cpp17_output_iterator
<char*> Out
;
84 typedef bidirectional_iterator
<const char*> Bi
;
86 Out r
= std::regex_replace(Out(buf
), Bi(std::begin(phone_book
)),
87 Bi(std::end(phone_book
)-1), phone_numbers
,
89 std::regex_constants::format_first_only
);
90 assert(base(r
) == buf
+32);
91 assert(buf
== std::string("123-555-1234, 555-2345, 555-3456"));
94 std::regex
phone_numbers("\\d{3}-\\d{4}");
95 const char phone_book
[] = "555-1234, 555-2345, 555-3456";
96 typedef cpp17_output_iterator
<char*> Out
;
97 typedef bidirectional_iterator
<const char*> Bi
;
99 Out r
= std::regex_replace(Out(buf
), Bi(std::begin(phone_book
)),
100 Bi(std::end(phone_book
)-1), phone_numbers
,
102 std::regex_constants::format_first_only
|
103 std::regex_constants::format_no_copy
);
104 assert(base(r
) == buf
+12);
105 assert(buf
== std::string("123-555-1234"));