Browse Source

remove indexOf

master
Dominic Tarr 11 years ago
parent
commit
3381ae47a4
  1. 50
      lib/b64.js

50
lib/b64.js

@ -39,13 +39,16 @@ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
// represent one byte
// if there is only one, then the three characters before it represent 2 bytes
// this is just a cheap hack to not do indexOf twice
placeHolders = indexOf(b64, '=');
placeHolders = placeHolders > 0 ? b64.length - placeHolders : 0;
var len = b64.length
placeHolders = '=' === b64[len - 2] ? 2 : '=' === b64[len - 1] ? 1 : 0
// base64 is 4/3 + up to two characters of the original data
arr = new Uint8Array(b64.length * 3 / 4 - placeHolders);
// if there are placeholders, only get up to the last complete 4 chars
l = placeHolders > 0 ? b64.length - 4 : b64.length;
var L = 0
@ -55,7 +58,7 @@ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
}
for (i = 0, j = 0; i < l; i += 4, j += 3) {
tmp = (decode(b64.charAt(i)) << 18) | (indexOf(lookup, b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3));
tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3));
push((tmp & 0xFF0000) >> 16);
push((tmp & 0xFF00) >> 8);
push(tmp & 0xFF);
@ -65,7 +68,7 @@ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4);
push(tmp & 0xFF);
} else if (placeHolders === 1) {
tmp = (indexOf(lookup, b64.charAt(i)) << 10) | (indexOf(lookup, b64.charAt(i + 1)) << 4) | (indexOf(lookup, b64.charAt(i + 2)) >> 2);
tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2);
push((tmp >> 8) & 0xFF);
push(tmp & 0xFF);
}
@ -79,8 +82,12 @@ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
output = "",
temp, length;
function encode (num) {
return lookup.charAt(num)
}
function tripletToBase64 (num) {
return lookup.charAt(num >> 18 & 0x3F) + lookup.charAt(num >> 12 & 0x3F) + lookup.charAt(num >> 6 & 0x3F) + lookup.charAt(num & 0x3F);
return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F);
};
// go through the array every three bytes, we'll deal with trailing stuff later
@ -93,15 +100,15 @@ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
switch (extraBytes) {
case 1:
temp = uint8[uint8.length - 1];
output += lookup.charAt(temp >> 2);
output += lookup.charAt((temp << 4) & 0x3F);
output += encode(temp >> 2);
output += encode((temp << 4) & 0x3F);
output += '==';
break;
case 2:
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]);
output += lookup.charAt(temp >> 10);
output += lookup.charAt((temp >> 4) & 0x3F);
output += lookup.charAt((temp << 2) & 0x3F);
output += encode(temp >> 10);
output += encode((temp >> 4) & 0x3F);
output += encode((temp << 2) & 0x3F);
output += '=';
break;
}
@ -111,29 +118,6 @@ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
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;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++) {
if ((typeof arr === 'string' && arr.charAt(from) === elt) ||
(typeof arr !== 'string' && arr[from] === elt)) {
return from;
}
}
return -1;
}

Loading…
Cancel
Save