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_iterators.h"
23 
24 int main()
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 
43         char out[100] = {0};
44         const char fmt[] = "prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
45         char* r = m.format(output_iterator<char*>(out),
46                     fmt, fmt + std::char_traits<char>::length(fmt),
47                     std::regex_constants::format_sed).base();
48         assert(r == out + 59);
49         assert(std::string(out) == "prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
50     }
51     {
52         std::match_results<const char*> m;
53         const char s[] = "abcdefghijk";
54         assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
55 
56         char out[100] = {0};
57         const char fmt[] = "match: &, m[1]: \\1, m[2]: \\2";
58         char* r = m.format(output_iterator<char*>(out),
59                     fmt, fmt + std::char_traits<char>::length(fmt),
60                     std::regex_constants::format_sed).base();
61         assert(r == out + 34);
62         assert(std::string(out) == "match: cdefghi, m[1]: efg, m[2]: e");
63     }
64 
65     {
66         std::match_results<const wchar_t*> m;
67         const wchar_t s[] = L"abcdefghijk";
68         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
69 
70         wchar_t out[100] = {0};
71         const wchar_t fmt[] = L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
72         wchar_t* r = m.format(output_iterator<wchar_t*>(out),
73                     fmt, fmt + std::char_traits<wchar_t>::length(fmt)).base();
74         assert(r == out + 58);
75         assert(std::wstring(out) == L"prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
76     }
77     {
78         std::match_results<const wchar_t*> m;
79         const wchar_t s[] = L"abcdefghijk";
80         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
81 
82         wchar_t out[100] = {0};
83         const wchar_t fmt[] = L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
84         wchar_t* r = m.format(output_iterator<wchar_t*>(out),
85                     fmt, fmt + std::char_traits<wchar_t>::length(fmt),
86                     std::regex_constants::format_sed).base();
87         assert(r == out + 59);
88         assert(std::wstring(out) == L"prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
89     }
90     {
91         std::match_results<const wchar_t*> m;
92         const wchar_t s[] = L"abcdefghijk";
93         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
94 
95         wchar_t out[100] = {0};
96         const wchar_t fmt[] = L"match: &, m[1]: \\1, m[2]: \\2";
97         wchar_t* r = m.format(output_iterator<wchar_t*>(out),
98                     fmt, fmt + std::char_traits<wchar_t>::length(fmt),
99                     std::regex_constants::format_sed).base();
100         assert(r == out + 34);
101         assert(std::wstring(out) == L"match: cdefghi, m[1]: efg, m[2]: e");
102     }
103 }
104