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