aboutsummaryrefslogtreecommitdiff
path: root/src/string_immutable.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-11-22 21:25:26 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-11-22 21:25:26 -0800
commit4f41c3fb40a7cd23a48c1a888353059a69693fa8 (patch)
tree03d543e4471514c4e87ffbc371d5c60015339793 /src/string_immutable.zig
parentb249ed7257777f2d9b5fe18ba09694d75f0d0a90 (diff)
downloadbun-4f41c3fb40a7cd23a48c1a888353059a69693fa8.tar.gz
bun-4f41c3fb40a7cd23a48c1a888353059a69693fa8.tar.zst
bun-4f41c3fb40a7cd23a48c1a888353059a69693fa8.zip
[FileSystemRouter] Fix failing tests
Diffstat (limited to 'src/string_immutable.zig')
-rw-r--r--src/string_immutable.zig29
1 files changed, 29 insertions, 0 deletions
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)];
+}