-
Notifications
You must be signed in to change notification settings - Fork 440
/
split.cpp
159 lines (146 loc) · 5.72 KB
/
split.cpp
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
// Range v3 library
//
// Copyright Eric Niebler 2014
//
// Use, modification and distribution is subject to 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)
//
// Project home: https://github.com/ericniebler/range-v3
#include <string>
#include <cctype>
#include <range/v3/core.hpp>
#include <range/v3/view/counted.hpp>
#include <range/v3/view/split.hpp>
#include <range/v3/view/empty.hpp>
#include <range/v3/view/remove_if.hpp>
#include "../simple_test.hpp"
#include "../test_utils.hpp"
#include "../test_iterators.hpp"
RANGES_DIAGNOSTIC_IGNORE_SIGN_CONVERSION
namespace
{
struct starts_with_g
{
template<typename I, typename S>
std::pair<bool, I> operator()(I b, S) const
{
return {*b == 'g', b};
}
};
template<std::size_t N>
ranges::iterator_range<char const*> c_str(char const (&sz)[N])
{
return {&sz[0], &sz[N-1]};
}
}
int main()
{
using namespace ranges;
{
std::string str("Now is the time for all good men to come to the aid of their country.");
auto rng = view::split(str, ' ');
CHECK(distance(rng) == 16);
if(distance(rng) == 16)
{
check_equal(*(next(begin(rng),0)), c_str("Now"));
check_equal(*(next(begin(rng),1)), c_str("is"));
check_equal(*(next(begin(rng),2)), c_str("the"));
check_equal(*(next(begin(rng),3)), c_str("time"));
check_equal(*(next(begin(rng),4)), c_str("for"));
check_equal(*(next(begin(rng),5)), c_str("all"));
check_equal(*(next(begin(rng),6)), c_str("good"));
check_equal(*(next(begin(rng),7)), c_str("men"));
check_equal(*(next(begin(rng),8)), c_str("to"));
check_equal(*(next(begin(rng),9)), c_str("come"));
check_equal(*(next(begin(rng),10)), c_str("to"));
check_equal(*(next(begin(rng),11)), c_str("the"));
check_equal(*(next(begin(rng),12)), c_str("aid"));
check_equal(*(next(begin(rng),13)), c_str("of"));
check_equal(*(next(begin(rng),14)), c_str("their"));
check_equal(*(next(begin(rng),15)), c_str("country."));
}
}
{
std::string str("Now is the time for all good men to come to the aid of their country.");
auto rng = view::split(str, c_str(" "));
CHECK(distance(rng) == 16);
if(distance(rng) == 16)
{
check_equal(*(next(begin(rng),0)), c_str("Now"));
check_equal(*(next(begin(rng),1)), c_str("is"));
check_equal(*(next(begin(rng),2)), c_str("the"));
check_equal(*(next(begin(rng),3)), c_str("time"));
check_equal(*(next(begin(rng),4)), c_str("for"));
check_equal(*(next(begin(rng),5)), c_str("all"));
check_equal(*(next(begin(rng),6)), c_str("good"));
check_equal(*(next(begin(rng),7)), c_str("men"));
check_equal(*(next(begin(rng),8)), c_str("to"));
check_equal(*(next(begin(rng),9)), c_str("come"));
check_equal(*(next(begin(rng),10)), c_str("to"));
check_equal(*(next(begin(rng),11)), c_str("the"));
check_equal(*(next(begin(rng),12)), c_str("aid"));
check_equal(*(next(begin(rng),13)), c_str("of"));
check_equal(*(next(begin(rng),14)), c_str("their"));
check_equal(*(next(begin(rng),15)), c_str("country."));
}
}
{
std::string str("Now is the time for all ggood men to come to the aid of their country.");
auto rng = view::split(str, starts_with_g{});
CHECK(distance(rng) == 3);
if(distance(rng) == 3)
{
check_equal(*begin(rng), c_str("Now is the time for all "));
check_equal(*next(begin(rng)), c_str("g"));
check_equal(*(next(begin(rng),2)), c_str("good men to come to the aid of their country."));
}
}
{
std::string str("Now is the time for all ggood men to come to the aid of their country.");
forward_iterator<std::string::iterator> i {str.begin()};
auto rng = view::counted(i, str.size()) | view::split(starts_with_g{});
CHECK(distance(rng) == 3);
if(distance(rng) == 3)
{
check_equal(*begin(rng), c_str("Now is the time for all "));
check_equal(*next(begin(rng)), c_str("g"));
check_equal(*(next(begin(rng),2)), c_str("good men to come to the aid of their country."));
}
}
{
std::string str("meow");
auto rng = view::split(str, view::empty<char>());
CHECK(distance(rng) == 4);
if(distance(rng) == 4)
{
check_equal(*(next(begin(rng),0)), c_str("m"));
check_equal(*(next(begin(rng),1)), c_str("e"));
check_equal(*(next(begin(rng),2)), c_str("o"));
check_equal(*(next(begin(rng),3)), c_str("w"));
}
}
{
int a[] = {0, 2, 3, 1, 4, 5, 1, 6, 7};
auto rng = a | view::remove_if([](int i) { return i % 2 == 0; });
auto srng = view::split(rng, 1);
CHECK(distance(srng) == 3);
check_equal(*begin(srng), {3});
check_equal(*next(begin(srng), 1), {5});
check_equal(*next(begin(srng), 2), {7});
}
{
std::string str("now is \t the\ttime");
auto rng = view::split(str, (int(*)(int))&std::isspace);
CHECK(distance(rng) == 4);
if(distance(rng) == 4)
{
check_equal(*(next(begin(rng),0)), c_str("now"));
check_equal(*(next(begin(rng),1)), c_str("is"));
check_equal(*(next(begin(rng),2)), c_str("the"));
check_equal(*(next(begin(rng),3)), c_str("time"));
}
}
return test_result();
}