File: split.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (275 lines) | stat: -rw-r--r-- 9,199 bytes parent folder | download | duplicates (4)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
 *   Copyright (C) 2024 T. Zachary Laine
 *
 *   Distributed under the Boost Software License, Version 1.0. (See
 *   accompanying file LICENSE_1_0.txt or copy at
 *   http://www.boost.org/LICENSE_1_0.txt)
 */

#include <boost/parser/split.hpp>

#include <boost/core/lightweight_test.hpp>


namespace bp = boost::parser;

#if BOOST_PARSER_USE_CONCEPTS
namespace deduction {
    std::string str;
    auto const parser = bp::char_;
    auto const skip = bp::ws;

    auto deduced_1 = bp::split_view(str, parser, skip, bp::trace::on);
    auto deduced_2 = bp::split_view(str, parser, skip);
    auto deduced_3 = bp::split_view(str, parser, bp::trace::on);
    auto deduced_4 = bp::split_view(str, parser);
}
#endif

int main()
{

// split_
{
    {
        auto r = bp::split("", bp::lit("XYZ"), bp::ws);
        int count = 0;
        for (auto subrange : r) {
            (void)subrange;
            ++count;
        }
        BOOST_TEST(count == 0);
    }
    {
        char const str[] = "aaXYZb";
        auto r = bp::split(str, bp::lit("XYZ"), bp::ws);
        int count = 0;
        int const offsets[] = {0, 2, 5, 6};
        for (auto subrange : r) {
            BOOST_TEST(subrange.begin() - str == offsets[count * 2 + 0]);
            BOOST_TEST(subrange.end() - str == offsets[count * 2 + 1]);
            ++count;
        }
        BOOST_TEST(count == 2);
    }
    {
        char const str[] = "aaXYZbaabaXYZ";
        auto r = str | bp::split(bp::lit("XYZ"), bp::ws, bp::trace::off);
        int count = 0;
        int const offsets[] = {0, 2, 5, 10, 13, 13};
        for (auto subrange : r) {
            BOOST_TEST(subrange.begin() - str == offsets[count * 2 + 0]);
            BOOST_TEST(subrange.end() - str == offsets[count * 2 + 1]);
            ++count;
        }
        BOOST_TEST(count == 3);
    }
    {
        char const str[] = "aaXYZbaabaXYZ";
        auto r = str | bp::split(bp::lit("XYZ"), bp::trace::off);
        int count = 0;
        int const offsets[] = {0, 2, 5, 10, 13, 13};
        for (auto subrange : r) {
            BOOST_TEST(subrange.begin() - str == offsets[count * 2 + 0]);
            BOOST_TEST(subrange.end() - str == offsets[count * 2 + 1]);
            ++count;
        }
        BOOST_TEST(count == 3);
    }
    {
        char const str[] = "aaXYZbaabaXYZ";
        auto r = str | bp::split(bp::lit("XYZ"));
        int count = 0;
        int const offsets[] = {0, 2, 5, 10, 13, 13};
        for (auto subrange : r) {
            BOOST_TEST(subrange.begin() - str == offsets[count * 2 + 0]);
            BOOST_TEST(subrange.end() - str == offsets[count * 2 + 1]);
            ++count;
        }
        BOOST_TEST(count == 3);
    }
    {
        char const str[] = "aaXYZbaabaXYZXYZ";
        auto r = str | bp::split(bp::lit("XYZ"));
        int count = 0;
        int const offsets[] = {0, 2, 5, 10, 13, 13, 16, 16};
        for (auto subrange : r) {
            BOOST_TEST(subrange.begin() - str == offsets[count * 2 + 0]);
            BOOST_TEST(subrange.end() - str == offsets[count * 2 + 1]);
            ++count;
        }
        BOOST_TEST(count == 4);
    }
    {
        char const str[] = "XYZaaXYZbaabaXYZXYZ";
        auto r = str | bp::split(bp::lit("XYZ"));
        int count = 0;
        int const offsets[] = {0, 0, 3, 5, 8, 13, 16, 16, 19, 19};
        for (auto subrange : r) {
            BOOST_TEST(subrange.begin() - str == offsets[count * 2 + 0]);
            BOOST_TEST(subrange.end() - str == offsets[count * 2 + 1]);
            ++count;
        }
        BOOST_TEST(count == 5);
    }
    {
        char const str[] = "XYZXYZaaXYZbaabaXYZXYZ";
        auto r = str | bp::split(bp::lit("XYZ"));
        int count = 0;
        int const offsets[] = {0, 0, 3, 3, 6, 8, 11, 16, 19, 19, 22, 22};
        for (auto subrange : r) {
            BOOST_TEST(subrange.begin() - str == offsets[count * 2 + 0]);
            BOOST_TEST(subrange.end() - str == offsets[count * 2 + 1]);
            ++count;
        }
        BOOST_TEST(count == 6);
    }
    {
        char const * str = "XYZXYZaaXYZbaabaXYZXYZ";
        auto r = bp::null_term(str) | bp::split(bp::lit("XYZ"));
        int count = 0;
        int const offsets[] = {0, 0, 3, 3, 6, 8, 11, 16, 19, 19, 22, 22};
        for (auto subrange : r) {
            BOOST_TEST(subrange.begin() - str == offsets[count * 2 + 0]);
            BOOST_TEST(subrange.end() - str == offsets[count * 2 + 1]);
            ++count;
        }
        BOOST_TEST(count == 6);
    }
    {
        char const * str = "XYZXYZaaXYZbaabaXYZXYZ";
        auto const r = bp::null_term(str) | bp::split(bp::lit("XYZ"));
        int count = 0;
        int const offsets[] = {0, 0, 3, 3, 6, 8, 11, 16, 19, 19, 22, 22};
        for (auto subrange : r) {
            BOOST_TEST(subrange.begin() - str == offsets[count * 2 + 0]);
            BOOST_TEST(subrange.end() - str == offsets[count * 2 + 1]);
            ++count;
        }
        BOOST_TEST(count == 6);
    }
}

// split_unicode
{
    {
        char const str_[] = "";
        auto str = str_ | bp::as_utf8;
        auto r = bp::split(str, bp::lit("XYZ"), bp::ws);
        int count = 0;
        for (auto subrange : r) {
            (void)subrange;
            ++count;
        }
        BOOST_TEST(count == 0);
    }
    {
        char const * str_ = "aaXYZb";
        auto str = bp::null_term(str_) | bp::as_utf16;
        auto r = bp::split(str, bp::lit("XYZ"), bp::ws);
        int count = 0;
        int const offsets[] = {0, 2, 5, 6};
        for (auto subrange : r) {
            BOOST_TEST(subrange.begin().base() - str_ == offsets[count * 2 + 0]);
            BOOST_TEST(subrange.end().base() - str_ == offsets[count * 2 + 1]);
            ++count;
        }
        BOOST_TEST(count == 2);
    }
    {
        char const str_[] = "aaXYZbaabaXYZ";
        auto str = str_ | bp::as_utf32;
        auto r = str | bp::split(bp::lit("XYZ"), bp::ws, bp::trace::off);
        int count = 0;
        int const offsets[] = {0, 2, 5, 10, 13, 13};
        for (auto subrange : r) {
            BOOST_TEST(subrange.begin().base() - str_ == offsets[count * 2 + 0]);
            BOOST_TEST(subrange.end().base() - str_ == offsets[count * 2 + 1]);
            ++count;
        }
        BOOST_TEST(count == 3);
    }
    {
        char const str_[] = "aaXYZbaabaXYZ";
        auto str = str_ | bp::as_utf8;
        const auto r = str | bp::split(bp::lit("XYZ"), bp::trace::off);
        int count = 0;
        int const offsets[] = {0, 2, 5, 10, 13, 13};
        for (auto subrange : r) {
            BOOST_TEST(subrange.begin().base() - str_ == offsets[count * 2 + 0]);
            BOOST_TEST(subrange.end().base() - str_ == offsets[count * 2 + 1]);
            ++count;
        }
        BOOST_TEST(count == 3);
    }
    {
        char const str_[] = "aaXYZbaabaXYZ";
        auto str = str_ | bp::as_utf16;
        auto r = str | bp::split(bp::lit("XYZ"));
        int count = 0;
        int const offsets[] = {0, 2, 5, 10, 13, 13};
        for (auto subrange : r) {
            BOOST_TEST(subrange.begin().base() - str_ == offsets[count * 2 + 0]);
            BOOST_TEST(subrange.end().base() - str_ == offsets[count * 2 + 1]);
            ++count;
        }
        BOOST_TEST(count == 3);
    }
    {
        char const str_[] = "aaXYZbaabaXYZXYZ";
        auto str = str_ | bp::as_utf32;
        auto r = str | bp::split(bp::lit("XYZ"));
        int count = 0;
        int const offsets[] = {0, 2, 5, 10, 13, 13, 16, 16};
        for (auto subrange : r) {
            BOOST_TEST(subrange.begin().base() - str_ == offsets[count * 2 + 0]);
            BOOST_TEST(subrange.end().base() - str_ == offsets[count * 2 + 1]);
            ++count;
        }
        BOOST_TEST(count == 4);
    }
    {
        char const str_[] = "XYZaaXYZbaabaXYZXYZ";
        auto str = str_ | bp::as_utf8;
        auto r = str | bp::split(bp::lit("XYZ"));
        int count = 0;
        int const offsets[] = {0, 0, 3, 5, 8, 13, 16, 16, 19, 19};
        for (auto subrange : r) {
            BOOST_TEST(subrange.begin().base() - str_ == offsets[count * 2 + 0]);
            BOOST_TEST(subrange.end().base() - str_ == offsets[count * 2 + 1]);
            ++count;
        }
        BOOST_TEST(count == 5);
    }
    {
        char const str_[] = "XYZXYZaaXYZbaabaXYZXYZ";
        auto str = str_ | bp::as_utf16;
        auto r = str | bp::split(bp::lit("XYZ"));
        int count = 0;
        int const offsets[] = {0, 0, 3, 3, 6, 8, 11, 16, 19, 19, 22, 22};
        for (auto subrange : r) {
            BOOST_TEST(subrange.begin().base() - str_ == offsets[count * 2 + 0]);
            BOOST_TEST(subrange.end().base() - str_ == offsets[count * 2 + 1]);
            ++count;
        }
        BOOST_TEST(count == 6);
    }
}

// doc_examples
{
    {
        auto r = "XYZaaXYZbaabaXYZXYZ" | bp::split(bp::lit("XYZ"));
        int count = 0;
        // Prints '' 'aa' 'baaba' '' ''.
        for (auto subrange : r) {
            std::cout << "'" << std::string_view(subrange.begin(), subrange.end() - subrange.begin()) << "' ";
            ++count;
        }
        std::cout << "\n";
        assert(count == 5);
    }
}

return boost::report_errors();
}