summaryrefslogtreecommitdiff
path: root/src/defs.js
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-11-12 21:59:58 +1100
committerdaurnimator <quae@daurnimator.com>2017-11-12 22:20:08 +1100
commit0a8bc0b30644c514d822fb32a184318f7353a285 (patch)
treee04468a215e76add80acafdae97d84a34f7e407c /src/defs.js
parent81a0e90a502faf9edebc63f54fce51aae4397e72 (diff)
downloadfengari-0a8bc0b30644c514d822fb32a184318f7353a285.tar.gz
fengari-0a8bc0b30644c514d822fb32a184318f7353a285.tar.bz2
fengari-0a8bc0b30644c514d822fb32a184318f7353a285.zip
Add lua.to_uristring
Diffstat (limited to 'src/defs.js')
-rw-r--r--src/defs.js20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/defs.js b/src/defs.js
index 315364a..3ebb747 100644
--- a/src/defs.js
+++ b/src/defs.js
@@ -188,6 +188,25 @@ const to_jsstring = function(value, from, to) {
return str;
};
+const uri_allowed = {}; /* bytes allowed unescaped in a uri */
+for (let c of ";,/?:@&=+$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,-_.!~*'()#") {
+ uri_allowed[c.charCodeAt(0)] = true;
+}
+
+/* utility function to convert a lua string to a js string with uri escaping */
+const to_uristring = function(a) {
+ let s = "";
+ for (let i=0; i<a.length; i++) {
+ let v = a[i];
+ if (uri_allowed[v]) {
+ s += String.fromCharCode(v);
+ } else {
+ s += "%" + (v<0x10?"0":"") + v.toString(16);
+ }
+ }
+ return s;
+};
+
const to_luastring_cache = {};
const to_luastring = function(str, cache) {
@@ -405,3 +424,4 @@ module.exports.is_luastring = is_luastring;
module.exports.luastring_cmp = luastring_cmp;
module.exports.to_jsstring = to_jsstring;
module.exports.to_luastring = to_luastring;
+module.exports.to_uristring = to_uristring;