From 302ecd5dc2e14ccf643d99fe19c9b7ae657e80b7 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Fri, 18 Sep 2026 23:30:26 -0600 Subject: [PATCH] Reduce URL percent-encoding allocations --- src/runtime.jl | 3 ++- test/runtime.jl | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/runtime.jl b/src/runtime.jl index 3d6ea16..f3100db 100644 --- a/src/runtime.jl +++ b/src/runtime.jl @@ -1504,7 +1504,8 @@ function _escapeuri(text) if char in 'A':'Z' || char in 'a':'z' || char in '0':'9' || char in "-_.~" write(io, char) else - print(io, '%', uppercase(string(byte; base = 16, pad = 2))) + write(io, UInt8('%'), codeunit("0123456789ABCDEF", (byte >> 4) + 1), + codeunit("0123456789ABCDEF", (byte & 0x0f) + 1)) end end return String(take!(io)) diff --git a/test/runtime.jl b/test/runtime.jl index fbf91a8..738121a 100644 --- a/test/runtime.jl +++ b/test/runtime.jl @@ -424,6 +424,16 @@ @test invoke(:_escape, reserved; allow_reserved = true) == reserved @test invoke(:_escape, "%2F"; allow_reserved = true) == "%2F" @test invoke(:_escape, "a b") == "a%20b" + @test invoke(:_escape, "AZaz09-_.~") == "AZaz09-_.~" + @test invoke(:_escape, "%2F") == "%252F" + @test invoke(:_escape, "α👽/\0") == "%CE%B1%F0%9F%91%BD%2F%00" + @test invoke(:_escape, SubString("prefixα 👽", 7)) == "%CE%B1%20%F0%9F%91%BD" + for byte in UInt8(0):UInt8(255) + char = Char(byte) + expected = char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~" ? + string(char) : "%" * uppercase(string(byte; base = 16, pad = 2)) + @test invoke(:_escape, String([byte])) == expected + end # allowReserved on a path parameter keeps slash-delimited values intact @test invoke(:_path_parameter, "path", "opa/examples/public servers", :simple, false) == "opa%2Fexamples%2Fpublic%20servers"