-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.cxx
383 lines (305 loc) · 10.1 KB
/
index.cxx
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "index.hxx"
#include "deps/datcxx/util-string/index.hxx"
#include <vector>
#include <regex>
namespace Hyper {
namespace Util {
namespace Path {
using String = std::string;
String delimiter () {
return ":";
}
bool isAbsolute(const String& path) {
return path[0] == Path::sep();
}
String basename(const String& path, const String& ext) {
std::vector<String> parts =
Hyper::Util::String::split(path, String(1, Path::sep()));
if (parts.empty()) {
return "";
}
const auto begin = parts.begin();
for (size_t i=0; i < parts.size(); i++) {
if (!parts[i].empty()) {
continue;
}
parts.erase(begin + i);
}
String s = parts.back();
if (s.substr(s.length() - ext.length(), s.length()) == ext) {
s = s.substr(0, s.length() - ext.length());
}
return s;
}
String basename(const String& path) {
String ext;
return Path::basename(path, ext);
}
String extname(const String& path) {
String ext;
auto tmp = Util::String::trim(path, "/");
if (tmp == "..") {
return "";
}
if (tmp == ".") {
return "";
}
if (tmp == "/") {
return "";
}
if (tmp == "//") {
return "";
}
size_t dotIdx = tmp.find_last_of(".");
if (dotIdx != String::npos) {
size_t dirSepIdx = tmp.find_last_of("/\\");
if (dotIdx > dirSepIdx + 1) {
ext = tmp.substr(dotIdx);
}
}
return ext;
}
String relative(const String& from, const String& to) {
if (from == to) return "";
const auto f = Path::resolve(from);
const auto t = Path::resolve(to);
if (f == t) return "";
const auto fSlashOffset = f.find_first_not_of("/", 1);
const auto tSlashOffset = t.find_first_not_of("/", 1);
const auto fStart = fSlashOffset == -1 ? 1 : fSlashOffset;
const auto tStart = tSlashOffset == -1 ? 1 : tSlashOffset;
const auto fEnd = f.length();
const auto tEnd = t.length();
const auto fLen = fEnd - fStart;
const auto tLen = tEnd - tStart;
const auto len = fLen < tLen ? fLen : tLen;
auto lcs = -1; // last common separator
for (auto i = 0; i <= len; ++i) {
if (i == len) {
if (tLen > len) {
if (i == 0) return t.substr(tStart + i);
if (t[tStart + i] == '/') return t.substr(tStart + i + 1);
} else if (fLen > len && (i == 0 || f[fStart + i] == '/')) {
lcs = i;
}
break;
}
const auto fPos = fStart + i;
if (t[tStart + i] != f[fPos]) break;
if (f[fPos] == '/') lcs = i;
}
String result;
for (auto i = fStart + lcs + 1; i <= fEnd; ++i) {
if (i == fEnd || f[i] == '/') {
result += result.length() == 0 ? ".." : "/..";
}
}
if (result.length() > 0) return result + t.substr(tStart + lcs);
return t.substr(t[lcs] == '/' ? tStart + lcs + 1 : tStart + lcs);
}
std::vector<String> splitPathList(const String& path) {
std::vector<String> result;
String::size_type start = 0;
String::size_type end = 0;
String delimiter = String(1, Path::sep());
while (end != String::npos) {
end = path.find(delimiter, start);
if (start < path.size() && start != end) {
String segment = path.substr(start, end - start);
result.push_back(segment);
}
start = end + delimiter.size();
}
return result;
}
String joinPathList(const std::vector<String>& list) {
String res;
int len = list.size();
int index = 0;
for (const auto& el : list) {
res += el;
if (++index != len) {
res += Path::sep();
}
}
return res;
}
std::vector<String> normalizePathList(std::vector<String> parts, bool allowAboveRoot) {
std::vector<String> res;
int len = parts.size();
for (int i = 0; i < len; i++) {
String p = parts[i];
if ((p.length() == 0) || p == ".") {
continue;
}
if (p == "..") {
if (!res.empty() && res[res.size() - 1] != "..") {
res.pop_back();
} else if (allowAboveRoot) {
res.emplace_back("..");
}
} else {
res.push_back(p);
}
}
return res;
}
String normalize(const String& path) {
bool isAbs = Path::isAbsolute(path);
bool trailingSlash = path[path.length() -1] == Path::sep();
auto split = Path::splitPathList(path);
auto normalized = Path::normalizePathList(split, !isAbs);
auto tmp = Path::joinPathList(normalized);
if (tmp.length() == 0 && !isAbs) {
tmp = ".";
}
if (path.length() > 0 && trailingSlash) {
tmp += Path::sep();
}
return (isAbs ? String(1, Path::sep()) : "") + tmp;
}
String joinVector(const std::vector<String>& args) {
if (args.size() == 1 && args[0] == "/") {
return "/";
}
String path;
for (const auto &el : args) {
if (el.length() > 0) {
if (path.length() == 0) {
path += el;
}
else {
path += Path::sep() + el;
}
}
}
return Path::normalize(path);
}
String resolveFromVector(const std::vector<String>& args) {
String resolvedPath = "";
bool resolvedAbsolute = false;
for (int i = args.size() - 1; i >= -1 && !resolvedAbsolute; i--) {
String path = args[i];
if (path.empty()) {
continue;
}
resolvedPath = path + Path::sep() + resolvedPath;
resolvedAbsolute = path[0] == Path::sep();
}
// At this point the path should be resolved to a full absolute path, but
// handle relative paths to be safe (might happen when process.cwd() fails)
// Normalize the path
auto split = Path::splitPathList(resolvedPath);
auto normalized = Path::normalizePathList(split, !resolvedAbsolute);
resolvedPath = Path::joinPathList(normalized);
auto sep = String(1, Path::sep());
String p = ((resolvedAbsolute ? sep : "") + resolvedPath);
if (p.empty()) {
return ".";
}
return p;
}
bool isPathSeparator (const char s) {
return s == '/' || s == '\\';
}
String dirname(const String& path) {
size_t len = path.length();
if (len == 0) {
return ".";
}
size_t rootEnd = -1;
size_t offset = 0;
const char ch = path[0];
if (len == 1) {
// `path` contains just a path separator, exit early to avoid
// unnecessary work or a dot.
return isPathSeparator(ch) ? path : ".";
}
// Try to match a root
if (isPathSeparator(ch)) {
// Possible UNC root
rootEnd = offset = 1;
if (isPathSeparator(path[1])) {
// Matched double path separator at beginning
size_t j = 2;
size_t last = j;
// Match 1 or more non-path separators
while (j < len && !isPathSeparator(path[j])) {
j++;
}
if (j < len && j != last) {
// Matched!
last = j;
// Match 1 or more path separators
while (j < len && isPathSeparator(path[j])) {
j++;
}
if (j < len && j != last) {
// Matched!
last = j;
// Match 1 or more non-path separators
while (j < len && !isPathSeparator(path[j])) {
j++;
}
if (j == len) {
// We matched a UNC root only
return path;
}
if (j != last) {
// We matched a UNC root with leftovers
// Offset by 1 to include the separator after the UNC root to
// treat it as a "normal root" on top of a (UNC) root
rootEnd = offset = j + 1;
}
}
}
}
// Possible device root
} else if (Util::String::test(String(1, ch), "[a-zA-Z]") && path[1] == ':') {
rootEnd = len > 2 && isPathSeparator(path[2]) ? 3 : 2;
offset = rootEnd;
}
size_t end = -1;
size_t matchedSlash = true;
for (size_t i = len - 1; i >= offset; --i) {
if (isPathSeparator(path[i])) {
if (!matchedSlash) {
end = i;
break;
}
} else {
// We saw the first non-path separator
matchedSlash = false;
}
}
if (end == -1) {
if (rootEnd == -1) {
return ".";
}
end = rootEnd;
}
return path.substr(0, end);
}
PathObject createObject () {
Path::PathObject object;
return object;
}
PathObject parse(const String& path) {
PathObject o;
if (path.empty()) {
throw std::runtime_error("Invalid path '" + path + "'");
}
o.root = path[0] == sep() ? String(1, sep()) : "";
o.dir = Path::dirname(path);
o.base = Path::basename(path);
o.ext = Path::extname(path);
o.name = o.base.substr(0, o.base.length() - o.ext.length());
return o;
}
String format(PathObject& o) {
String dir = o.dir.length() > 0 ? o.dir + Path::sep() : "";
return dir + o.base;
}
} // namespace Path
} // namespace Util
} // namespace Hyper