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