-
Notifications
You must be signed in to change notification settings - Fork 1
/
text.cc
46 lines (34 loc) · 1.16 KB
/
text.cc
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
#include "text.h"
std::vector<std::string> split(const std::string &str, char separator,
bool includeEmpty)
{
std::vector<std::string> elements;
for (std::string::size_type begin = 0, stop = 0, size = str.size();
; begin = stop + 1) {
// check for trailing empty string
if (!includeEmpty && (begin == size)) break;
// find separator
stop = str.find(separator, begin);
// remember element if allowed
if ((stop > begin) || includeEmpty)
elements.push_back(str.substr(begin, stop - begin));
// terminate on EOS
if (stop == std::string::npos) break;
}
// OK
return elements;
}
std::string strip(const std::string &str)
{
// compute start and end of string
std::string::size_type start = 0;
std::string::size_type end = str.size();
while ((start < end) && isspace(str[start]))
++start;
while ((end > start) && isspace(str[end - 1]))
--end;
// test for already stripped string
if ((start == 0) && (end == str.size())) return str;
// do the strip
return str.substr(start, end - start);
}