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