-
Notifications
You must be signed in to change notification settings - Fork 9
/
string_utility.hpp
218 lines (188 loc) · 5.18 KB
/
string_utility.hpp
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
#pragma once
#include <cstring>
#include <algorithm>
#include <sstream>
#include <iomanip>
#include <vector>
template <typename Str>
struct tokenizer
{
tokenizer(Str const& str)
: _string(str), _offset(0)
{}
tokenizer(Str const& str, Str const& delimiters)
: _string(str), _offset(0), _delimiters(delimiters)
{}
bool next_token()
{
return next_token(_delimiters);
}
bool next_token(Str const& delimiters)
{
size_t i = _string.find_first_not_of(delimiters, _offset);
if (i == Str::npos)
{
_offset = _string.length();
return false;
}
size_t j = _string.find_first_of(delimiters, i);
if (j == Str::npos) {
_token = _string.substr(i);
_offset = _string.length();
return true;
}
_token = _string.substr(i, j - i);
_offset = j;
return true;
}
const Str get_token() const
{
return _token;
}
void reset()
{
_offset = 0;
}
size_t _offset;
const Str _string;
Str _token;
Str _delimiters;
};
template <typename Str>
struct string_utility
{
static Str to_upper(const Str& str)
{
Str temp(str);
std::transform(temp.begin(), temp.end(), temp.begin(), ::toupper);
return temp;
}
static Str to_lower(const Str& str)
{
Str temp(str);
std::transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
return temp;
}
static Str trim_left(const Str& str)
{
Str temp(str);
auto it = temp.begin();
for (it = temp.begin(); it != temp.end(); it++) {
if (!isspace(*it)) {
break;
}
}
if (it == temp.end()) {
temp.clear();
} else {
temp.erase(temp.begin(), it);
}
return temp;
}
static Str trim_right(const Str& str)
{
Str temp(str);
for (auto it = temp.end() - 1; ;it--) {
if (!isspace(*it)) {
temp.erase(it + 1, temp.end());
break;
}
if (it == temp.begin()) {
temp.clear();
break;
}
}
return temp;
}
static Str trim(const Str& str)
{
Str temp = trim_left(str);
return trim_right(temp);
}
static bool starts_with(Str const & value, Str const & starting)
{
if (starting.size() > value.size()) return false;
return std::equal(starting.begin(), starting.end(), value.begin());
}
static bool ends_with(Str const & value, Str const & ending)
{
if (ending.size() > value.size()) return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
static bool equals_ignore_case(const Str& str1, const Str& str2)
{
return to_lower(str1) == to_lower(str2);
}
static bool istarts_with(Str const & value, Str const & starting)
{
if (starting.size() > value.size()) return false;
Str temp = value.substr(0, starting.size());
return equals_ignore_case(to_lower(starting), to_lower(temp));
}
static bool iends_with(Str const & value, Str const & ending)
{
if (ending.size() > value.size()) return false;
Str temp = value.substr(value.size() - ending.size(), ending.size());
return equals_ignore_case(to_lower(ending), to_lower(temp));
}
template <typename T>
static T from_string(const Str& str)
{
T obj;
std::basic_istringstream<typename Str::value_type> temp(str);
temp >> obj;
return obj;
}
static bool from_string(const Str& str)
{
bool obj;
std::basic_istringstream<typename Str::value_type> temp(str);
temp >> std::boolalpha >> obj;
return obj;
}
template <typename T>
static T from_hex_string(const Str& str)
{
T obj;
std::basic_istringstream<typename Str::value_type> temp(str);
temp >> std::hex >> obj;
return obj;
}
template <typename T>
static Str to_string(const T& var)
{
std::basic_ostringstream<typename Str::value_type> temp;
temp << var;
return temp.str();
}
static Str to_string(const bool& var)
{
std::basic_ostringstream<typename Str::value_type> temp;
temp << std::boolalpha << var;
return temp.str();
}
template <typename T>
static Str to_hex_string(const T& var, int width)
{
std::basic_ostringstream<typename Str::value_type> temp;
temp << std::hex;
if(width > 0)
{
temp << std::setw(width) << std::setfill<typename Str::value_type>('0');
}
temp << var;
return temp.str();
}
static std::vector<Str> split(Str const& str, Str const& delimiters)
{
std::vector<Str> ss;
tokenizer<Str> token(str, delimiters);
while (token.next_token())
{
ss.push_back(token.get_token());
}
return ss;
}
};
typedef string_utility<std::string> string_utility_a;
typedef string_utility<std::wstring> string_utility_w;