-
Notifications
You must be signed in to change notification settings - Fork 4
/
str.hpp
78 lines (73 loc) · 1.84 KB
/
str.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
#pragma once
template <typename T_CHAR>
void str_trim_right(std::basic_string<T_CHAR>& str, const T_CHAR* spaces)
{
typedef std::basic_string<T_CHAR> string_type;
size_t j = str.find_last_not_of(spaces);
if (j == string_type::npos)
{
str.clear();
}
else
{
str = str.substr(0, j + 1);
}
}
template <typename T_STR_CONTAINER>
inline typename T_STR_CONTAINER::value_type
str_join(const T_STR_CONTAINER& container,
const typename T_STR_CONTAINER::value_type& sep)
{
typename T_STR_CONTAINER::value_type result;
typename T_STR_CONTAINER::const_iterator it, end;
it = container.begin();
end = container.end();
if (it != end)
{
result = *it;
for (++it; it != end; ++it)
{
result += sep;
result += *it;
}
}
return result;
}
template <typename T_STR_CONTAINER>
void
str_split(T_STR_CONTAINER& container,
const typename T_STR_CONTAINER::value_type& str,
const typename T_STR_CONTAINER::value_type& chars)
{
container.clear();
size_t i = 0, k = str.find_first_of(chars);
while (k != T_STR_CONTAINER::value_type::npos)
{
container.push_back(str.substr(i, k - i));
i = k + 1;
k = str.find_first_of(chars, i);
}
container.push_back(str.substr(i));
}
template <typename T_STR>
bool
str_replace_all(T_STR& str, const T_STR& from, const T_STR& to)
{
bool ret = false;
size_t i = 0;
for (;;) {
i = str.find(from, i);
if (i == T_STR::npos)
break;
ret = true;
str.replace(i, from.size(), to);
i += to.size();
}
return ret;
}
template <typename T_STR>
inline bool
str_replace_all(T_STR& str, const typename T_STR::value_type* from, const typename T_STR::value_type* to)
{
return str_replace_all(str, T_STR(from), T_STR(to));
}