Last active
June 24, 2020 11:53
-
-
Save pfitzseb/d6a9ebee8404e12ec69132af6cf2dada to your computer and use it in GitHub Desktop.
encodeURIcomponent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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