forked from chronolaw/boost_guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.cpp
More file actions
123 lines (93 loc) · 2.53 KB
/
Copy pathtokenizer.cpp
File metadata and controls
123 lines (93 loc) · 2.53 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
// Copyright (c) 2015
// Author: Chrono Law
#include <cstring>
#include <std.hpp>
using namespace std;
#include <boost/algorithm/string.hpp>
#include <boost/tokenizer.hpp>
using namespace boost;
template<typename T>
void print(T &tok)
{
for (auto& x : tok)
{ cout << "[" << x << "]"; }
cout << endl;
}
//////////////////////////////////////////
void case1()
{
string str("Link raise the master-sword.");
tokenizer<> tok(str);
print(tok);
//for(auto& x: tok)
//{ cout << "["<< x << "]";}
}
//////////////////////////////////////////
void case2()
{
char str[] = "Link ;; <master-sword> zelda";
char_separator<char> sep;
tokenizer<char_separator<char>, char*> tok(str, str +strlen(str), sep);
print(tok);
tok.assign(str, str +strlen(str),
char_separator<char>(" ;-", "<>"));
print(tok);
tok.assign(str, str +strlen(str),
char_separator<char>(" ;-<>", "", keep_empty_tokens));
print(tok);
}
//////////////////////////////////////////
void case3()
{
string str = "id,100,name,\"mario\"";
escaped_list_separator<char> sep;
tokenizer<escaped_list_separator<char> > tok(str, sep);
print(tok);
}
//////////////////////////////////////////
void case4()
{
string str = "2233344445";
int offsets[] = {2,3,4};
offset_separator sep(offsets, offsets + 3, true, false);
tokenizer<offset_separator> tok(str, sep);
print(tok);
tok.assign(str, offset_separator(offsets, offsets + 3, false));
print(tok);
str += "56667";
tok.assign(str, offset_separator(offsets, offsets + 3, true, false));
print(tok);
}
//////////////////////////////////////////
template<typename Func, typename String = std::string>
struct tokenizer_wrapper
{
//typedef typename Func::string_type String;
typedef tokenizer<Func, typename String::const_iterator, String > type;
};
void case5()
{
wstring str(L"Link mario samus");
auto p = make_split_iterator(str, first_finder(L" ", is_iequal()));
decltype(p) endp;
for (; p != endp; ++p)
{ wcout << L"[" << *p << L"]" ; }
cout << endl;
char_separator<wchar_t> sep(L" ");
//tokenizer<char_separator<wchar_t>,wstring::const_iterator,
// wstring > tok(str, sep);
tokenizer_wrapper<
char_separator<wchar_t>,wstring>::type tok(str, sep);
for(auto& x : tok)
{ wcout << L"["<< x << L"]"; }
cout << endl;
}
//////////////////////////////////////////
int main()
{
case1();
case2();
case3();
case4();
case5();
}