Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

normalize backslash on windows to forward slash #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/util.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn normalizePath(allocator: std.mem.Allocator, path: []const u8) ![]u8 {

if (components.root()) |root| {
try buffer.appendSlice(root);
if (!lastIsSep(buffer.items)) try buffer.append(std.fs.path.sep);
if (!lastIsSep(buffer.items)) try buffer.append('/');
}

while (components.next()) |component| {
Expand All @@ -45,7 +45,7 @@ pub fn normalizePath(allocator: std.mem.Allocator, path: []const u8) ![]u8 {
} else if (std.mem.eql(u8, component.name, ".")) {
continue;
} else {
if (buffer.items.len != 0 and !lastIsSep(buffer.items)) try buffer.append(std.fs.path.sep);
if (buffer.items.len != 0 and !lastIsSep(buffer.items)) try buffer.append('/');
try buffer.appendSlice(component.name);
}
}
Expand All @@ -57,9 +57,13 @@ fn lastIsSep(path: []const u8) bool {
return std.fs.path.isSep(path[path.len - 1]);
}

test "normalizePath" {
test normalizePath {
try expectNormalizedPath("C:/bar", "C:/blah/../bar/.");
try expectNormalizedPath("/bar", "/../bar/.");

if (@import("builtin").target.os.tag == .windows) {
try expectNormalizedPath("foo/bar/baz", "foo\\bar\\baz");
}
}

fn expectNormalizedPath(expected: []const u8, input: []const u8) !void {
Expand Down
Loading