Last active
August 28, 2021 17:21
-
-
Save Twilyze/e2c6b6d23e7b0ebbca2f41fba1ee835b to your computer and use it in GitHub Desktop.
Reproduce JavaScript encodeURI and encodeURIComponent functions using AutoHotkey. (v1.1.30.03)
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
EncodeURI(uri, excepts := "!#$&'()*+,-./:;=?@_~") | |
{ | |
VarSetCapacity(str, StrPut(uri, "UTF-8"), 0) | |
StrPut(uri, &str, "UTF-8") | |
f := A_FormatInteger | |
SetFormat, IntegerFast, H | |
while code := NumGet(str, A_Index - 1, "UChar") | |
if (code >= 0x30 && code <= 0x39 ; 0-9 | |
|| code >= 0x41 && code <= 0x5A ; A-Z | |
|| code >= 0x61 && code <= 0x7A ; a-z | |
|| InStr(excepts, Chr(code), true)) | |
res .= Chr(code) | |
else | |
res .= "%" . SubStr(code + 0x100, -1) | |
SetFormat, IntegerFast, %f% | |
return res | |
} | |
EncodeURIComponent(uri, excepts := "!'()*-._~") | |
{ | |
return EncodeURI(uri, excepts) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment