xref: /llvm-project/libcxx/test/std/re/re.results/re.results.form/form2.pass.cpp (revision 5f26d8636f506b487962cd2a9b0e32940e93fa9b)
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 // template <class OutputIter, class ST, class SA>
14 //   OutputIter
15 //   format(OutputIter out, const basic_string<char_type, ST, SA>& fmt,
16 //          regex_constants::match_flag_type flags = regex_constants::format_default) const;
17 
18 #include <regex>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 #include "test_iterators.h"
23 #include "test_allocator.h"
24 
main(int,char **)25 int main(int, char**)
26 {
27     typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > nstr;
28     {
29         std::match_results<const char*> m;
30         const char s[] = "abcdefghijk";
31         assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
32 
33         char out[100] = {0};
34         nstr fmt("prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
35         auto r = m.format(cpp17_output_iterator<char*>(out), fmt);
36         assert(base(r) == out + 58);
37         assert(std::string(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         char out[100] = {0};
45         nstr fmt("prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
46         auto r = m.format(cpp17_output_iterator<char*>(out), fmt, std::regex_constants::format_sed);
47         assert(base(r) == out + 59);
48         assert(std::string(out) == "prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
49     }
50     {
51         std::match_results<const char*> m;
52         const char s[] = "abcdefghijk";
53         assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
54 
55         char out[100] = {0};
56         nstr fmt("match: &, m[1]: \\1, m[2]: \\2");
57         auto r = m.format(cpp17_output_iterator<char*>(out), fmt, std::regex_constants::format_sed);
58         assert(base(r) == out + 34);
59         assert(std::string(out) == "match: cdefghi, m[1]: efg, m[2]: e");
60     }
61 
62 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
63     typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t> > wstr;
64     {
65         std::match_results<const wchar_t*> m;
66         const wchar_t s[] = L"abcdefghijk";
67         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
68 
69         wchar_t out[100] = {0};
70         wstr fmt(L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
71         auto r = m.format(cpp17_output_iterator<wchar_t*>(out), fmt);
72         assert(base(r) == out + 58);
73         assert(std::wstring(out) == L"prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
74     }
75     {
76         std::match_results<const wchar_t*> m;
77         const wchar_t s[] = L"abcdefghijk";
78         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
79 
80         wchar_t out[100] = {0};
81         wstr fmt(L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
82         auto r = m.format(cpp17_output_iterator<wchar_t*>(out), fmt, std::regex_constants::format_sed);
83         assert(base(r) == out + 59);
84         assert(std::wstring(out) == L"prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
85     }
86     {
87         std::match_results<const wchar_t*> m;
88         const wchar_t s[] = L"abcdefghijk";
89         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
90 
91         wchar_t out[100] = {0};
92         wstr fmt(L"match: &, m[1]: \\1, m[2]: \\2");
93         auto r = m.format(cpp17_output_iterator<wchar_t*>(out), fmt, std::regex_constants::format_sed);
94         assert(base(r) == out + 34);
95         assert(std::wstring(out) == L"match: cdefghi, m[1]: efg, m[2]: e");
96     }
97 #endif // TEST_HAS_NO_WIDE_CHARACTERS
98 
99   return 0;
100 }
101