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 ST, class SA>
14// basic_string<char_type, ST, SA>
15// format(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_allocator.h"
23
24int main(int, char**)
25{
26 typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > nstr;
27 {
28 std::match_results<const char*> m;
29 const char s[] = "abcdefghijk";
30 assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
31
32 nstr fmt("prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
33 nstr out = m.format(fmt);
34 assert(out == "prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
35 }
36 {
37 std::match_results<const char*> m;
38 const char s[] = "abcdefghijk";
39 assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
40
41 nstr fmt("prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
42 nstr out = m.format(fmt, std::regex_constants::format_sed);
43 assert(out == "prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
44 }
45 {
46 std::match_results<const char*> m;
47 const char s[] = "abcdefghijk";
48 assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
49
50 nstr fmt("match: &, m[1]: \\1, m[2]: \\2");
51 nstr out = m.format(fmt, std::regex_constants::format_sed);
52 assert(out == "match: cdefghi, m[1]: efg, m[2]: e");
53 }
54
55#ifndef TEST_HAS_NO_WIDE_CHARACTERS
56 typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t> > wstr;
57 {
58 std::match_results<const wchar_t*> m;
59 const wchar_t s[] = L"abcdefghijk";
60 assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
61
62 wstr fmt(L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
63 wstr out = m.format(fmt);
64 assert(out == L"prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
65 }
66 {
67 std::match_results<const wchar_t*> m;
68 const wchar_t s[] = L"abcdefghijk";
69 assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
70
71 wstr fmt(L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
72 wstr out = m.format(fmt, std::regex_constants::format_sed);
73 assert(out == L"prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
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 wstr fmt(L"match: &, m[1]: \\1, m[2]: \\2");
81 wstr out = m.format(fmt, std::regex_constants::format_sed);
82 assert(out == L"match: cdefghi, m[1]: efg, m[2]: e");
83 }
84#endif // TEST_HAS_NO_WIDE_CHARACTERS
85
86 return 0;
87}
88

source code of libcxx/test/std/re/re.results/re.results.form/form3.pass.cpp