-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
50 lines (42 loc) · 1.03 KB
/
util.cpp
File metadata and controls
50 lines (42 loc) · 1.03 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
#include "util.hpp"
#include <cassert>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
namespace util {
using namespace std;
vector<string> read_file(const string &path) {
vector<string> lines;
ifstream input(path);
string line;
while (getline(input, line)) {
lines.push_back(line);
}
return lines;
}
bool starts_with(const string &input, const string &prefix) {
return input.rfind(prefix, 0) == 0;
}
vector<string> split(const string &line, const string &delimiter) {
vector<string> result;
size_t last = 0;
size_t next = 0;
while ((next = line.find(delimiter, last)) != string::npos) {
const string token = line.substr(last, next - last);
result.push_back(token);
last = next + delimiter.size();
}
result.push_back(line.substr(last));
return result;
}
vector<string> split_by_spaces(const string &line) {
std::stringstream ss(line);
std::string word;
vector<string> result;
while (ss >> word) {
result.push_back(word);
}
return result;
}
} // namespace util