Skip to content

Instantly share code, notes, and snippets.

@pfitzseb
Last active June 24, 2020 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfitzseb/d6a9ebee8404e12ec69132af6cf2dada to your computer and use it in GitHub Desktop.
Save pfitzseb/d6a9ebee8404e12ec69132af6cf2dada to your computer and use it in GitHub Desktop.
encodeURIcomponent
using Printf
UNESCAPED = Set(codeunits("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.!~*'()"))
# https://tc39.es/ecma262/#sec-encodeuricomponent-uricomponent
function encode_uri_component(uri)
isvalid(uri) || throw(ArgumentError("`encode_uri_component` can only handle valid UTF8 strings."))
io = IOBuffer()
for cp in codeunits(uri)
if cp in UNESCAPED
print(io, Char(cp))
else
print(io, '%')
@printf(io, "%2X", cp)
end
end
return String(take!(io))
end
@test encode_uri_component(";,/?:@&=+\$") == "%3B%2C%2F%3F%3A%40%26%3D%2B%24"
@test encode_uri_component("-_.!~*'()") == "-_.!~*'()"
@test encode_uri_component("#") == "%23"
@test encode_uri_component("ABC abc 123") == "ABC%20abc%20123"
@test encode_uri_component("шеллы") == "%D1%88%D0%B5%D0%BB%D0%BB%D1%8B"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment