1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <regex>
11
12 // class match_results<BidirectionalIterator, Allocator>
13
14 // template <class OutputIter>
15 // OutputIter
16 // format(OutputIter out, const char_type* fmt_first, const char_type* fmt_last,
17 // regex_constants::match_flag_type flags = regex_constants::format_default) const;
18
19 #include <regex>
20 #include <cassert>
21
22 #include "test_macros.h"
23 #include "test_iterators.h"
24
main()25 int main()
26 {
27 {
28 std::match_results<const char*> m;
29 const char s[] = "abcdefghijk";
30 assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
31
32 char out[100] = {0};
33 const char fmt[] = "prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
34 char* r = m.format(output_iterator<char*>(out),
35 fmt, fmt + std::char_traits<char>::length(fmt)).base();
36 assert(r == out + 58);
37 assert(std::string(out) == "prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
38 }
39 {
40 std::match_results<const char*> m;
41 const char s[] = "abcdefghijk";
42 assert(std::regex_search(s, m, std::regex("cd((e)fg)hi",
43 std::regex_constants::nosubs)));
44
45 char out[100] = {0};
46 const char fmt[] = "prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
47 char* r = m.format(output_iterator<char*>(out),
48 fmt, fmt + std::char_traits<char>::length(fmt)).base();
49 assert(r == out + 54);
50 assert(std::string(out) == "prefix: ab, match: cdefghi, suffix: jk, m[1]: , m[2]: ");
51 }
52 {
53 std::match_results<const char*> m;
54 const char s[] = "abcdefghijk";
55 assert(std::regex_search(s, m, std::regex("cdefghi")));
56
57 char out[100] = {0};
58 const char fmt[] = "prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
59 char* r = m.format(output_iterator<char*>(out),
60 fmt, fmt + std::char_traits<char>::length(fmt)).base();
61 assert(r == out + 54);
62 assert(std::string(out) == "prefix: ab, match: cdefghi, suffix: jk, m[1]: , m[2]: ");
63 }
64 {
65 std::match_results<const char*> m;
66 const char s[] = "abcdefghijk";
67 assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
68
69 char out[100] = {0};
70 const char fmt[] = "prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
71 char* r = m.format(output_iterator<char*>(out),
72 fmt, fmt + std::char_traits<char>::length(fmt),
73 std::regex_constants::format_sed).base();
74 assert(r == out + 59);
75 assert(std::string(out) == "prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
76 }
77 {
78 std::match_results<const char*> m;
79 const char s[] = "abcdefghijk";
80 assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
81
82 char out[100] = {0};
83 const char fmt[] = "match: &, m[1]: \\1, m[2]: \\2";
84 char* r = m.format(output_iterator<char*>(out),
85 fmt, fmt + std::char_traits<char>::length(fmt),
86 std::regex_constants::format_sed).base();
87 assert(r == out + 34);
88 assert(std::string(out) == "match: cdefghi, m[1]: efg, m[2]: e");
89 }
90 {
91 std::match_results<const char*> m;
92 const char s[] = "abcdefghijk";
93 assert(std::regex_search(s, m, std::regex("cd((e)fg)hi",
94 std::regex_constants::nosubs)));
95
96 char out[100] = {0};
97 const char fmt[] = "match: &, m[1]: \\1, m[2]: \\2";
98 char* r = m.format(output_iterator<char*>(out),
99 fmt, fmt + std::char_traits<char>::length(fmt),
100 std::regex_constants::format_sed).base();
101 assert(r == out + 30);
102 assert(std::string(out) == "match: cdefghi, m[1]: , m[2]: ");
103 }
104 {
105 std::match_results<const char*> m;
106 const char s[] = "abcdefghijk";
107 assert(std::regex_search(s, m, std::regex("cdefghi")));
108
109 char out[100] = {0};
110 const char fmt[] = "match: &, m[1]: \\1, m[2]: \\2";
111 char* r = m.format(output_iterator<char*>(out),
112 fmt, fmt + std::char_traits<char>::length(fmt),
113 std::regex_constants::format_sed).base();
114 assert(r == out + 30);
115 assert(std::string(out) == "match: cdefghi, m[1]: , m[2]: ");
116 }
117
118 {
119 std::match_results<const wchar_t*> m;
120 const wchar_t s[] = L"abcdefghijk";
121 assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
122
123 wchar_t out[100] = {0};
124 const wchar_t fmt[] = L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
125 wchar_t* r = m.format(output_iterator<wchar_t*>(out),
126 fmt, fmt + std::char_traits<wchar_t>::length(fmt)).base();
127 assert(r == out + 58);
128 assert(std::wstring(out) == L"prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
129 }
130 {
131 std::match_results<const wchar_t*> m;
132 const wchar_t s[] = L"abcdefghijk";
133 assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
134
135 wchar_t out[100] = {0};
136 const wchar_t fmt[] = L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
137 wchar_t* r = m.format(output_iterator<wchar_t*>(out),
138 fmt, fmt + std::char_traits<wchar_t>::length(fmt),
139 std::regex_constants::format_sed).base();
140 assert(r == out + 59);
141 assert(std::wstring(out) == L"prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
142 }
143 {
144 std::match_results<const wchar_t*> m;
145 const wchar_t s[] = L"abcdefghijk";
146 assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
147
148 wchar_t out[100] = {0};
149 const wchar_t fmt[] = L"match: &, m[1]: \\1, m[2]: \\2";
150 wchar_t* r = m.format(output_iterator<wchar_t*>(out),
151 fmt, fmt + std::char_traits<wchar_t>::length(fmt),
152 std::regex_constants::format_sed).base();
153 assert(r == out + 34);
154 assert(std::wstring(out) == L"match: cdefghi, m[1]: efg, m[2]: e");
155 }
156 }
157