-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cpp
99 lines (73 loc) · 2.27 KB
/
util.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
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
#include <fstream>
#include <sstream>
#include <iostream>
#include "util.hpp"
// Split a string on the given delimiter, ignoring any empty strings produced
std::vector<std::string> split(const std::string& str, const char delim) {
const auto size = str.size();
std::vector<std::string> vec{};
size_t pos = 0;
while (pos < size) {
size_t count = 0;
// Iterate forward until we reach the end of the string or find the next delimiter
while ((pos + count < size) && (str.at(pos + count) != delim)) {
++count;
}
if (count != 0) {
// If count == 0, str.at(pos, count) will return an empty string
vec.push_back(str.substr(pos, count));
}
// Iterate forward, skipping the delimiter
pos = pos + count + 1;
}
return vec;
}
std::string read_file(const std::string& path) {
try {
std::ifstream file{};
file.exceptions(std::ifstream::badbit | std::ifstream::failbit | std::ifstream::eofbit);
file.open(path);
std::stringstream buff{};
buff << file.rdbuf();
return buff.str();
} catch (const std::runtime_error& err) {
std::cout << "error: unable to read file " + path << std::endl;
throw err;
}
}
static bool is_space(const char c) {
switch (c) {
case ' ':
case '\n':
case '\r':
case '\t':
case '\v':
case '\f':
return true;
default:
return false;
}
}
// Remove leading and trailing whitespace from a string
std::string trim_copy(std::string str) {
auto it = str.begin();
while ((it != str.end()) && is_space(*it)) {
++it;
}
str.erase(str.begin(), it);
auto rit = str.rbegin();
while ((rit != str.rend()) && is_space(*rit)) {
++rit;
}
str.erase(rit.base(), str.end());
return str;
}
// Replace all occurences of 'search' with 'replace' in 'str' and return a new copy
std::string replace_copy(std::string str, const std::string& search, const std::string& replace) {
size_t pos = str.find(search);
while (pos != std::string::npos) {
str.replace(pos, search.size(), replace);
pos = str.find(search, pos + replace.size());
}
return str;
}