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