-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
64 lines (56 loc) · 1.41 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "utils.hpp"
/*--------------------------------------------------------------------------------
Misc Utils Implementation
--------------------------------------------------------------------------------*/
vector<string> utils::read_lines(const string& filename) {
ifstream file(filename);
user_error(string("Invalid file: ") + filename, file.good());
vector<string> lines;
string curr;
while (getline(file, curr))
{
if (curr.size() > 0)
lines.push_back(curr);
}
file.close();
return lines;
}
vector<string> utils::split(const string& s, char delimiter)
{
vector<string> tokens;
string token;
istringstream token_stream(s);
while (getline(token_stream, token, delimiter))
{
tokens.push_back(token);
}
return tokens;
}
/*--------------------------------------------------------------------------------
String Extentions
--------------------------------------------------------------------------------*/
string repeat(string str, const size_t n)
{
if (n == 0) {
str.clear();
str.shrink_to_fit();
return str;
}
else if (n == 1 || str.empty()) {
return str;
}
const auto period = str.size();
if (period == 1) {
str.append(n - 1, str.front());
return str;
}
str.reserve(period * n);
size_t m{ 2 };
for (; m < n; m *= 2) str += str;
str.append(str.c_str(), (n - (m / 2)) * period);
return str;
}
string operator*(string str, size_t n)
{
return repeat(move(str), n);
}