diff options
| author | Grace Yoder <grace_yoder@outlook.com> | 2026-07-17 19:54:15 -0600 |
|---|---|---|
| committer | Grace Yoder <grace_yoder@outlook.com> | 2026-07-17 20:08:24 -0600 |
| commit | 55a1697e7175c4c82989c0633ef2227fb0295b8e (patch) | |
| tree | e3592665ca7f051849debe716a2cfc2f2592110b /src/utils.zig | |
Diffstat (limited to 'src/utils.zig')
| -rwxr-xr-x | src/utils.zig | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/utils.zig b/src/utils.zig new file mode 100755 index 0000000..ae03d76 --- /dev/null +++ b/src/utils.zig @@ -0,0 +1,23 @@ +const std = @import("std"); +const Io = std.Io; +const File = Io.File; +const Allocator = std.mem.Allocator; +const ArenaAllocator = std.heap.ArenaAllocator; +const ArrayList = std.ArrayList; +const StringArrayHashMap = std.StringArrayHashMapUnmanaged; + +pub fn sanitizePath(alloc: Allocator, path: []const u8) ![]const u8 { + const clean = try alloc.alloc(u8, path.len); + var list = ArrayList(u8).initBuffer(clean); + var pit = std.mem.splitScalar(u8, path, '/'); + while (pit.next()) |part| { + if (part.len == 0) continue; + if (std.mem.eql(u8, part, "..")) continue; + // This should never actually allocate + try list.appendSlice(alloc, part); + try list.append(alloc, '/'); + } + const new_len = list.items.len - 1; + _ = alloc.resize(clean, new_len); + return clean[0..new_len]; +} |
