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 ST, class SA>
15 //   basic_string<char_type, ST, SA>
16 //   format(const basic_string<char_type, ST, SA>& fmt,
17 //          regex_constants::match_flag_type flags = regex_constants::format_default) const;
18 
19 #include <iostream>
20 
21 #include <regex>
22 #include <cassert>
23 
24 #include "test_allocator.h"
25 
main()26 int main()
27 {
28     typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > nstr;
29     typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t> > wstr;
30     {
31         std::match_results<const char*> m;
32         const char s[] = "abcdefghijk";
33         assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
34 
35         nstr fmt("prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
36         nstr out = m.format(fmt);
37         assert(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 
44         nstr fmt("prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
45         nstr out = m.format(fmt, std::regex_constants::format_sed);
46         assert(out == "prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
47     }
48     {
49         std::match_results<const char*> m;
50         const char s[] = "abcdefghijk";
51         assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
52 
53         nstr fmt("match: &, m[1]: \\1, m[2]: \\2");
54         nstr out = m.format(fmt, std::regex_constants::format_sed);
55         assert(out == "match: cdefghi, m[1]: efg, m[2]: e");
56     }
57 
58     {
59         std::match_results<const wchar_t*> m;
60         const wchar_t s[] = L"abcdefghijk";
61         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
62 
63         wstr fmt(L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
64         wstr out = m.format(fmt);
65         assert(out == L"prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
66     }
67     {
68         std::match_results<const wchar_t*> m;
69         const wchar_t s[] = L"abcdefghijk";
70         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
71 
72         wstr fmt(L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
73         wstr out = m.format(fmt, std::regex_constants::format_sed);
74         assert(out == L"prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
75     }
76     {
77         std::match_results<const wchar_t*> m;
78         const wchar_t s[] = L"abcdefghijk";
79         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
80 
81         wstr fmt(L"match: &, m[1]: \\1, m[2]: \\2");
82         wstr out = m.format(fmt, std::regex_constants::format_sed);
83         assert(out == L"match: cdefghi, m[1]: efg, m[2]: e");
84     }
85 }
86