xref: /llvm-project/libcxx/test/std/re/re.results/re.results.form/form4.pass.cpp (revision f4c1258d5633fcf06385ff3fd1f4bf57ab971964)
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 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
52     {
53         std::match_results<const wchar_t*> m;
54         const wchar_t s[] = L"abcdefghijk";
55         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
56 
57         const wchar_t fmt[] = L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
58         std::wstring out = m.format(fmt);
59         assert(out == L"prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
60     }
61     {
62         std::match_results<const wchar_t*> m;
63         const wchar_t s[] = L"abcdefghijk";
64         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
65 
66         const wchar_t fmt[] = L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
67         std::wstring out = m.format(fmt, std::regex_constants::format_sed);
68         assert(out == L"prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
69     }
70     {
71         std::match_results<const wchar_t*> m;
72         const wchar_t s[] = L"abcdefghijk";
73         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
74 
75         const wchar_t fmt[] = L"match: &, m[1]: \\1, m[2]: \\2";
76         std::wstring out = m.format(fmt, std::regex_constants::format_sed);
77         assert(out == L"match: cdefghi, m[1]: efg, m[2]: e");
78     }
79 #endif // TEST_HAS_NO_WIDE_CHARACTERS
80 
81   return 0;
82 }
83