-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
49 lines (45 loc) · 978 Bytes
/
utils.cpp
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
#include "common_header.hpp"
#include "utils.hpp"
#include <string>
#include <vector>
std::vector<std::string> split(const std::string& line, char c) {
std::vector<std::string> components;
size_t pos = 0;
do {
size_t npos = line.find(c, pos);
if (npos != std::string::npos) {
auto s = line.substr(pos, npos - pos);
if (!s.empty()) {
components.push_back(s);
}
}
else {
auto s = line.substr(pos);
if (!s.empty()) {
components.push_back(s);
}
break;
}
pos = npos + 1;
} while (true);
return components;
}
std::string trimmed(const std::string& str) {
std::string out;
bool first_non_empty_occurred = false;
for (int i = 0; i < str.size(); i++) {
if (str[i] != ' ') first_non_empty_occurred = true;
if (first_non_empty_occurred) {
out.push_back(str[i]);
}
}
first_non_empty_occurred = false;
int i;
for (i = out.size() - 1; i >= 0; i--) {
if (out[i] != ' ') {
break;
}
}
out.reserve(i + 1);
return out;
}