aboutsummaryrefslogtreecommitdiffstats
path: root/test/std/re/re.results/re.results.form/form1.pass.cpp
blob: 5b04c684fef1a30ff19b679023593df130d15f6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// <regex>

// class match_results<BidirectionalIterator, Allocator>

// template <class OutputIter>
//   OutputIter
//   format(OutputIter out, const char_type* fmt_first, const char_type* fmt_last,
//          regex_constants::match_flag_type flags = regex_constants::format_default) const;

#include <regex>
#include <cassert>

#include "test_iterators.h"

int main()
{
    {
        std::match_results<const char*> m;
        const char s[] = "abcdefghijk";
        assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));

        char out[100] = {0};
        const char fmt[] = "prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
        char* r = m.format(output_iterator<char*>(out),
                    fmt, fmt + std::char_traits<char>::length(fmt)).base();
        assert(r == out + 58);
        assert(std::string(out) == "prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
    }
    {
        std::match_results<const char*> m;
        const char s[] = "abcdefghijk";
        assert(std::regex_search(s, m, std::regex("cd((e)fg)hi",
                                                  std::regex_constants::nosubs)));

        char out[100] = {0};
        const char fmt[] = "prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
        char* r = m.format(output_iterator<char*>(out),
                    fmt, fmt + std::char_traits<char>::length(fmt)).base();
        assert(r == out + 54);
        assert(std::string(out) == "prefix: ab, match: cdefghi, suffix: jk, m[1]: , m[2]: ");
    }
    {
        std::match_results<const char*> m;
        const char s[] = "abcdefghijk";
        assert(std::regex_search(s, m, std::regex("cdefghi")));

        char out[100] = {0};
        const char fmt[] = "prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
        char* r = m.format(output_iterator<char*>(out),
                    fmt, fmt + std::char_traits<char>::length(fmt)).base();
        assert(r == out + 54);
        assert(std::string(out) == "prefix: ab, match: cdefghi, suffix: jk, m[1]: , m[2]: ");
    }
    {
        std::match_results<const char*> m;
        const char s[] = "abcdefghijk";
        assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));

        char out[100] = {0};
        const char fmt[] = "prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
        char* r = m.format(output_iterator<char*>(out),
                    fmt, fmt + std::char_traits<char>::length(fmt),
                    std::regex_constants::format_sed).base();
        assert(r == out + 59);
        assert(std::string(out) == "prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
    }
    {
        std::match_results<const char*> m;
        const char s[] = "abcdefghijk";
        assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));

        char out[100] = {0};
        const char fmt[] = "match: &, m[1]: \\1, m[2]: \\2";
        char* r = m.format(output_iterator<char*>(out),
                    fmt, fmt + std::char_traits<char>::length(fmt),
                    std::regex_constants::format_sed).base();
        assert(r == out + 34);
        assert(std::string(out) == "match: cdefghi, m[1]: efg, m[2]: e");
    }
    {
        std::match_results<const char*> m;
        const char s[] = "abcdefghijk";
        assert(std::regex_search(s, m, std::regex("cd((e)fg)hi",
                                                  std::regex_constants::nosubs)));

        char out[100] = {0};
        const char fmt[] = "match: &, m[1]: \\1, m[2]: \\2";
        char* r = m.format(output_iterator<char*>(out),
                    fmt, fmt + std::char_traits<char>::length(fmt),
                    std::regex_constants::format_sed).base();
        assert(r == out + 30);
        assert(std::string(out) == "match: cdefghi, m[1]: , m[2]: ");
    }
    {
        std::match_results<const char*> m;
        const char s[] = "abcdefghijk";
        assert(std::regex_search(s, m, std::regex("cdefghi")));

        char out[100] = {0};
        const char fmt[] = "match: &, m[1]: \\1, m[2]: \\2";
        char* r = m.format(output_iterator<char*>(out),
                    fmt, fmt + std::char_traits<char>::length(fmt),
                    std::regex_constants::format_sed).base();
        assert(r == out + 30);
        assert(std::string(out) == "match: cdefghi, m[1]: , m[2]: ");
    }

    {
        std::match_results<const wchar_t*> m;
        const wchar_t s[] = L"abcdefghijk";
        assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));

        wchar_t out[100] = {0};
        const wchar_t fmt[] = L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
        wchar_t* r = m.format(output_iterator<wchar_t*>(out),
                    fmt, fmt + std::char_traits<wchar_t>::length(fmt)).base();
        assert(r == out + 58);
        assert(std::wstring(out) == L"prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
    }
    {
        std::match_results<const wchar_t*> m;
        const wchar_t s[] = L"abcdefghijk";
        assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));

        wchar_t out[100] = {0};
        const wchar_t fmt[] = L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
        wchar_t* r = m.format(output_iterator<wchar_t*>(out),
                    fmt, fmt + std::char_traits<wchar_t>::length(fmt),
                    std::regex_constants::format_sed).base();
        assert(r == out + 59);
        assert(std::wstring(out) == L"prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
    }
    {
        std::match_results<const wchar_t*> m;
        const wchar_t s[] = L"abcdefghijk";
        assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));

        wchar_t out[100] = {0};
        const wchar_t fmt[] = L"match: &, m[1]: \\1, m[2]: \\2";
        wchar_t* r = m.format(output_iterator<wchar_t*>(out),
                    fmt, fmt + std::char_traits<wchar_t>::length(fmt),
                    std::regex_constants::format_sed).base();
        assert(r == out + 34);
        assert(std::wstring(out) == L"match: cdefghi, m[1]: efg, m[2]: e");
    }
}