-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cpp
136 lines (113 loc) · 3.5 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <filesystem>
#include "Utils.h"
#include "ParsedScripts/Values/Constant.h"
bool Utils::EndsWith(std::string str, std::string ending) {
if (ending.size() > str.size()) return false;
return std::equal(ending.rbegin(), ending.rend(), str.rbegin());
}
std::string Utils::SubString(const std::string& str, int start, int end) {
return str.substr(start, end - start);
}
std::vector<std::string> Utils::SubVector(const std::vector<std::string> &vec, int start, int end) {
std::vector<std::string> result;
for (int i = start; i < end; i++) {
result.push_back(vec[i]);
}
return result;
}
void Utils::DumpErrorInfo(const RuntimeException& e) {
std::cerr << "An error has occurred" << std::endl;
// If it's a constant object we can dump more info
auto val = dynamic_cast<Constant*>(e.Object);
if (val == nullptr) {
return;
}
std::cerr << "Value: " << val->ToString() << std::endl;
}
std::string Utils::GetAbsolutePath(const std::string& path) {
std::filesystem::path pathInput(path);
// Add current path if the input path is relative
std::filesystem::path fullPath;
if (pathInput.is_absolute()) {
fullPath = pathInput;
} else {
std::filesystem::path currentPath = std::filesystem::current_path();
fullPath = currentPath / pathInput;
}
fullPath = fullPath.lexically_normal();
return fullPath.string();
}
std::string Utils::TrimString(const std::string &val) {
int firstNonSpace = -1;
for (int i = 0; i < val.size(); ++i) {
if (val[i] == ' ') {
continue;
}
firstNonSpace = i;
break;
}
int lastNonSpace = -1;
for (int i = val.size(); i > 0; --i) {
if (val[i] == ' ' || val[i] == '\0') {
continue;
}
lastNonSpace = i+1;
break;
}
return val.substr(firstNonSpace, lastNonSpace - firstNonSpace);
}
bool Utils::ContainsChar(const std::string &str, const char &v) {
for (char c : str) {
if (c == v) {
return true;
}
}
return false;
}
std::vector<std::string> Utils::SplitString(const std::string &str, const char &delimiter) {
return SplitString(str, delimiter, -1);
}
std::vector<std::string> Utils::SplitString(const std::string &str, const char &delimiter, int count) {
std::vector<std::string> result;
if (count == 1) {
result.push_back(str);
return result;
}
std::string current;
for (char c : str) {
if (c == delimiter) {
result.push_back(current);
current.clear();
if (count == result.size() - 1) {
break;
}
} else {
current += c;
}
}
result.push_back(current);
return result;
}
// Split string but don't split if the delimiter is inside a string
std::vector<std::string> Utils::SafeSplit(const std::string &str, const char &delimiter) {
std::vector<std::string> result;
std::string current;
bool inString = false;
for (char c : str) {
if (c == '"') {
inString = !inString;
}
if (c == delimiter && !inString) {
result.push_back(current);
current.clear();
} else {
current += c;
}
}
result.push_back(current);
return result;
}
std::vector<std::string> Utils::SplitString(const std::string &str, const std::string &delimiter, int count) {
return std::vector<std::string>(); // TODO: Implement
}