@@ -81,16 +81,34 @@ pub const URL = struct {
8181 /// For URLs without a path, it will add src as the path.
8282 pub fn stitch (
8383 allocator : Allocator ,
84- path : []const u8 ,
85- base : []const u8 ,
84+ _path : []const u8 ,
85+ _base : []const u8 ,
8686 comptime opts : StitchOpts ,
8787 ) ! StitchReturn (opts ) {
88- if (base .len == 0 or isCompleteHTTPUrl (path )) {
89- return simpleStitch (allocator , path , opts );
88+ if (_base .len == 0 or isCompleteHTTPUrl (_path )) {
89+ return simpleStitch (allocator , _path , opts );
9090 }
9191
92+ if (_path .len == 0 ) {
93+ return simpleStitch (allocator , _base , opts );
94+ }
95+
96+ // base should get stripped of its hash whenever we are stitching.
97+ const base = if (std .mem .indexOfScalar (u8 , _base , '#' )) | hash_pos |
98+ _base [0.. hash_pos ]
99+ else
100+ _base ;
101+
102+ const path_hash_start = std .mem .indexOfScalar (u8 , _path , '#' );
103+ const path = if (path_hash_start ) | pos | _path [0.. pos ] else _path ;
104+ const hash = if (path_hash_start ) | pos | _path [pos .. ] else "" ;
105+
106+ // if path is just hash, we just append it to base.
92107 if (path .len == 0 ) {
93- return simpleStitch (allocator , base , opts );
108+ if (comptime opts .null_terminated ) {
109+ return std .fmt .allocPrintSentinel (allocator , "{s}{s}" , .{ base , hash }, 0 );
110+ }
111+ return std .fmt .allocPrint (allocator , "{s}{s}" , .{ base , hash });
94112 }
95113
96114 if (std .mem .startsWith (u8 , path , "//" )) {
@@ -101,9 +119,9 @@ pub const URL = struct {
101119
102120 const protocol = base [0.. index ];
103121 if (comptime opts .null_terminated ) {
104- return std .fmt .allocPrintSentinel (allocator , "{s}:{s}" , .{ protocol , path }, 0 );
122+ return std .fmt .allocPrintSentinel (allocator , "{s}:{s}{s} " , .{ protocol , path , hash }, 0 );
105123 }
106- return std .fmt .allocPrint (allocator , "{s}:{s}" , .{ protocol , path });
124+ return std .fmt .allocPrint (allocator , "{s}:{s}{s} " , .{ protocol , path , hash });
107125 }
108126
109127 // Quick hack because domains have to be at least 3 characters.
@@ -133,7 +151,10 @@ pub const URL = struct {
133151
134152 // We preallocate all of the space possibly needed.
135153 // This is the root, old_path, new path, 3 slashes and perhaps a null terminated slot.
136- var out = try allocator .alloc (u8 , root .len + old_path .len + path .len + 3 + if (comptime opts .null_terminated ) 1 else 0 );
154+ var out = try allocator .alloc (
155+ u8 ,
156+ root .len + old_path .len + path .len + hash .len + 3 + if (comptime opts .null_terminated ) 1 else 0 ,
157+ );
137158 var end : usize = 0 ;
138159 @memmove (out [0.. root .len ], root );
139160 end += root .len ;
@@ -180,6 +201,11 @@ pub const URL = struct {
180201 read += 1 ;
181202 }
182203
204+ if (hash .len > 0 ) {
205+ @memmove (out [write .. write + hash .len ], hash );
206+ write += hash .len ;
207+ }
208+
183209 if (comptime opts .null_terminated ) {
184210 // we always have an extra space
185211 out [write ] = 0 ;
0 commit comments