• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <regex>
11 
12 // class match_results<BidirectionalIterator, Allocator>
13 
14 // template <class OutputIter, class ST, class SA>
15 //   OutputIter
16 //   format(OutputIter out, const basic_string<char_type, ST, SA>& fmt,
17 //          regex_constants::match_flag_type flags = regex_constants::format_default) const;
18 
19 #include <iostream>
20 
21 #include <regex>
22 #include <cassert>
23 
24 #include "test_macros.h"
25 #include "test_iterators.h"
26 #include "test_allocator.h"
27 
main()28 int main()
29 {
30     typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > nstr;
31     typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t> > wstr;
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         char out[100] = {0};
38         nstr fmt("prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
39         char* r = m.format(output_iterator<char*>(out), fmt).base();
40         assert(r == out + 58);
41         assert(std::string(out) == "prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
42     }
43     {
44         std::match_results<const char*> m;
45         const char s[] = "abcdefghijk";
46         assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
47 
48         char out[100] = {0};
49         nstr fmt("prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
50         char* r = m.format(output_iterator<char*>(out),
51                     fmt, std::regex_constants::format_sed).base();
52         assert(r == out + 59);
53         assert(std::string(out) == "prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
54     }
55     {
56         std::match_results<const char*> m;
57         const char s[] = "abcdefghijk";
58         assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
59 
60         char out[100] = {0};
61         nstr fmt("match: &, m[1]: \\1, m[2]: \\2");
62         char* r = m.format(output_iterator<char*>(out),
63                     fmt, std::regex_constants::format_sed).base();
64         assert(r == out + 34);
65         assert(std::string(out) == "match: cdefghi, m[1]: efg, m[2]: e");
66     }
67 
68     {
69         std::match_results<const wchar_t*> m;
70         const wchar_t s[] = L"abcdefghijk";
71         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
72 
73         wchar_t out[100] = {0};
74         wstr fmt(L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
75         wchar_t* r = m.format(output_iterator<wchar_t*>(out), fmt).base();
76         assert(r == out + 58);
77         assert(std::wstring(out) == L"prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
78     }
79     {
80         std::match_results<const wchar_t*> m;
81         const wchar_t s[] = L"abcdefghijk";
82         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
83 
84         wchar_t out[100] = {0};
85         wstr fmt(L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2");
86         wchar_t* r = m.format(output_iterator<wchar_t*>(out),
87                     fmt, std::regex_constants::format_sed).base();
88         assert(r == out + 59);
89         assert(std::wstring(out) == L"prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
90     }
91     {
92         std::match_results<const wchar_t*> m;
93         const wchar_t s[] = L"abcdefghijk";
94         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
95 
96         wchar_t out[100] = {0};
97         wstr fmt(L"match: &, m[1]: \\1, m[2]: \\2");
98         wchar_t* r = m.format(output_iterator<wchar_t*>(out),
99                     fmt, std::regex_constants::format_sed).base();
100         assert(r == out + 34);
101         assert(std::wstring(out) == L"match: cdefghi, m[1]: efg, m[2]: e");
102     }
103 }
104