Zero-pad percent_encode escapes to two uppercase hex digits

This commit is contained in:
Michael Tesař
2026-08-05 23:45:34 +02:00
parent f7f95ad763
commit c7af2576f5

View File

@@ -114,6 +114,8 @@ join_url :: proc(scheme, host, path: string, queries: map[string]string, fragmen
}
percent_encode :: proc(s: string, allocator := context.allocator) -> string {
HEX_DIGITS_UPPER := "0123456789ABCDEF" // NOTE(michtesar): RFC 3986 §2.1
b := strings.builder_make(allocator)
strings.builder_grow(&b, len(s) + 16) // NOTE(tetra): A reasonable number to allow for the number of things we need to escape.
@@ -124,10 +126,9 @@ percent_encode :: proc(s: string, allocator := context.allocator) -> string {
case:
bytes, n := utf8.encode_rune(ch)
for byte in bytes[:n] {
buf: [2]u8 = ---
t := strconv.write_int(buf[:], i64(byte), 16)
strings.write_rune(&b, '%')
strings.write_string(&b, t)
strings.write_byte(&b, '%')
strings.write_byte(&b, HEX_DIGITS_UPPER[byte >> 4])
strings.write_byte(&b, HEX_DIGITS_UPPER[byte & 0xF])
}
}
}