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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
const tester = @import("../test/tester.zig");
const std = @import("std");
threadlocal var parser_join_input_buffer: [1024]u8 = undefined;
threadlocal var parser_buffer: [1024]u8 = undefined;
// This function is based on Node.js' path.normalize function.
// https://github.com/nodejs/node/blob/36bb31be5f0b85a0f6cbcb36b64feb3a12c60984/lib/path.js#L66
pub fn normalizeStringGeneric(str: []const u8, buf: []u8, comptime allow_above_root: bool, comptime separator: u8, comptime isPathSeparator: anytype, lastIndexOfSeparator: anytype) []u8 {
var i: usize = 0;
var last_segment_length: i32 = 0;
var last_slash: i32 = -1;
var dots: i32 = 0;
var code: u8 = 0;
var written_len: usize = 0;
const stop_len = str.len;
while (i <= stop_len) : (i += 1) {
if (i < stop_len) {
code = str[i];
} else if (@call(std.builtin.CallOptions{ .modifier = .always_inline }, isPathSeparator, .{code})) {
break;
} else {
code = separator;
}
if (@call(std.builtin.CallOptions{ .modifier = .always_inline }, isPathSeparator, .{code})) {
if (last_slash == @intCast(i32, i) - 1 or dots == 1) {
// NOOP
} else if (dots == 2) {
if (written_len < 2 or last_segment_length != 2 or buf[written_len - 1] != '.' or buf[written_len - 2] != '.') {
if (written_len > 2) {
if (lastIndexOfSeparator(buf[0..written_len])) |last_slash_index| {
written_len = last_slash_index;
last_segment_length = @intCast(i32, written_len - 1 - (lastIndexOfSeparator(buf[0..written_len]) orelse 0));
} else {
written_len = 0;
}
last_slash = @intCast(i32, i);
dots = 0;
continue;
} else if (written_len != 0) {
written_len = 0;
last_segment_length = 0;
last_slash = @intCast(i32, i);
dots = 0;
continue;
}
if (allow_above_root) {
if (written_len > 0) {
buf[written_len] = separator;
written_len += 1;
}
buf[written_len] = '.';
written_len += 1;
buf[written_len] = '.';
written_len += 1;
last_segment_length = 2;
}
}
} else {
if (written_len > 0) {
buf[written_len] = separator;
written_len += 1;
}
const slice = str[@intCast(usize, @intCast(usize, last_slash + 1))..i];
std.mem.copy(u8, buf[written_len .. written_len + slice.len], slice);
written_len += slice.len;
last_segment_length = @intCast(i32, i) - last_slash - 1;
}
last_slash = @intCast(i32, i);
dots = 0;
} else if (code == '.' and dots != -1) {
dots += 1;
} else {
dots = -1;
}
}
return buf[0..written_len];
}
pub const Platform = enum {
auto,
loose,
windows,
posix,
pub fn isSeparator(comptime _platform: Platform, char: u8) bool {
const platform = _platform.resolve();
switch (platform) {
.auto => unreachable,
.loose => {
return isSepAny(char);
},
.windows => {
return isSepWin32(char);
},
.posix => {
return isSepPosix(char);
},
}
}
pub fn leadingSeparatorIndex(comptime _platform: Platform, path: anytype) ?usize {
switch (_platform.resolve()) {
.windows => {
if (path.len < 1)
return null;
if (path[0] == '/')
return 0;
if (path[0] == '\\')
return 0;
if (path.len < 3)
return null;
// C:\
// C:/
if (path[0] >= 'A' and path[0] <= 'Z' and path[1] == ':') {
if (path[2] == '/')
return 2;
if (path[2] == '\\')
return 2;
}
return null;
},
.posix => {
if (path.len > 0 and path[0] == '/') {
return 0;
} else {
return null;
}
},
else => {
return leadingSeparatorIndex(.windows, path) orelse leadingSeparatorIndex(.posix, path);
},
}
}
pub fn resolve(comptime _platform: Platform) Platform {
if (_platform == .auto) {
switch (std.Target.current.os.tag) {
.windows => {
return .windows;
},
.freestanding, .emscripten, .other => {
return .loose;
},
else => {
return .posix;
},
}
}
return _platform;
}
};
pub fn normalizeString(str: []const u8, comptime allow_above_root: bool, comptime _platform: Platform) []u8 {
return normalizeStringBuf(str, &parser_buffer, allow_above_root, _platform);
}
pub fn normalizeStringBuf(str: []const u8, buf: []u8, comptime allow_above_root: bool, comptime _platform: Platform) []u8 {
comptime const platform = _platform.resolve();
switch (platform) {
.auto => unreachable,
.windows => {
return normalizeStringWindowsBuf(str, buf, allow_above_root);
},
.posix => {
return normalizeStringPosixBuf(str, buf, allow_above_root);
},
.loose => {
return normalizeStringLooseBuf(str, buf, allow_above_root);
},
}
}
pub fn normalizeStringAlloc(allocator: *std.mem.Allocator, str: []const u8, comptime allow_above_root: bool, comptime _platform: Platform) ![]const u8 {
return try allocator.dupe(u8, normalizeString(str, allow_above_root, _platform));
}
pub fn normalizeAndJoin2(_cwd: []const u8, comptime _platform: Platform, part: anytype, part2: anytype) []const u8 {
const parts = [_][]const u8{ part, part2 };
const slice = normalizeAndJoinString(_cwd, &parts, _platform);
return slice;
}
pub fn normalizeAndJoin(_cwd: []const u8, comptime _platform: Platform, part: anytype) []const u8 {
const parts = [_][]const u8{
part,
};
const slice = normalizeAndJoinString(_cwd, &parts, _platform);
return slice;
}
// Convert parts of potentially invalid file paths into a single valid filpeath
// without querying the filesystem
// This is the equivalent of
pub fn normalizeAndJoinString(_cwd: []const u8, parts: anytype, comptime _platform: Platform) []const u8 {
return normalizeAndJoinStringBuf(_cwd, &parser_join_input_buffer, parts, _platform);
}
pub fn normalizeAndJoinStringBuf(_cwd: []const u8, buf: []u8, parts: anytype, comptime _platform: Platform) []const u8 {
if (parts.len == 0) {
return _cwd;
}
if ((_platform == .loose or _platform == .posix) and parts.len == 1 and parts[0].len == 1 and parts[0] == std.fs.path.sep_posix) {
return "/";
}
var cwd = _cwd;
var out: usize = 0;
// When parts[0] is absolute, we treat that as, effectively, the cwd
var ignore_cwd = cwd.len == 0;
// Windows leading separators can be a lot of things...
// So we need to do this instead of just checking the first char.
var leading_separator: []const u8 = "";
if (_platform.leadingSeparatorIndex(parts[0])) |leading_separator_i| {
leading_separator = parts[0][0 .. leading_separator_i + 1];
ignore_cwd = true;
}
if (!ignore_cwd) {
leading_separator = cwd[0 .. 1 + (_platform.leadingSeparatorIndex(_cwd) orelse unreachable)]; // cwd must be absolute
cwd = _cwd[leading_separator.len..cwd.len];
out = cwd.len;
std.debug.assert(out < buf.len);
std.mem.copy(u8, buf[0..out], cwd);
}
for (parts) |part, i| {
// This never returns leading separators.
var normalized_part = normalizeString(part, true, _platform);
if (normalized_part.len == 0) {
continue;
}
switch (_platform.resolve()) {
.windows => {
buf[out] = std.fs.path.sep_windows;
},
else => {
buf[out] = std.fs.path.sep_posix;
},
}
out += 1;
const start = out;
out += normalized_part.len;
std.debug.assert(out < buf.len);
std.mem.copy(u8, buf[start..out], normalized_part);
}
// One last normalization, to remove any ../ added
const result = normalizeStringBuf(buf[0..out], parser_buffer[leading_separator.len..parser_buffer.len], false, _platform);
std.mem.copy(u8, buf[0..leading_separator.len], leading_separator);
std.mem.copy(u8, buf[leading_separator.len .. result.len + leading_separator.len], result);
return buf[0 .. result.len + leading_separator.len];
}
pub fn isSepPosix(char: u8) bool {
return char == std.fs.path.sep_posix;
}
pub fn isSepWin32(char: u8) bool {
return char == std.fs.path.sep_windows;
}
pub fn isSepAny(char: u8) bool {
return @call(.{ .modifier = .always_inline }, isSepPosix, .{char}) or @call(.{ .modifier = .always_inline }, isSepWin32, .{char});
}
pub fn lastIndexOfSeparatorWindows(slice: []const u8) ?usize {
return std.mem.lastIndexOfScalar(u8, slice, std.fs.path.sep_windows);
}
pub fn lastIndexOfSeparatorPosix(slice: []const u8) ?usize {
return std.mem.lastIndexOfScalar(u8, slice, std.fs.path.sep_posix);
}
pub fn lastIndexOfSeparatorLoose(slice: []const u8) ?usize {
return std.mem.lastIndexOfAny(u8, slice, "/\\");
}
pub fn normalizeStringPosix(str: []const u8, comptime allow_above_root: bool) []u8 {
return normalizeStringGenericBuf(str, &parser_buffer, allow_above_root, std.fs.path.sep_posix, isSepPosix, lastIndexOfSeparatorPosix);
}
pub fn normalizeStringPosixBuf(str: []const u8, buf: []u8, comptime allow_above_root: bool) []u8 {
return normalizeStringGeneric(str, buf, allow_above_root, std.fs.path.sep_posix, isSepPosix, lastIndexOfSeparatorPosix);
}
pub fn normalizeStringWindows(str: []const u8, comptime allow_above_root: bool) []u8 {
return normalizeStringGenericBuf(str, &parser_buffer, allow_above_root, std.fs.path.sep_windows, isSepWin32, lastIndexOfSeparatorWindows);
}
pub fn normalizeStringWindowsBuf(str: []const u8, buf: []u8, comptime allow_above_root: bool) []u8 {
return normalizeStringGeneric(str, buf, allow_above_root, std.fs.path.sep_windows, isSepWin32, lastIndexOfSeparatorWindows);
}
pub fn normalizeStringLoose(str: []const u8, comptime allow_above_root: bool) []u8 {
return normalizeStringGenericBuf(str, &parser_buffer, allow_above_root, std.fs.path.sep_posix, isSepAny, lastIndexOfSeparatorLoose);
}
pub fn normalizeStringLooseBuf(str: []const u8, buf: []u8, comptime allow_above_root: bool) []u8 {
return normalizeStringGeneric(str, buf, allow_above_root, std.fs.path.sep_posix, isSepAny, lastIndexOfSeparatorLoose);
}
test "normalizeAndJoinStringPosix" {
var t = tester.Tester.t(std.heap.c_allocator);
defer t.report(@src());
const string = []const u8;
const cwd = "/Users/jarredsumner/Code/app";
_ = t.expect(
"/Users/jarredsumner/Code/app/foo/bar/file.js",
normalizeAndJoinString(cwd, [_]string{ "foo", "bar", "file.js" }, .posix),
@src(),
);
_ = t.expect(
"/Users/jarredsumner/Code/app/foo/file.js",
normalizeAndJoinString(cwd, [_]string{ "foo", "bar", "../file.js" }, .posix),
@src(),
);
_ = t.expect(
"/Users/jarredsumner/Code/app/foo/file.js",
normalizeAndJoinString(cwd, [_]string{ "foo", "./bar", "../file.js" }, .posix),
@src(),
);
_ = t.expect(
"/Users/jarredsumner/Code/app/foo/file.js",
normalizeAndJoinString(cwd, [_]string{ "././././foo", "././././bar././././", "../file.js" }, .posix),
@src(),
);
_ = t.expect(
"/Code/app/foo/file.js",
normalizeAndJoinString(cwd, [_]string{ "/Code/app", "././././foo", "././././bar././././", "../file.js" }, .posix),
@src(),
);
_ = t.expect(
"/Code/app/foo/file.js",
normalizeAndJoinString(cwd, [_]string{ "/Code/app", "././././foo", ".", "././././bar././././", ".", "../file.js" }, .posix),
@src(),
);
_ = t.expect(
"/Code/app/file.js",
normalizeAndJoinString(cwd, [_]string{ "/Code/app", "././././foo", "..", "././././bar././././", ".", "../file.js" }, .posix),
@src(),
);
}
test "normalizeAndJoinStringLoose" {
var t = tester.Tester.t(std.heap.c_allocator);
defer t.report(@src());
const string = []const u8;
const cwd = "/Users/jarredsumner/Code/app";
_ = t.expect(
"/Users/jarredsumner/Code/app/foo/bar/file.js",
normalizeAndJoinString(cwd, [_]string{ "foo", "bar", "file.js" }, .loose),
@src(),
);
_ = t.expect(
"/Users/jarredsumner/Code/app/foo/file.js",
normalizeAndJoinString(cwd, [_]string{ "foo", "bar", "../file.js" }, .loose),
@src(),
);
_ = t.expect(
"/Users/jarredsumner/Code/app/foo/file.js",
normalizeAndJoinString(cwd, [_]string{ "foo", "./bar", "../file.js" }, .loose),
@src(),
);
_ = t.expect(
"/Users/jarredsumner/Code/app/foo/file.js",
normalizeAndJoinString(cwd, [_]string{ "././././foo", "././././bar././././", "../file.js" }, .loose),
@src(),
);
_ = t.expect(
"/Code/app/foo/file.js",
normalizeAndJoinString(cwd, [_]string{ "/Code/app", "././././foo", "././././bar././././", "../file.js" }, .loose),
@src(),
);
_ = t.expect(
"/Code/app/foo/file.js",
normalizeAndJoinString(cwd, [_]string{ "/Code/app", "././././foo", ".", "././././bar././././", ".", "../file.js" }, .loose),
@src(),
);
_ = t.expect(
"/Code/app/file.js",
normalizeAndJoinString(cwd, [_]string{ "/Code/app", "././././foo", "..", "././././bar././././", ".", "../file.js" }, .loose),
@src(),
);
_ = t.expect(
"/Users/jarredsumner/Code/app/foo/bar/file.js",
normalizeAndJoinString(cwd, [_]string{ "foo", "bar", "file.js" }, .loose),
@src(),
);
_ = t.expect(
"/Users/jarredsumner/Code/app/foo/file.js",
normalizeAndJoinString(cwd, [_]string{ "foo", "bar", "../file.js" }, .loose),
@src(),
);
_ = t.expect(
"/Users/jarredsumner/Code/app/foo/file.js",
normalizeAndJoinString(cwd, [_]string{ "foo", "./bar", "../file.js" }, .loose),
@src(),
);
_ = t.expect(
"/Users/jarredsumner/Code/app/foo/file.js",
normalizeAndJoinString(cwd, [_]string{ ".\\.\\.\\.\\foo", "././././bar././././", "..\\file.js" }, .loose),
@src(),
);
_ = t.expect(
"/Code/app/foo/file.js",
normalizeAndJoinString(cwd, [_]string{ "/Code/app", "././././foo", "././././bar././././", "../file.js" }, .loose),
@src(),
);
_ = t.expect(
"/Code/app/foo/file.js",
normalizeAndJoinString(cwd, [_]string{ "/Code/app", "././././foo", ".", "././././bar././././", ".", "../file.js" }, .loose),
@src(),
);
_ = t.expect(
"/Code/app/file.js",
normalizeAndJoinString(cwd, [_]string{ "/Code/app", "././././foo", "..", "././././bar././././", ".", "../file.js" }, .loose),
@src(),
);
}
test "normalizeStringPosix" {
var t = tester.Tester.t(std.heap.c_allocator);
defer t.report(@src());
// Don't mess up strings that
_ = t.expect("foo/bar.txt", try normalizeStringAlloc(std.heap.c_allocator, "/foo/bar.txt", true, .posix), @src());
_ = t.expect("foo/bar.txt", try normalizeStringAlloc(std.heap.c_allocator, "/foo/bar.txt", false, .posix), @src());
_ = t.expect("foo/bar", try normalizeStringAlloc(std.heap.c_allocator, "/foo/bar", true, .posix), @src());
_ = t.expect("foo/bar", try normalizeStringAlloc(std.heap.c_allocator, "/foo/bar", false, .posix), @src());
_ = t.expect("foo/bar", try normalizeStringAlloc(std.heap.c_allocator, "/././foo/././././././bar/../bar/../bar", true, .posix), @src());
_ = t.expect("foo/bar", try normalizeStringAlloc(std.heap.c_allocator, "/foo/bar", false, .posix), @src());
_ = t.expect("foo/bar", try normalizeStringAlloc(std.heap.c_allocator, "/foo/bar//////", false, .posix), @src());
_ = t.expect("foo/bar", try normalizeStringAlloc(std.heap.c_allocator, "/////foo/bar//////", false, .posix), @src());
_ = t.expect("foo/bar", try normalizeStringAlloc(std.heap.c_allocator, "/////foo/bar", false, .posix), @src());
_ = t.expect("", try normalizeStringAlloc(std.heap.c_allocator, "/////", false, .posix), @src());
_ = t.expect("..", try normalizeStringAlloc(std.heap.c_allocator, "../boom/../", true, .posix), @src());
_ = t.expect("", try normalizeStringAlloc(std.heap.c_allocator, "./", true, .posix), @src());
}
test "normalizeStringWindows" {
var t = tester.Tester.t(std.heap.c_allocator);
defer t.report(@src());
// Don't mess up strings that
_ = t.expect("foo\\bar.txt", try normalizeStringAlloc(std.heap.c_allocator, "\\foo\\bar.txt", true, .windows), @src());
_ = t.expect("foo\\bar.txt", try normalizeStringAlloc(std.heap.c_allocator, "\\foo\\bar.txt", false, .windows), @src());
_ = t.expect("foo\\bar", try normalizeStringAlloc(std.heap.c_allocator, "\\foo\\bar", true, .windows), @src());
_ = t.expect("foo\\bar", try normalizeStringAlloc(std.heap.c_allocator, "\\foo\\bar", false, .windows), @src());
_ = t.expect("foo\\bar", try normalizeStringAlloc(std.heap.c_allocator, "\\.\\.\\foo\\.\\.\\.\\.\\.\\.\\bar\\..\\bar\\..\\bar", true, .windows), @src());
_ = t.expect("foo\\bar", try normalizeStringAlloc(std.heap.c_allocator, "\\foo\\bar", false, .windows), @src());
_ = t.expect("foo\\bar", try normalizeStringAlloc(std.heap.c_allocator, "\\foo\\bar\\\\\\\\\\\\", false, .windows), @src());
_ = t.expect("foo\\bar", try normalizeStringAlloc(std.heap.c_allocator, "\\\\\\\\\\foo\\bar\\\\\\\\\\\\", false, .windows), @src());
_ = t.expect("foo\\bar", try normalizeStringAlloc(std.heap.c_allocator, "\\\\\\\\\\foo\\bar", false, .windows), @src());
_ = t.expect("", try normalizeStringAlloc(std.heap.c_allocator, "\\\\\\\\\\", false, .windows), @src());
_ = t.expect("..", try normalizeStringAlloc(std.heap.c_allocator, "..\\boom\\..\\", true, .windows), @src());
_ = t.expect("", try normalizeStringAlloc(std.heap.c_allocator, ".\\", true, .windows), @src());
}
|