From 4f41c3fb40a7cd23a48c1a888353059a69693fa8 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Tue, 22 Nov 2022 21:25:26 -0800 Subject: [FileSystemRouter] Fix failing tests --- src/string_immutable.zig | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/string_immutable.zig') diff --git a/src/string_immutable.zig b/src/string_immutable.zig index 654080f8a..5acc5befd 100644 --- a/src/string_immutable.zig +++ b/src/string_immutable.zig @@ -3762,3 +3762,32 @@ pub fn isIPAddress(input: []const u8) bool { return false; } } + +pub fn cloneNormalizingSeparators( + allocator: std.mem.Allocator, + input: []const u8, +) ![]u8 { + // remove duplicate slashes in the file path + var base = withoutTrailingSlash(input); + var tokenized = std.mem.tokenize(u8, base, std.fs.path.sep_str); + var buf = try allocator.alloc(u8, base.len + 2); + std.debug.assert(base.len > 0); + if (base[0] == std.fs.path.sep) { + buf[0] = std.fs.path.sep; + } + var remain = buf[@as(usize, @boolToInt(base[0] == std.fs.path.sep))..]; + + while (tokenized.next()) |token| { + if (token.len == 0) continue; + std.mem.copy(u8, remain, token); + remain[token.len..][0] = std.fs.path.sep; + remain = remain[token.len + 1 ..]; + } + if ((remain.ptr - 1) != buf.ptr and (remain.ptr - 1)[0] != std.fs.path.sep) { + remain[0] = std.fs.path.sep; + remain = remain[1..]; + } + remain[0] = 0; + + return buf[0 .. @ptrToInt(remain.ptr) - @ptrToInt(buf.ptr)]; +} -- cgit v1.2.3