replace trimmed_path with path

This commit is contained in:
Halil Durak
2025-11-04 17:24:45 +03:00
parent aa884803e3
commit 97c92d7873

View File

@@ -81,31 +81,31 @@ pub const URL = struct {
/// For URLs without a path, it will add src as the path. /// For URLs without a path, it will add src as the path.
pub fn stitch( pub fn stitch(
allocator: Allocator, allocator: Allocator,
path: []const u8, raw_path: []const u8,
base: []const u8, base: []const u8,
comptime opts: StitchOpts, comptime opts: StitchOpts,
) !StitchReturn(opts) { ) !StitchReturn(opts) {
const trimmed_path = std.mem.trim(u8, path, &.{ '\n', '\r' }); const path = std.mem.trim(u8, raw_path, &.{ '\n', '\r' });
if (base.len == 0 or isCompleteHTTPUrl(trimmed_path)) { if (base.len == 0 or isCompleteHTTPUrl(path)) {
return simpleStitch(allocator, trimmed_path, opts); return simpleStitch(allocator, path, opts);
} }
if (trimmed_path.len == 0) { if (path.len == 0) {
return simpleStitch(allocator, base, opts); return simpleStitch(allocator, base, opts);
} }
if (std.mem.startsWith(u8, trimmed_path, "//")) { if (std.mem.startsWith(u8, path, "//")) {
// network-path reference // network-path reference
const index = std.mem.indexOfScalar(u8, base, ':') orelse { const index = std.mem.indexOfScalar(u8, base, ':') orelse {
return simpleStitch(allocator, trimmed_path, opts); return simpleStitch(allocator, path, opts);
}; };
const protocol = base[0..index]; const protocol = base[0..index];
if (comptime opts.null_terminated) { if (comptime opts.null_terminated) {
return std.fmt.allocPrintSentinel(allocator, "{s}:{s}", .{ protocol, trimmed_path }, 0); return std.fmt.allocPrintSentinel(allocator, "{s}:{s}", .{ protocol, path }, 0);
} }
return std.fmt.allocPrint(allocator, "{s}:{s}", .{ protocol, trimmed_path }); return std.fmt.allocPrint(allocator, "{s}:{s}", .{ protocol, path });
} }
// Quick hack because domains have to be at least 3 characters. // Quick hack because domains have to be at least 3 characters.
@@ -119,11 +119,11 @@ pub const URL = struct {
root = base[0 .. pos + protocol_end]; root = base[0 .. pos + protocol_end];
} }
if (trimmed_path[0] == '/') { if (path[0] == '/') {
if (comptime opts.null_terminated) { if (comptime opts.null_terminated) {
return std.fmt.allocPrintSentinel(allocator, "{s}{s}", .{ root, trimmed_path }, 0); return std.fmt.allocPrintSentinel(allocator, "{s}{s}", .{ root, path }, 0);
} }
return std.fmt.allocPrint(allocator, "{s}{s}", .{ root, trimmed_path }); return std.fmt.allocPrint(allocator, "{s}{s}", .{ root, path });
} }
var old_path = std.mem.trimStart(u8, base[root.len..], "/"); var old_path = std.mem.trimStart(u8, base[root.len..], "/");
@@ -135,7 +135,7 @@ pub const URL = struct {
// We preallocate all of the space possibly needed. // We preallocate all of the space possibly needed.
// This is the root, old_path, new path, 3 slashes and perhaps a null terminated slot. // This is the root, old_path, new path, 3 slashes and perhaps a null terminated slot.
var out = try allocator.alloc(u8, root.len + old_path.len + trimmed_path.len + 3 + if (comptime opts.null_terminated) 1 else 0); var out = try allocator.alloc(u8, root.len + old_path.len + path.len + 3 + if (comptime opts.null_terminated) 1 else 0);
var end: usize = 0; var end: usize = 0;
@memmove(out[0..root.len], root); @memmove(out[0..root.len], root);
end += root.len; end += root.len;
@@ -148,8 +148,8 @@ pub const URL = struct {
out[end] = '/'; out[end] = '/';
end += 1; end += 1;
} }
@memmove(out[end .. end + trimmed_path.len], trimmed_path); @memmove(out[end .. end + path.len], path);
end += trimmed_path.len; end += path.len;
var read: usize = root.len; var read: usize = root.len;
var write: usize = root.len; var write: usize = root.len;