move index to unibabel-mozilla

This commit is contained in:
AJ ONeal 2015-06-10 17:45:19 -06:00
parent a35f1b86c1
commit bff3fb392d
2 changed files with 52 additions and 2 deletions

View File

@ -69,13 +69,17 @@ function uint6ToB64 (nUint6) {
}
function base64EncArr (aBytes) {
function base64EncArr (aBytes, delim) {
var nMod3 = 2, sB64Enc = "";
for (var nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) {
nMod3 = nIdx % 3;
if (nIdx > 0 && (nIdx * 4 / 3) % 76 === 0) { sB64Enc += "\r\n"; }
if (delim) {
if (nIdx > 0 && (nIdx * 4 / 3) % 76 === 0) {
sB64Enc += "\r\n";
}
}
nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24);
if (nMod3 === 2 || aBytes.length - nIdx === 1) {
sB64Enc += String.fromCharCode(uint6ToB64(nUint24 >>> 18 & 63), uint6ToB64(nUint24 >>> 12 & 63), uint6ToB64(nUint24 >>> 6 & 63), uint6ToB64(nUint24 & 63));

46
unibabel.hex.js Normal file
View File

@ -0,0 +1,46 @@
(function () {
'use strict';
function bufferToHex(arr) {
var i;
var len;
var hex = '';
var c;
for (i = 0, len = arr.length; i < len; i += 1) {
c = arr[i].toString(16);
if (c.length < 2) {
c = '0' + c;
}
hex += c;
}
return hex;
}
function hexToBuffer(hex) {
// TODO use Uint8Array or ArrayBuffer or DataView
var i;
var byteLen = hex.length / 2;
var arr;
var j = 0;
if (byteLen !== parseInt(byteLen, 10)) {
throw new Error("Invalid hex length '" + hex.length + "'");
}
arr = new Uint8Array(byteLen);
for (i = 0; i < byteLen; i += 1) {
arr[i] = parseInt(hex[j] + hex[j + 1], 16);
j += 2;
}
return arr;
}
// Hex Convenience Functions
window.Unibabel.hexToBuffer = hexToBuffer;
window.Unibabel.bufferToHex = bufferToHex;
}());