Skip to content

Instantly share code, notes, and snippets.

@inlife
Created December 4, 2016 17:49
Show Gist options
  • Save inlife/a5e2ee31330e1b044d98e24325980e12 to your computer and use it in GitHub Desktop.
Save inlife/a5e2ee31330e1b044d98e24325980e12 to your computer and use it in GitHub Desktop.
Squirrel url_encode
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