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_macros.h"
25 #include "test_allocator.h"
26 
main()27 int main()
28 {
29     typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > nstr;
30     typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t> > wstr;
31     {
32         std::match_results<const char*> m;
33         const char s[] = "abcdefghijk";
34         assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
35 
36         nstr fmt("prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
37         nstr out = m.format(fmt);
38         assert(out == "prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
39     }
40     {
41         std::match_results<const char*> m;
42         const char s[] = "abcdefghijk";
43         assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
44 
45         nstr fmt("prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
46         nstr out = m.format(fmt, std::regex_constants::format_sed);
47         assert(out == "prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
48     }
49     {
50         std::match_results<const char*> m;
51         const char s[] = "abcdefghijk";
52         assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
53 
54         nstr fmt("match: &, m[1]: \\1, m[2]: \\2");
55         nstr out = m.format(fmt, std::regex_constants::format_sed);
56         assert(out == "match: cdefghi, m[1]: efg, m[2]: e");
57     }
58 
59     {
60         std::match_results<const wchar_t*> m;
61         const wchar_t s[] = L"abcdefghijk";
62         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
63 
64         wstr fmt(L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
65         wstr out = m.format(fmt);
66         assert(out == L"prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
67     }
68     {
69         std::match_results<const wchar_t*> m;
70         const wchar_t s[] = L"abcdefghijk";
71         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
72 
73         wstr fmt(L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
74         wstr out = m.format(fmt, std::regex_constants::format_sed);
75         assert(out == L"prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
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         wstr fmt(L"match: &, m[1]: \\1, m[2]: \\2");
83         wstr out = m.format(fmt, std::regex_constants::format_sed);
84         assert(out == L"match: cdefghi, m[1]: efg, m[2]: e");
85     }
86 }
87