Browse Source

use hardcoded ranges instead of indexOf

master
Dominic Tarr 11 years ago
parent
commit
c9ac169fff
  1. 36
      lib/b64.js

36
lib/b64.js

@ -1,10 +1,30 @@
(function (exports) {
'use strict';
var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
;(function (exports) {
'use strict';
function decode (c) {
return indexOf(lookup, c)
var ZERO = '0'.charCodeAt(0)
var PLUS = '+'.charCodeAt(0)
var SLASH = '/'.charCodeAt(0)
var NUMBER = '0'.charCodeAt(0)
var LOWER = 'a'.charCodeAt(0)
var UPPER = 'A'.charCodeAt(0)
function decode (elt) {
var code = elt.charCodeAt(0)
if(code === PLUS)
return 62 // '+'
if(code === SLASH)
return 63 // '/'
if(code < NUMBER)
return -1 //no match
if(code < NUMBER + 10)
return code - NUMBER + 26 + 26
if(code < UPPER + 26)
return code - UPPER
if(code < LOWER + 26)
return code - LOWER + 26
}
function b64ToByteArray(b64) {
@ -91,9 +111,14 @@
module.exports.toByteArray = b64ToByteArray;
module.exports.fromByteArray = uint8ToBase64;
module.exports.indexOf = indexOf
module.exports.lookup = lookup
}());
function indexOf (arr, elt /*, from*/) {
var len = arr.length;
var from = Number(arguments[1]) || 0;
@ -111,3 +136,4 @@ function indexOf (arr, elt /*, from*/) {
}
return -1;
}

Loading…
Cancel
Save