Skip to content

Commit cecdd47

Browse files
committed
bind more ada functions
also includes a fix for releasing memory if parsing failed
1 parent 900c8d2 commit cecdd47

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

vendor/ada/root.zig

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub const ParseError = error{Invalid};
1919
pub fn parse(input: []const u8) ParseError!URL {
2020
const url = c.ada_parse(input.ptr, input.len);
2121
if (!c.ada_is_valid(url)) {
22-
free(url);
2322
return error.Invalid;
2423
}
2524

@@ -29,7 +28,6 @@ pub fn parse(input: []const u8) ParseError!URL {
2928
pub fn parseWithBase(input: []const u8, base: []const u8) ParseError!URL {
3029
const url = c.ada_parse_with_base(input.ptr, input.len, base.ptr, base.len);
3130
if (!c.ada_is_valid(url)) {
32-
free(url);
3331
return error.Invalid;
3432
}
3533

@@ -53,6 +51,11 @@ pub inline fn isValid(url: URL) bool {
5351
return c.ada_is_valid(url);
5452
}
5553

54+
/// Creates a new `URL` from given `URL`.
55+
pub inline fn copy(url: URL) URL {
56+
return c.ada_copy(url);
57+
}
58+
5659
/// Can return an empty string.
5760
/// Contrary to other getters, returned slice is heap allocated.
5861
pub inline fn getOrigin(url: URL) []const u8 {
@@ -92,9 +95,18 @@ pub inline fn getHash(url: URL) []const u8 {
9295
return hash.data[0..hash.length];
9396
}
9497

95-
/// Returns an empty string if not provided.
98+
pub inline fn getHashNullable(url: URL) String {
99+
return c.ada_get_hash(url);
100+
}
101+
102+
/// `data` is null if host not provided.
103+
pub inline fn getHostNullable(url: URL) String {
104+
return c.ada_get_host(url);
105+
}
106+
107+
/// Returns an empty string if host not provided.
96108
pub inline fn getHost(url: URL) []const u8 {
97-
const host = c.ada_get_host(url);
109+
const host = getHostNullable(url);
98110
if (host.data == null) {
99111
return "";
100112
}
@@ -111,6 +123,10 @@ pub inline fn getHostname(url: URL) []const u8 {
111123
return hostname.data[0..hostname.length];
112124
}
113125

126+
pub inline fn getPathnameNullable(url: URL) String {
127+
return c.ada_get_pathname(url);
128+
}
129+
114130
pub inline fn getPathname(url: URL) []const u8 {
115131
const pathname = c.ada_get_pathname(url);
116132
return pathname.data[0..pathname.length];
@@ -168,3 +184,18 @@ pub inline fn setHash(url: URL, input: []const u8) void {
168184
pub inline fn clearSearch(url: URL) void {
169185
return c.ada_clear_search(url);
170186
}
187+
188+
pub const Scheme = struct {
189+
pub const http: u8 = 0;
190+
pub const not_special: u8 = 1;
191+
pub const https: u8 = 2;
192+
pub const ws: u8 = 3;
193+
pub const ftp: u8 = 4;
194+
pub const wss: u8 = 5;
195+
pub const file: u8 = 6;
196+
};
197+
198+
/// Returns one of the constants defined in `Scheme`.
199+
pub inline fn getSchemeType(url: URL) u8 {
200+
return c.ada_get_scheme_type(url);
201+
}

0 commit comments

Comments
 (0)