mirror of
https://github.com/odin-lang/Odin.git
synced 2026-08-14 09:44:26 +00:00
Zero-pad percent_encode escapes to two uppercase hex digits
This commit is contained in:
@@ -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])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user