From f52f04adda19250baac614827e88fb66c08ce6ed Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 27 Mar 2026 16:05:41 -0700 Subject: [PATCH] add percentEncodeW and percentDecodeW --- url.zig | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/url.zig b/url.zig index 44523ebcb57a839027987271c101ab16c94e2d39..7b78e437509752c533ddfdb3fd1c946631bc6897 100644 --- a/url.zig +++ b/url.zig @@ -1470,20 +1470,23 @@ pub fn percentDecode(allocator: std.mem.Allocator, input: []const u8) ![]u8 { var result = std.ArrayList(u8).init(allocator); errdefer result.deinit(); try result.ensureUnusedCapacity(input.len); + try percentDecodeW(result.writer(), input); + return result.toOwnedSlice(); +} +pub fn percentDecodeW(writer: anytype, input: []const u8) !void { var i: usize = 0; while (i < input.len) : (i += 1) { if (input[i] == '%') { if (input.len >= i + 1 + 2) { if (std.ascii.isHex(input[i + 1]) and std.ascii.isHex(input[i + 2])) { - try result.append(extras.parseDigits(u8, input[i + 1 ..][0..2], 16) catch unreachable); + try writer.writeAll(&.{extras.parseDigits(u8, input[i + 1 ..][0..2], 16) catch unreachable}); i += 2; continue; } } } - try result.append(input[i]); + try writer.writeAll(&.{input[i]}); } - return result.toOwnedSlice(); } /// https://url.spec.whatwg.org/#concept-domain-to-ascii @@ -1799,6 +1802,19 @@ pub fn percentEncodeScalarAL(list: *std.ArrayList(u8), cp: []const u8, comptime try list.append(cp[0]); } } +pub fn percentEncodeW(writer: anytype, input: []const u8, comptime set: fn (u8) bool) !void { + var it = std.unicode.Utf8View.initUnchecked(input).iterator(); + while (it.nextCodepointSlice()) |sl| { + if (set(sl[0])) { + for (sl) |b| { + try writer.writeAll(&.{'%'}); + try writer.print("{X:0>2}", .{b}); + } + } else { + try writer.writeAll(sl); + } + } +} pub fn percentEncodeAL(list: *std.ArrayList(u8), input: []const u8, comptime set: fn (u8) bool) !void { var it = std.unicode.Utf8View.initUnchecked(input).iterator(); while (it.nextCodepointSlice()) |sl| { -- 2.54.0