Skip to content
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
14 changes: 8 additions & 6 deletions src/Uri.zig
Original file line number Diff line number Diff line change
Expand Up @@ -449,29 +449,31 @@ pub fn resolveImport(
if (parsed_uri.port) |port| result.printAssumeCapacity(":{d}", .{port});
}
}
var aw: std.Io.Writer.Allocating = .fromArrayList(allocator, &result);
var aw: std.Io.Writer.Allocating = try .initCapacity(allocator, sub_path.len);
defer aw.deinit();

std.Uri.Component.percentEncode(&aw.writer, sub_path, isPathChar) catch unreachable;

const percent_encoded_path = parsed_uri.path.percent_encoded;

const joined_path = try std.fs.path.resolvePosix(allocator, &.{ percent_encoded_path, "..", sub_path });
const joined_path = try std.fs.path.resolvePosix(allocator, &.{ percent_encoded_path, "..", aw.written() });
defer allocator.free(joined_path);

std.Uri.Component.percentEncode(&aw.writer, joined_path, isPathChar) catch unreachable;
try result.appendSlice(allocator, joined_path);

return .{ .raw = try aw.toOwnedSlice() };
return .{ .raw = try result.toOwnedSlice(allocator) };
}

test "resolve" {
const uri: Uri = try .parseWithOs(std.testing.allocator, "file:///dir/main.zig", false);
const uri: Uri = try .parseWithOs(std.testing.allocator, "file:///foo%20bar/main.zig", false);
defer uri.deinit(std.testing.allocator);

const parsed_uri = std.Uri.parse(uri.raw) catch unreachable;

const resolved_uri = try resolveImport(std.testing.allocator, uri, parsed_uri, "foo bar.zig");
defer resolved_uri.deinit(std.testing.allocator);

try std.testing.expectEqualStrings("file:///dir/foo%20bar.zig", resolved_uri.raw);
try std.testing.expectEqualStrings("file:///foo%20bar/foo%20bar.zig", resolved_uri.raw);

var round_trip_uri: Uri = try .parseWithOs(std.testing.allocator, resolved_uri.raw, false);
defer round_trip_uri.deinit(std.testing.allocator);
Expand Down