forked from chronolaw/boost_guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_algo.cpp
More file actions
241 lines (182 loc) · 5.3 KB
/
Copy pathstring_algo.cpp
File metadata and controls
241 lines (182 loc) · 5.3 KB
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
// Copyright (c) 2015
// Author: Chrono Law
#include <std.hpp>
using namespace std;
#include <boost/utility/string_ref.hpp>
#include <boost/algorithm/string.hpp>
using namespace boost;
//////////////////////////////////////////
void case1()
{
string str("readme.txt");
if (ends_with(str, "txt"))
{
cout << to_upper_copy(str) + " UPPER" << endl;
assert(ends_with(str, "txt"));
}
replace_first(str, "readme", "followme");
cout << str << endl;
vector<char> v(str.begin(), str.end());
vector<char> v2 = to_upper_copy(
erase_first_copy(v, "txt"));
for (auto ch : v2)
{ cout << ch; }
}
//////////////////////////////////////////
void case2()
{
string str("FireEmblem Heroes\n");
cout << to_upper_copy(str);
cout << str;
to_lower(str);
cout << str;
}
//////////////////////////////////////////
void case3()
{
string str("Power Bomb");
assert(iends_with(str, "bomb"));
assert(!ends_with(str, "bomb"));
assert(starts_with(str, "Pow"));
assert(contains(str, "er"));
string str2 = to_lower_copy(str);
assert(iequals(str, str2));
string str3("power suit");
assert(ilexicographical_compare(str, str3));
assert(all(str2.substr(0, 5), is_lower()));
}
//////////////////////////////////////////
void case4()
{
string str1("Samus"), str2("samus");
assert(!is_equal()(str1, str2));
assert( is_less()(str1, str2));
//cout << (to_upper_copy(str1) == to_upper_copy(str2)) << endl;
//cout << is_iequal()(str1, str2) << endl;
assert(!is_equal()(str1, string_ref(str2)));
}
//////////////////////////////////////////
#include <boost/format.hpp>
struct is_zero_or_one
{
bool operator()(char x)
{ return x == '0' || x == '1';}
};
auto is01 = [](char x)
{ return x == '0' || x == '1';};
void case5()
{
format fmt("|%s|\n");
string str = " samus aran ";
cout << fmt % trim_copy(str);
cout << fmt % trim_left_copy(str);
trim_right(str);
cout << fmt % str;
string str2 = "2015 Happy new Year!!!";
cout << fmt % trim_left_copy_if(str2, is_digit());
cout << fmt % trim_right_copy_if(str2, is_punct());
cout << fmt % trim_copy_if(str2,
is_punct() || is_digit() || is_space());
}
//////////////////////////////////////////
void case6()
{
format fmt("|%s|. pos = %d\n");
string str = "Long long ago, there was a king.";
iterator_range<string::iterator> rge;
rge = find_first(str, "long");
cout << fmt % rge % (rge.begin() - str.begin());
rge = ifind_first(str, "long");
cout << fmt % rge % (rge.begin() - str.begin());
rge = find_nth(str, "ng", 2);
cout << fmt % rge % (rge.begin() - str.begin());
rge = find_head(str, 4);
cout << fmt % rge % (rge.begin() - str.begin());
rge = find_tail(str, 5);
cout << fmt % rge % (rge.begin() - str.begin());
rge = find_first(str, "samus");
assert(rge.empty() && !rge);
}
//////////////////////////////////////////
void case7()
{
string str = "Samus beat the monster.\n";
cout << replace_first_copy(str, "Samus", "samus");
replace_last(str, "beat", "kill");
cout << str;
replace_tail(str, 9, "ridley.\n");
cout << str;
cout << ierase_all_copy(str, "samus");
cout << replace_nth_copy(str, "l", 1, "L");
cout << erase_tail_copy(str, 8);
}
//////////////////////////////////////////
void case8()
{
string str = "Samus,Link.Zelda::Mario-Luigi+zelda";
deque<string> d;
ifind_all(d, str, "zELDA");
assert(d.size() == 2);
for (auto x : d)
{ cout << "["<< x << "] "; }
cout << endl;
list<iterator_range<string::iterator> > l;
split(l, str, is_any_of(",.:-+"));
for (auto x : l)
{ cout << "["<< x << "]"; }
cout << endl;
l.clear();
split(l, str, is_any_of(".:-"), token_compress_on);
for (auto x : l)
{ cout << "["<< x << "]"; }
cout << endl;
}
//////////////////////////////////////////
#include <boost/assign.hpp>
void case9()
{
using namespace boost::assign;
vector<string> v = list_of("Samus")("Link")("Zelda")("Mario");
cout << join(v, "+") << endl;
//cout << join_if(v, "**", is_contains_a());
cout << join_if(v, "**",
[](string_ref s)
{ return contains(s, "a"); }
);
cout << endl;
}
//////////////////////////////////////////
void case10()
{
string str("Samus||samus||mario||||Link");
//typedef find_iterator<string::iterator> string_find_iterator;
//string_find_iterator pos, end;
//for (pos = make_find_iterator(str, first_finder("samus", is_iequal()));
// pos != end; ++pos)
//{ cout << "[" << *pos << "]" ; }
auto pos = make_find_iterator(str, first_finder("samus", is_iequal()));
decltype(pos) end;
for(; pos != end; ++pos)
{ cout << "[" << *pos << "]" ; }
cout << endl;
typedef split_iterator<string::iterator> string_split_iterator;
string_split_iterator p, endp;
for (p = make_split_iterator(str, first_finder("||", is_iequal()));
p != endp; ++p)
{ cout << "[" << *p << "]" ; }
cout << endl;
}
//////////////////////////////////////////
int main()
{
case1();
case2();
case3();
case4();
case5();
case6();
case7();
case8();
case9();
case10();
}