authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-01-22 16:38:34 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-01-22 16:38:34 -08:00
log3ea94ec9e260de535cc778c297f33c100d918465
treea2643089db86558b44c1f6bc596d18e5d82e4695
parent9392e9e3186ef5f84d2a37432a0116fbd96a803d

populate username field


3 files changed, 32 insertions(+), 18 deletions(-)

generate.ts+1-1
......@@ -50,7 +50,7 @@ pub fn parsePass(input: []const u8, base: ?[]const u8, href: []const u8, origin:
5050 _ = href;
5151 _ = origin;
5252 try expect(u.protocol).toEqualString(protocol);
53 _ = username;
53 try expect(u.username).toEqualString(username);
5454 _ = password;
5555 _ = host;
5656 _ = hostname;
url.zig+30-16
......@@ -8,6 +8,7 @@ pub const URL = struct {
88 href: []const u8,
99 scheme: []const u8 = "",
1010 protocol: []const u8,
11 username: []const u8,
1112
1213 pub const HostKind = enum {
1314 name,
......@@ -327,11 +328,21 @@ pub const URL = struct {
327328 // 3. Set atSignSeen to true.
328329 atSignSeen = true;
329330 // 4. For each codePoint in buffer:
330 {
331 var it = std.unicode.Utf8View.initUnchecked(buffer.items).iterator();
332 while (it.nextCodepointSlice()) |sl| {
331333 // 1. If codePoint is U+003A (:) and passwordTokenSeen is false, then set passwordTokenSeen to true and continue.
334 if (sl[0] == ':' and !passwordTokenSeen) {
335 passwordTokenSeen = true;
336 continue;
337 }
332338 // 2. Let encodedCodePoints be the result of running UTF-8 percent-encode codePoint using the userinfo percent-encode set.
333339 // 3. If passwordTokenSeen is true, then append encodedCodePoints to url’s password.
334340 // 4. Otherwise, append encodedCodePoints to url’s username.
341 if (passwordTokenSeen) {
342 try percentEncodeScalarML(&href, 5, sl, is_userinfo_percent_char);
343 } else {
344 try percentEncodeScalarML(&href, 3, sl, is_userinfo_percent_char);
345 }
335346 }
336347 // 5. Set buffer to the empty string.
337348 buffer.clearRetainingCapacity();
......@@ -769,6 +780,7 @@ pub const URL = struct {
769780 const url: URL = .{
770781 .href = _href,
771782 .protocol = _href[0..extras.sum(usize, href.lengths[0..2])],
783 .username = _href[extras.sum(usize, href.lengths[0..2])..][0..href.lengths[3]],
772784 };
773785 return url;
774786 }
......@@ -1323,23 +1335,25 @@ fn is_fragment_percent_char(c: u8) bool {
13231335 if (c == '`') return true;
13241336 return is_c0control_percent_char(c);
13251337}
1326
1327// fn is_userinfo_percent_char(c: u8) bool {
1328// if (c == '/') return true;
1329// if (c == ':') return true;
1330// if (c == ';') return true;
1331// if (c == '=') return true;
1332// if (c == '@') return true;
1333// if (c >= '[' and c <= '^') return true;
1334// if (c == '|') return true;
1335// return is_path_percent_char(c);
1336// }
1338/// https://url.spec.whatwg.org/#userinfo-percent-encode-set
1339fn is_userinfo_percent_char(c: u8) bool {
1340 if (c == '/') return true;
1341 if (c == ':') return true;
1342 if (c == ';') return true;
1343 if (c == '=') return true;
1344 if (c == '@') return true;
1345 if (c >= '[' and c <= '^') return true;
1346 if (c == '|') return true;
1347 return is_path_percent_char(c);
1348}
1349// /// https://url.spec.whatwg.org/#component-percent-encode-set
13371350// fn is_component_percent_char(c: u8) bool {
13381351// if (c >= '$' and c <= '&') return true;
13391352// if (c == '+') return true;
13401353// if (c == ',') return true;
13411354// return is_userinfo_percent_char(c);
13421355// }
1356// /// https://url.spec.whatwg.org/#application-x-www-form-urlencoded-percent-encode-set
13431357// fn is_formurlencoded_percent_char(c: u8) bool {
13441358// if (c == '!') return true;
13451359// if (c >= '\'' and c <= ')') return true;
......@@ -1360,7 +1374,7 @@ fn percentEncodeScalarAL(list: *std.ArrayList(u8), cp: []const u8, comptime set:
13601374 if (set(cp[0])) {
13611375 for (cp) |b| {
13621376 try list.append('%');
1363 try list.writer().print("{d:0<2}", .{b});
1377 try list.writer().print("{X:0<2}", .{b});
13641378 }
13651379 } else {
13661380 try list.append(cp[0]);
......@@ -1372,7 +1386,7 @@ fn percentEncodeAL(list: *std.ArrayList(u8), input: []const u8, comptime set: fn
13721386 if (set(sl[0])) {
13731387 for (sl) |b| {
13741388 try list.append('%');
1375 try list.writer().print("{d:0<2}", .{b});
1389 try list.writer().print("{X:0<2}", .{b});
13761390 }
13771391 } else {
13781392 try list.appendSlice(sl);
......@@ -1383,7 +1397,7 @@ fn percentEncodeScalarML(list: *ManyArrayList(15, u8), n: usize, cp: []const u8,
13831397 if (set(cp[0])) {
13841398 for (cp) |b| {
13851399 try list.appendSlice(n, &.{'%'});
1386 try list.print(n, "{d:0<2}", .{b});
1400 try list.print(n, "{X:0<2}", .{b});
13871401 }
13881402 } else {
13891403 try list.appendSlice(n, &.{cp[0]});
......@@ -1395,7 +1409,7 @@ fn percentEncodeML(list: *ManyArrayList(15, u8), n: usize, input: []const u8, co
13951409 if (set(sl[0])) {
13961410 for (sl) |b| {
13971411 try list.appendSlice(n, &.{'%'});
1398 try list.print(n, "{d:0<2}", .{b});
1412 try list.print(n, "{X:0<2}", .{b});
13991413 }
14001414 } else {
14011415 try list.appendSlice(n, sl);
zig-out/test.zig+1-1
......@@ -20,7 +20,7 @@ pub fn parsePass(input: []const u8, base: ?[]const u8, href: []const u8, origin:
2020 _ = href;
2121 _ = origin;
2222 try expect(u.protocol).toEqualString(protocol);
23 _ = username;
23 try expect(u.username).toEqualString(username);
2424 _ = password;
2525 _ = host;
2626 _ = hostname;