• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // string_type
15 //   format(const char_type* fmt,
16 //          regex_constants::match_flag_type flags = regex_constants::format_default) const;
17 
18 #include <iostream>
19 
20 #include <regex>
21 #include <cassert>
22 #include "test_macros.h"
23 
main()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         const char fmt[] = "prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
32         std::string out = m.format(fmt);
33         assert(out == "prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
34     }
35     {
36         std::match_results<const char*> m;
37         const char s[] = "abcdefghijk";
38         assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
39 
40         const char fmt[] = "prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
41         std::string out = m.format(fmt, std::regex_constants::format_sed);
42         assert(out == "prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
43     }
44     {
45         std::match_results<const char*> m;
46         const char s[] = "abcdefghijk";
47         assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
48 
49         const char fmt[] = "match: &, m[1]: \\1, m[2]: \\2";
50         std::string out = m.format(fmt, std::regex_constants::format_sed);
51         assert(out == "match: cdefghi, m[1]: efg, m[2]: e");
52     }
53 
54     {
55         std::match_results<const wchar_t*> m;
56         const wchar_t s[] = L"abcdefghijk";
57         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
58 
59         const wchar_t fmt[] = L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
60         std::wstring out = m.format(fmt);
61         assert(out == L"prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
62     }
63     {
64         std::match_results<const wchar_t*> m;
65         const wchar_t s[] = L"abcdefghijk";
66         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
67 
68         const wchar_t fmt[] = L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
69         std::wstring out = m.format(fmt, std::regex_constants::format_sed);
70         assert(out == L"prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
71     }
72     {
73         std::match_results<const wchar_t*> m;
74         const wchar_t s[] = L"abcdefghijk";
75         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
76 
77         const wchar_t fmt[] = L"match: &, m[1]: \\1, m[2]: \\2";
78         std::wstring out = m.format(fmt, std::regex_constants::format_sed);
79         assert(out == L"match: cdefghi, m[1]: efg, m[2]: e");
80     }
81 }
82