@@ -83,12 +83,28 @@ pub const URL = struct {
8383 return WebApiURL .init (allocator , self .uri );
8484 }
8585
86+ const StitchOpts = struct {
87+ alloc : AllocWhen = .always ,
88+
89+ const AllocWhen = enum {
90+ always ,
91+ if_needed ,
92+ };
93+ };
8694 /// Properly stitches two URL fragments together.
8795 ///
8896 /// For URLs with a path, it will replace the last entry with the src.
8997 /// For URLs without a path, it will add src as the path.
90- pub fn stitch (allocator : Allocator , src : []const u8 , base : []const u8 ) ! []const u8 {
98+ pub fn stitch (
99+ allocator : Allocator ,
100+ src : []const u8 ,
101+ base : []const u8 ,
102+ opts : StitchOpts ,
103+ ) ! []const u8 {
91104 if (base .len == 0 ) {
105+ if (opts .alloc == .always ) {
106+ return allocator .dupe (u8 , src );
107+ }
92108 return src ;
93109 }
94110
@@ -161,7 +177,7 @@ test "URL: Stitching Base & Src URLs (Basic)" {
161177
162178 const base = "https://www.google.com/xyz/abc/123" ;
163179 const src = "something.js" ;
164- const result = try URL .stitch (allocator , src , base );
180+ const result = try URL .stitch (allocator , src , base , .{} );
165181 defer allocator .free (result );
166182 try testing .expectString ("https://www.google.com/xyz/abc/something.js" , result );
167183}
@@ -171,7 +187,7 @@ test "URL: Stitching Base & Src URLs (Just Ending Slash)" {
171187
172188 const base = "https://www.google.com/" ;
173189 const src = "something.js" ;
174- const result = try URL .stitch (allocator , src , base );
190+ const result = try URL .stitch (allocator , src , base , .{} );
175191 defer allocator .free (result );
176192 try testing .expectString ("https://www.google.com/something.js" , result );
177193}
@@ -181,7 +197,7 @@ test "URL: Stitching Base & Src URLs (No Ending Slash)" {
181197
182198 const base = "https://www.google.com" ;
183199 const src = "something.js" ;
184- const result = try URL .stitch (allocator , src , base );
200+ const result = try URL .stitch (allocator , src , base , .{} );
185201 defer allocator .free (result );
186202 try testing .expectString ("https://www.google.com/something.js" , result );
187203}
@@ -191,7 +207,7 @@ test "URL: Stiching Base & Src URLs (Both Local)" {
191207
192208 const base = "./abcdef/123.js" ;
193209 const src = "something.js" ;
194- const result = try URL .stitch (allocator , src , base );
210+ const result = try URL .stitch (allocator , src , base , .{} );
195211 defer allocator .free (result );
196212 try testing .expectString ("./abcdef/something.js" , result );
197213}
0 commit comments