-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
86 lines (75 loc) · 1.92 KB
/
utils.cpp
File metadata and controls
86 lines (75 loc) · 1.92 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
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
#include "utils.hpp"
std::string escape(const std::string &str)
{
std::string escaped;
for (size_t i = 0; i < str.length(); i++)
{
if (str[i] == '\n')
escaped += "\\n";
else if (str[i] == '\r')
escaped += "\\r";
else
escaped += str[i];
}
return escaped;
}
std::string ltrim(const std::string &str)
{
size_t start = str.find_first_not_of(" \t\n\t");
return (start == std::string::npos) ? "" : str.substr(start);
}
std::string rtrim(const std::string &str)
{
size_t end = str.find_last_not_of(" \t\n\t");
return (end == std::string::npos) ? "" : str.substr(0, end + 1);
}
// Removed line end and any whitespace at the start and end of the string
std::string trimstr(const std::string &str)
{
return rtrim(ltrim(str));
}
std::vector<std::string> split(const std::string &str, char delim, bool trim)
{
std::vector<std::string> splits;
std::istringstream iss(str);
std::string line;
if (iss.str().empty())
return splits;
while (std::getline(iss, line, delim))
{
if (line.empty() && splits.size() > 0)
splits.back() += delim;
else
splits.push_back(line);
}
if (trim)
splits.back().erase(splits.back().length() - 1);
// Removing empty string at the end of the vector
while (splits.size() > 0 && splits.back().empty())
splits.pop_back();
//for (size_t i = 0; i < splits.size(); i++)
//{
// std::cout << "[" << i << "] " << splits[i] << " (" << splits[i].length() << "): ";
// for (size_t j = 0; j < splits[i].length(); j++)
// std::cout << " " << (int)splits[i][j];
// std::cout << std::endl;
//}
return splits;
}
std::string c(int code)
{
std::stringstream ss;
ss << std::setw(3) << std::setfill('0') << code;
return ss.str();
}
std::string join(const std::string arr[], size_t size, const std::string& separator)
{
std::string result;
for (size_t i = 0; i < size; i++)
{
result += arr[i];
if (i < size - 1)
result += separator;
}
return result;
}