@@ -9,26 +9,34 @@ const Values = @import("values.zig").Values;
99
1010pub const URL = @This ();
1111
12- allocator : std.mem.Allocator = std .heap .page_allocator ,
12+ allocator : std.mem.Allocator ,
13+
1314uri : Uri = undefined ,
1415scheme : ? []const u8 = undefined ,
1516host : ? []const u8 = undefined ,
1617path : []const u8 = "/" ,
1718fragment : ? []const u8 = undefined ,
1819query : ? []const u8 = undefined ,
1920
20- // querymap: ?StringHashMap(std.ArrayList([]const u8)) = StringHashMap(std.ArrayList([]const u8)).init(std.heap.page_allocator),
21- values : ? std .StringHashMap (std .ArrayList ([]const u8 )) = std . StringHashMap ( std . ArrayList ([] const u8 )). init ( std . heap . page_allocator ) ,
21+ // querymap: ?StringHashMap(std.ArrayList([]const u8))
22+ values : ? std .StringHashMap (std .ArrayList ([]const u8 )) = undefined ,
2223
2324// https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Web_mechanics/What_is_a_URL
2425
2526pub fn init (self : URL ) URL {
2627 return .{
2728 .allocator = self .allocator ,
28- // .querymap = self.querymap ,
29+ . values = std . StringHashMap ( std . ArrayList ([] const u8 )). init ( self .allocator ) ,
2930 };
3031}
3132
33+ pub fn deinit (self : * URL ) void {
34+ if (self .values != null ) {
35+ self .values .? .deinit ();
36+ }
37+ // self.allocator.destroy(self);
38+ }
39+
3240const SliceReader = struct {
3341 const Self = @This ();
3442
@@ -93,7 +101,9 @@ fn uriToUrl(self: *URL, uri: Uri) void {
93101
94102 if (uri .query != null ) {
95103 self .query = @constCast (uri .query .? .percent_encoded );
96- try parseQuery (& self .values .? , @constCast (uri .query .? .percent_encoded ));
104+ if (self .values != null ) {
105+ try parseQuery (& self .values .? , @constCast (uri .query .? .percent_encoded ));
106+ }
97107 }
98108 if (uri .fragment != null ) {
99109 self .fragment = @constCast (uri .fragment .? .percent_encoded );
@@ -108,6 +118,8 @@ pub fn parseUri(self: *URL, text: []const u8) ParseError!*URL {
108118}
109119
110120pub fn parseQuery (map : * std .StringHashMap (std .ArrayList ([]const u8 )), uri_query : []const u8 ) ! void {
121+ const allocator = std .heap .page_allocator ;
122+
111123 var queryitmes = std .mem .splitSequence (u8 , uri_query , "&" );
112124 while (true ) {
113125 const pair = queryitmes .next ();
@@ -131,7 +143,7 @@ pub fn parseQuery(map: *std.StringHashMap(std.ArrayList([]const u8)), uri_query:
131143 var al : std .ArrayList ([]const u8 ) = undefined ;
132144 const v = map .get (key .? );
133145 if (v == null ) {
134- al = std .ArrayList ([]const u8 ).init (std . heap . page_allocator );
146+ al = std .ArrayList ([]const u8 ).init (allocator );
135147 al .append (value .? ) catch continue ;
136148 map .put (key .? , al ) catch continue ;
137149 continue ;
0 commit comments