Skip to content

Instantly share code, notes, and snippets.

@Twilyze
Last active August 28, 2021 17:21
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 Twilyze/e2c6b6d23e7b0ebbca2f41fba1ee835b to your computer and use it in GitHub Desktop.
Save Twilyze/e2c6b6d23e7b0ebbca2f41fba1ee835b to your computer and use it in GitHub Desktop.
Reproduce JavaScript encodeURI and encodeURIComponent functions using AutoHotkey. (v1.1.30.03)
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