xref: /minix3/external/bsd/libc++/dist/libcxx/test/re/re.alg/re.alg.replace/test1.pass.cpp (revision 4684ddb6aab0b36791c8099bc705d6140b3d05d0)
1*4684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
2*4684ddb6SLionel Sambuc //
3*4684ddb6SLionel Sambuc //                     The LLVM Compiler Infrastructure
4*4684ddb6SLionel Sambuc //
5*4684ddb6SLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
6*4684ddb6SLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
7*4684ddb6SLionel Sambuc //
8*4684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
9*4684ddb6SLionel Sambuc 
10*4684ddb6SLionel Sambuc // <regex>
11*4684ddb6SLionel Sambuc 
12*4684ddb6SLionel Sambuc // template <class OutputIterator, class BidirectionalIterator,
13*4684ddb6SLionel Sambuc //           class traits, class charT, class ST, class SA>
14*4684ddb6SLionel Sambuc //     OutputIterator
15*4684ddb6SLionel Sambuc //     regex_replace(OutputIterator out,
16*4684ddb6SLionel Sambuc //                   BidirectionalIterator first, BidirectionalIterator last,
17*4684ddb6SLionel Sambuc //                   const basic_regex<charT, traits>& e,
18*4684ddb6SLionel Sambuc //                   const basic_string<charT, ST, SA>& fmt,
19*4684ddb6SLionel Sambuc //                   regex_constants::match_flag_type flags =
20*4684ddb6SLionel Sambuc //                                              regex_constants::match_default);
21*4684ddb6SLionel Sambuc 
22*4684ddb6SLionel Sambuc #include <regex>
23*4684ddb6SLionel Sambuc #include <cassert>
24*4684ddb6SLionel Sambuc 
25*4684ddb6SLionel Sambuc #include "test_iterators.h"
26*4684ddb6SLionel Sambuc 
main()27*4684ddb6SLionel Sambuc int main()
28*4684ddb6SLionel Sambuc {
29*4684ddb6SLionel Sambuc     {
30*4684ddb6SLionel Sambuc         std::regex phone_numbers("\\d{3}-\\d{4}");
31*4684ddb6SLionel Sambuc         const char phone_book[] = "555-1234, 555-2345, 555-3456";
32*4684ddb6SLionel Sambuc         typedef output_iterator<char*> Out;
33*4684ddb6SLionel Sambuc         typedef bidirectional_iterator<const char*> Bi;
34*4684ddb6SLionel Sambuc         char buf[100] = {0};
35*4684ddb6SLionel Sambuc         Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
36*4684ddb6SLionel Sambuc                                     Bi(std::end(phone_book)-1), phone_numbers,
37*4684ddb6SLionel Sambuc                                     std::string("123-$&"));
38*4684ddb6SLionel Sambuc         assert(r.base() == buf+40);
39*4684ddb6SLionel Sambuc         assert(buf == std::string("123-555-1234, 123-555-2345, 123-555-3456"));
40*4684ddb6SLionel Sambuc     }
41*4684ddb6SLionel Sambuc     {
42*4684ddb6SLionel Sambuc         std::regex phone_numbers("\\d{3}-\\d{4}");
43*4684ddb6SLionel Sambuc         const char phone_book[] = "555-1234, 555-2345, 555-3456";
44*4684ddb6SLionel Sambuc         typedef output_iterator<char*> Out;
45*4684ddb6SLionel Sambuc         typedef bidirectional_iterator<const char*> Bi;
46*4684ddb6SLionel Sambuc         char buf[100] = {0};
47*4684ddb6SLionel Sambuc         Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
48*4684ddb6SLionel Sambuc                                     Bi(std::end(phone_book)-1), phone_numbers,
49*4684ddb6SLionel Sambuc                                     std::string("123-$&"),
50*4684ddb6SLionel Sambuc                                     std::regex_constants::format_sed);
51*4684ddb6SLionel Sambuc         assert(r.base() == buf+43);
52*4684ddb6SLionel Sambuc         assert(buf == std::string("123-$555-1234, 123-$555-2345, 123-$555-3456"));
53*4684ddb6SLionel Sambuc     }
54*4684ddb6SLionel Sambuc     {
55*4684ddb6SLionel Sambuc         std::regex phone_numbers("\\d{3}-\\d{4}");
56*4684ddb6SLionel Sambuc         const char phone_book[] = "555-1234, 555-2345, 555-3456";
57*4684ddb6SLionel Sambuc         typedef output_iterator<char*> Out;
58*4684ddb6SLionel Sambuc         typedef bidirectional_iterator<const char*> Bi;
59*4684ddb6SLionel Sambuc         char buf[100] = {0};
60*4684ddb6SLionel Sambuc         Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
61*4684ddb6SLionel Sambuc                                     Bi(std::end(phone_book)-1), phone_numbers,
62*4684ddb6SLionel Sambuc                                     std::string("123-&"),
63*4684ddb6SLionel Sambuc                                     std::regex_constants::format_sed);
64*4684ddb6SLionel Sambuc         assert(r.base() == buf+40);
65*4684ddb6SLionel Sambuc         assert(buf == std::string("123-555-1234, 123-555-2345, 123-555-3456"));
66*4684ddb6SLionel Sambuc     }
67*4684ddb6SLionel Sambuc     {
68*4684ddb6SLionel Sambuc         std::regex phone_numbers("\\d{3}-\\d{4}");
69*4684ddb6SLionel Sambuc         const char phone_book[] = "555-1234, 555-2345, 555-3456";
70*4684ddb6SLionel Sambuc         typedef output_iterator<char*> Out;
71*4684ddb6SLionel Sambuc         typedef bidirectional_iterator<const char*> Bi;
72*4684ddb6SLionel Sambuc         char buf[100] = {0};
73*4684ddb6SLionel Sambuc         Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
74*4684ddb6SLionel Sambuc                                     Bi(std::end(phone_book)-1), phone_numbers,
75*4684ddb6SLionel Sambuc                                     std::string("123-$&"),
76*4684ddb6SLionel Sambuc                                     std::regex_constants::format_no_copy);
77*4684ddb6SLionel Sambuc         assert(r.base() == buf+36);
78*4684ddb6SLionel Sambuc         assert(buf == std::string("123-555-1234123-555-2345123-555-3456"));
79*4684ddb6SLionel Sambuc     }
80*4684ddb6SLionel Sambuc     {
81*4684ddb6SLionel Sambuc         std::regex phone_numbers("\\d{3}-\\d{4}");
82*4684ddb6SLionel Sambuc         const char phone_book[] = "555-1234, 555-2345, 555-3456";
83*4684ddb6SLionel Sambuc         typedef output_iterator<char*> Out;
84*4684ddb6SLionel Sambuc         typedef bidirectional_iterator<const char*> Bi;
85*4684ddb6SLionel Sambuc         char buf[100] = {0};
86*4684ddb6SLionel Sambuc         Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
87*4684ddb6SLionel Sambuc                                     Bi(std::end(phone_book)-1), phone_numbers,
88*4684ddb6SLionel Sambuc                                     std::string("123-$&"),
89*4684ddb6SLionel Sambuc                                     std::regex_constants::format_first_only);
90*4684ddb6SLionel Sambuc         assert(r.base() == buf+32);
91*4684ddb6SLionel Sambuc         assert(buf == std::string("123-555-1234, 555-2345, 555-3456"));
92*4684ddb6SLionel Sambuc     }
93*4684ddb6SLionel Sambuc     {
94*4684ddb6SLionel Sambuc         std::regex phone_numbers("\\d{3}-\\d{4}");
95*4684ddb6SLionel Sambuc         const char phone_book[] = "555-1234, 555-2345, 555-3456";
96*4684ddb6SLionel Sambuc         typedef output_iterator<char*> Out;
97*4684ddb6SLionel Sambuc         typedef bidirectional_iterator<const char*> Bi;
98*4684ddb6SLionel Sambuc         char buf[100] = {0};
99*4684ddb6SLionel Sambuc         Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
100*4684ddb6SLionel Sambuc                                     Bi(std::end(phone_book)-1), phone_numbers,
101*4684ddb6SLionel Sambuc                                     std::string("123-$&"),
102*4684ddb6SLionel Sambuc                                     std::regex_constants::format_first_only |
103*4684ddb6SLionel Sambuc                                     std::regex_constants::format_no_copy);
104*4684ddb6SLionel Sambuc         assert(r.base() == buf+12);
105*4684ddb6SLionel Sambuc         assert(buf == std::string("123-555-1234"));
106*4684ddb6SLionel Sambuc     }
107*4684ddb6SLionel Sambuc }
108