From bce8b9e20686a5d4fe601c404f6e1494c5824025 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 28 May 2014 10:49:53 -0400 Subject: [PATCH] refactor(zip) decode --- lib/client/zip.js | 72 ++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/lib/client/zip.js b/lib/client/zip.js index e0453731..10f0548e 100644 --- a/lib/client/zip.js +++ b/lib/client/zip.js @@ -56,42 +56,44 @@ var Util, Zip, pako; */ function decode(s) { - /*jshint bitwise:false */ - s = s.replace(/%([EF][0-9A-F])%([89AB][0-9A-F])%([89AB][0-9A-F])/gi, - function(code,hex1,hex2,hex3) { - var n, n3, - n1 = parseInt(hex1,16) - 0xE0, - n2 = parseInt(hex2,16) - 0x80; - - if (n1 === 0 && n2 < 32) - return code; - - n3 = parseInt(hex3,16)-0x80; - n = (n1<<12) + (n2<<6) + n3; - - if (n > 0xFFFF) - return code; - - return String.fromCharCode(n); - }); - s = s.replace(/%([CD][0-9A-F])%([89AB][0-9A-F])/gi, - function(code,hex1,hex2) { - var n1, n2; - - n1 = parseInt(hex1,16) - 0xC0; - - if (n1 < 2) - return code; - - n2 = parseInt(hex2,16) - 0x80; - - return String.fromCharCode((n1<<6) + n2); - }); - s = s.replace(/%([0-7][0-9A-F])/gi, - function(code, hex) { - return String.fromCharCode(parseInt(hex,16)); - }); + s = s.replace(/%([EF][0-9A-F])%([89AB][0-9A-F])%([89AB][0-9A-F])/gi, ef) + .replace(/%([CD][0-9A-F])%([89AB][0-9A-F])/gi, cd) + .replace(/%([0-7][0-9A-F])/gi, o7); return s; + + function o7(code, hex) { + return String.fromCharCode(parseInt(hex,16)); + } + + function ef(code, hex1, hex2, hex3) { + var n, n3, + n1 = parseInt(hex1,16) - 0xE0, + n2 = parseInt(hex2,16) - 0x80; + + if (n1 === 0 && n2 < 32) + return code; + + n3 = parseInt(hex3,16)-0x80; + n = (n1<<12) + (n2<<6) + n3; + + if (n > 0xFFFF) + return code; + + return String.fromCharCode(n); + } + + function cd(code, hex1, hex2) { + var n1, n2; + + n1 = parseInt(hex1,16) - 0xC0; + + if (n1 < 2) + return code; + + n2 = parseInt(hex2,16) - 0x80; + + return String.fromCharCode((n1<<6) + n2); + } } })();