-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.cpp
More file actions
40 lines (31 loc) · 1.17 KB
/
Copy pathutils.cpp
File metadata and controls
40 lines (31 loc) · 1.17 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
// Available under GPLv3.
// Adapted from code by Albert Zeyer at http://github.com/albertz/planet_wars-cpp/blob/master/
// Author: Iouri Khramtsov
#include "utils.h"
typedef unsigned char uchar;
std::string TrimSpaces(const std::string& str) {
size_t start = 0;
for(size_t i = 0; i < str.size(); ++i)
if(!isspace((uchar)str[i]) || isgraph((uchar)str[i])) {
start = i;
break;
}
size_t n = 0;
for(size_t i = str.size(); i > start; --i)
if(!isspace((uchar)str[i-1]) || isgraph((uchar)str[i-1])) {
n = i - start;
break;
}
return str.substr(start, n);
}
std::vector<std::string> Tokenize(const std::string& s, const std::string& delimiters) {
std::vector<std::string> tokens;
std::string::size_type lastPos = s.find_first_not_of(delimiters, 0);
std::string::size_type pos = s.find_first_of(delimiters, lastPos);
while (std::string::npos != pos || std::string::npos != lastPos) {
tokens.push_back(s.substr(lastPos, pos - lastPos));
lastPos = s.find_first_not_of(delimiters, pos);
pos = s.find_first_of(delimiters, lastPos);
}
return tokens;
}