Created
December 4, 2016 17:49
-
-
Save inlife/a5e2ee31330e1b044d98e24325980e12 to your computer and use it in GitHub Desktop.
Squirrel url_encode
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
local R = regexp("[A-Za-z0-9]"); | |
function isalnum(value) { | |
return R.match(value.tochar()); | |
} | |
function url_encode(value) { | |
local escaped = ""; | |
for (local i = 0; i < value.len(); i++) { | |
local c = value[i].tointeger(); | |
// Keep alphanumeric and other accepted characters intact | |
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') { | |
escaped += c.tochar(); | |
continue; | |
} | |
if (c == ' ') { | |
escaped += "+"; | |
continue; | |
} | |
// Any other characters are percent-encoded | |
escaped += "%" + format("%02X", c); | |
} | |
return escaped; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment