-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_util.h
More file actions
164 lines (128 loc) · 3.61 KB
/
string_util.h
File metadata and controls
164 lines (128 loc) · 3.61 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
#ifndef __STRING_UTIL_H__
#define __STRING_UTIL_H__
#pragma once
#include <cctype>
#include <sstream>
#include <string>
#include <vector>
namespace string_util {
/* Prototypes */
bool ends_with(const std::string&, const std::string&);
template <template<typename> class Container, typename T>
std::string join(const Container<T>&, const char delim=' ');
template <template<typename> class Container, typename T>
std::string join(const Container<T>&, const std::string&);
std::string lstrip(const std::string&);
std::string rstrip(const std::string&);
std::vector<std::string> split(const std::string&, char delim=' ');
std::vector<std::string> split(const std::string&, const std::string&);
bool starts_with(const std::string&, const std::string&);
std::string strip(const std::string&);
/* Implementations */
bool ends_with(const std::string& s, const std::string& end)
{
for (auto i1 = s.rbegin(), i2 = end.rbegin(); i2 != end.rend(); ++i1, ++i2) {
if (i1 == s.rend())
return false;
else if (*i1 != *i2)
return false;
}
return true;
}
template <template<typename> class Container, typename T>
std::string join(const Container<T>& subs, const char delim)
{
return join(subs, std::string{delim});
}
template <template<typename> class Container>
std::string join(const Container<std::string>& subs, const std::string& delim)
{
if (subs.size() == 0) {
return {};
} else {
std::string result;
for (auto& sub : subs) {
result += sub + delim;
}
/* Remove the last 'delim' again. */
result.erase(result.size() - delim.size());
return result;
}
}
template <template<typename> class Container, typename T>
std::string join(const Container<T>& subs, const std::string& delim)
{
std::vector<std::string> string_subs;
for (const auto& s : subs) {
std::stringstream ss;
ss << s;
string_subs.push_back(ss.str());
}
return join(string_subs, delim);
}
std::string lstrip(const std::string& s)
{
std::string result;
auto i = s.begin();
while (i != s.end()) {
if (std::isspace(*i) != 0 || std::iscntrl(*i) != 0)
++i;
else
break;
}
while (i != s.end()) {
result.push_back(*i);
++i;
}
return result;
}
std::string rstrip(const std::string& s)
{
std::string result;
auto i = s.rbegin();
while (i != s.rend()) {
if (std::isspace(*i) != 0 || std::iscntrl(*i) != 0)
++i;
else
break;
}
while (i != s.rend()) {
result = *i + result;
++i;
}
return result;
}
std::vector<std::string> split(const std::string& s, const char delim)
{
return split(s, std::string{delim});
}
std::vector<std::string> split(const std::string& s, const std::string& delim)
{
std::vector<std::string> subs;
size_t dpos = 0, oldpos = 0;
while (dpos != std::string::npos) {
dpos = s.find(delim, oldpos);
if (dpos == std::string::npos)
subs.push_back(s.substr(oldpos));
else
subs.push_back(s.substr(oldpos, dpos-oldpos));
oldpos = dpos + delim.size();
}
return subs;
}
bool starts_with(const std::string& s, const std::string& start)
{
for (auto i1 = s.begin(), i2 = start.begin(); i2 != start.end(); ++i1, ++i2) {
if (i1 == s.end())
return false;
else if (*i1 != *i2)
return false;
}
return true;
}
std::string strip(const std::string& s)
{
return rstrip(lstrip(s));
}
} /* namespace string_util */
#endif /* __STRING_UTIL_H__ */