Mercurial > urweb
comparison lib/js/urweb.js @ 1813:4c61bb7acb9b
Minor js optimizations.
* flatten() now uses accumulator and join.
* reuse regexps instead of allocating new each time
author | Vladimir Shabanov <vshabanoff@gmail.com> |
---|---|
date | Thu, 30 Aug 2012 17:58:37 +0400 |
parents | ea5f920ffa35 |
children | aadfc1a138d8 |
comparison
equal
deleted
inserted
replaced
1812:ea5f920ffa35 | 1813:4c61bb7acb9b |
---|---|
593 | 593 |
594 function cr(n) { | 594 function cr(n) { |
595 return closures[n]; | 595 return closures[n]; |
596 } | 596 } |
597 | 597 |
598 function flatten(cls, tr) { | 598 function flattenAcc(a, cls, tr) { |
599 if (tr.cat1 != null) | 599 if (tr.cat1 != null) { |
600 return flatten(cls, tr.cat1) + flatten(cls, tr.cat2); | 600 flattenAcc(a, cls, tr.cat1); |
601 else if (tr.closure != null) { | 601 flattenAcc(a, cls, tr.cat2); |
602 } else if (tr.closure != null) { | |
602 var cl = newClosure(tr.closure); | 603 var cl = newClosure(tr.closure); |
603 cls.v = cons(cl, cls.v); | 604 cls.v = cons(cl, cls.v); |
604 return "cr(" + cl + ")"; | 605 a.push("cr(", cl.toString(), ")"); |
605 } else | 606 } else |
606 return tr; | 607 a.push(tr); |
608 } | |
609 | |
610 function flatten(cls, tr) { | |
611 var a = []; | |
612 flattenAcc(a, cls, tr); | |
613 return a.join(""); | |
607 } | 614 } |
608 | 615 |
609 function flattenLocal(s) { | 616 function flattenLocal(s) { |
610 var cls = {v : null}; | 617 var cls = {v : null}; |
611 var r = flatten(cls, s); | 618 var r = flatten(cls, s); |
1245 } | 1252 } |
1246 | 1253 |
1247 function uf(s) { | 1254 function uf(s) { |
1248 if (s.length == 0) | 1255 if (s.length == 0) |
1249 return "_"; | 1256 return "_"; |
1250 s = s.replace(new RegExp ("\\.", "g"), ".2E"); | 1257 s = s.replace(/\./g, ".2E"); |
1251 return (s.charAt(0) == '_' ? "_" : "") + encodeURIComponent(s).replace(new RegExp ("%", "g"), "."); | 1258 return (s.charAt(0) == '_' ? "_" : "") + encodeURIComponent(s).replace(/%/g, "."); |
1252 } | 1259 } |
1253 | 1260 |
1254 function uu(s) { | 1261 function uu(s) { |
1255 if (s.length > 0 && s.charAt(0) == '_') { | 1262 if (s.length > 0 && s.charAt(0) == '_') { |
1256 s = s.substring(1); | 1263 s = s.substring(1); |
1257 } else if (s.length >= 3 && (s.charAt(0) == '%' || s.charAt(0) == '.') | 1264 } else if (s.length >= 3 && (s.charAt(0) == '%' || s.charAt(0) == '.') |
1258 && s.charAt(1) == '5' && (s.charAt(2) == 'f' || s.charAt(2) == 'F')) | 1265 && s.charAt(1) == '5' && (s.charAt(2) == 'f' || s.charAt(2) == 'F')) |
1259 s = s.substring(3); | 1266 s = s.substring(3); |
1260 s = s.replace(new RegExp ("\\+", "g"), " "); | 1267 s = s.replace(/\+/g, " "); |
1261 s = s.replace(new RegExp ("\\.", "g"), "%"); | 1268 s = s.replace(/\./g, "%"); |
1262 return decodeURIComponent(s); | 1269 return decodeURIComponent(s); |
1263 } | 1270 } |
1264 | 1271 |
1265 function atr(s) { | 1272 function atr(s) { |
1266 return s.replace(new RegExp ("\"", "g"), """).replace(new RegExp ("&", "g"), "&") | 1273 return s.replace(/\"/g, """).replace(/&/g, "&") |
1267 } | 1274 } |
1268 | 1275 |
1269 function ub(b) { | 1276 function ub(b) { |
1270 return b ? "1" : "0"; | 1277 return b ? "1" : "0"; |
1271 } | 1278 } |
1272 | 1279 |