get_protocol: don't allocate for protocol string

This commit is contained in:
nikneym
2025-10-01 12:29:51 +03:00
committed by Halil Durak
parent 27c9e18535
commit 26c2b258b4
3 changed files with 9 additions and 5 deletions

View File

@@ -281,7 +281,7 @@ pub const HTMLAnchorElement = struct {
// TODO return a disposable string
pub fn get_protocol(self: *parser.Anchor, page: *Page) ![]const u8 {
var u = try url(self, page);
return u.get_protocol(page);
return u.get_protocol();
}
pub fn set_protocol(self: *parser.Anchor, v: []const u8, page: *Page) !void {

View File

@@ -41,8 +41,9 @@ pub const Location = struct {
return page.navigateFromWebAPI(href, .{ .reason = .script });
}
pub fn get_protocol(self: *Location, page: *Page) ![]const u8 {
return self.url.get_protocol(page);
pub fn get_protocol(self: *Location) []const u8 {
if (self.url) |*u| return u.get_protocol();
return "";
}
pub fn get_host(self: *Location, page: *Page) ![]const u8 {

View File

@@ -164,8 +164,11 @@ pub const URL = struct {
return aw.written();
}
pub fn get_protocol(self: *URL, page: *Page) ![]const u8 {
return try std.mem.concat(page.arena, u8, &[_][]const u8{ self.uri.scheme, ":" });
pub fn get_protocol(self: *const URL) []const u8 {
// std.Uri keeps a pointer to "https", "http" (scheme part) so we know
// its followed by ':'.
const scheme = self.uri.scheme;
return scheme.ptr[0 .. scheme.len + 1];
}
pub fn get_username(self: *URL) []const u8 {