From d2f4c0afe17df1c412e97ff27241ecf894c4ced3 Mon Sep 17 00:00:00 2001 From: kayomn Date: Tue, 4 Oct 2022 23:15:59 +0100 Subject: [PATCH] Fix paths being created with trailing "/" always --- src/sys.zig | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/sys.zig b/src/sys.zig index 7864d70..cd0cadd 100644 --- a/src/sys.zig +++ b/src/sys.zig @@ -550,26 +550,39 @@ pub const FileSystem = enum { .length = 0, }; - for (sequences) |sequence| if (sequence.len != 0) { - var components = mem.Spliterator(u8){ - .source = sequence, - .delimiter = "/", - }; + if (sequences.len != 0) { + const last_sequence_index = sequences.len - 1; - while (components.next()) |component| if (component.len != 0) { - for (component) |byte| { + for (sequences) |sequence, index| if (sequence.len != 0) { + var components = mem.Spliterator(u8){ + .source = sequence, + .delimiter = "/", + }; + + while (components.next()) |component| if (component.len != 0) { + for (component) |byte| { + if (path.length == Path.max) return error.TooLong; + + path.buffer[path.length] = byte; + path.length += 1; + } + + if (components.hasNext()) { + if (path.length == Path.max) return error.TooLong; + + path.buffer[path.length] = '/'; + path.length += 1; + } + }; + + if (index < last_sequence_index) { if (path.length == Path.max) return error.TooLong; - path.buffer[path.length] = byte; + path.buffer[path.length] = '/'; path.length += 1; } - - if (path.length == Path.max) return error.TooLong; - - path.buffer[path.length] = '/'; - path.length += 1; }; - }; + } return path; }