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 typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t> > wstr;
29 {
30 std::match_results<const char*> m;
31 const char s[] = "abcdefghijk";
32 assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
33
34 char out[100] = {0};
35 nstr fmt("prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
36 char* r = m.format(output_iterator<char*>(out), fmt).base();
37 assert(r == out + 58);
38 assert(std::string(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 char out[100] = {0};
46 nstr fmt("prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
47 char* r = m.format(output_iterator<char*>(out),
48 fmt, std::regex_constants::format_sed).base();
49 assert(r == out + 59);
50 assert(std::string(out) == "prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
51 }
52 {
53 std::match_results<const char*> m;
54 const char s[] = "abcdefghijk";
55 assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
56
57 char out[100] = {0};
58 nstr fmt("match: &, m[1]: \\1, m[2]: \\2");
59 char* r = m.format(output_iterator<char*>(out),
60 fmt, std::regex_constants::format_sed).base();
61 assert(r == out + 34);
62 assert(std::string(out) == "match: cdefghi, m[1]: efg, m[2]: e");
63 }
64
65 {
66 std::match_results<const wchar_t*> m;
67 const wchar_t s[] = L"abcdefghijk";
68 assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
69
70 wchar_t out[100] = {0};
71 wstr fmt(L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
72 wchar_t* r = m.format(output_iterator<wchar_t*>(out), fmt).base();
73 assert(r == out + 58);
74 assert(std::wstring(out) == L"prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
75 }
76 {
77 std::match_results<const wchar_t*> m;
78 const wchar_t s[] = L"abcdefghijk";
79 assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
80
81 wchar_t out[100] = {0};
82 wstr fmt(L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
83 wchar_t* r = m.format(output_iterator<wchar_t*>(out),
84 fmt, std::regex_constants::format_sed).base();
85 assert(r == out + 59);
86 assert(std::wstring(out) == L"prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
87 }
88 {
89 std::match_results<const wchar_t*> m;
90 const wchar_t s[] = L"abcdefghijk";
91 assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
92
93 wchar_t out[100] = {0};
94 wstr fmt(L"match: &, m[1]: \\1, m[2]: \\2");
95 wchar_t* r = m.format(output_iterator<wchar_t*>(out),
96 fmt, std::regex_constants::format_sed).base();
97 assert(r == out + 34);
98 assert(std::wstring(out) == L"match: cdefghi, m[1]: efg, m[2]: e");
99 }
100
101 return 0;
102 }
103