walnut.js/lib/com.daplie.walnut.current/bower_components/unibabel/index.js

105 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

(function () {
'use strict';
function utf8ToBinaryString(str) {
var escstr = encodeURIComponent(str);
// replaces any uri escape sequence, such as %0A,
// with binary escape, such as 0x0A
var binstr = escstr.replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode('0x' + p1);
});
return binstr;
}
function utf8ToBuffer(str) {
var binstr = utf8ToBinaryString(str);
var buf = binaryStringToBuffer(binstr);
return buf;
}
function utf8ToBase64(str) {
var binstr = utf8ToBinaryString(str);
return btoa(binstr);
}
function binaryStringToUtf8(binstr) {
var escstr = binstr.replace(/(.)/g, function (m, p) {
var code = p.charCodeAt(p).toString(16).toUpperCase();
if (code.length < 2) {
code = '0' + code;
}
return '%' + code;
});
return decodeURIComponent(escstr);
}
function bufferToUtf8(buf) {
var binstr = bufferToBinaryString(buf);
return binaryStringToUtf8(binstr);
}
function base64ToUtf8(b64) {
var binstr = atob(b64);
return binaryStringToUtf8(binstr);
}
function bufferToBinaryString(buf) {
var binstr = Array.prototype.map.call(buf, function (ch) {
return String.fromCharCode(ch);
}).join('');
return binstr;
}
function bufferToBase64(arr) {
var binstr = bufferToBinaryString(arr);
return btoa(binstr);
}
function binaryStringToBuffer(binstr) {
var buf;
if ('undefined' !== typeof Uint8Array) {
buf = new Uint8Array(binstr.length);
} else {
buf = [];
}
Array.prototype.forEach.call(binstr, function (ch, i) {
buf[i] = ch.charCodeAt(0);
});
return buf;
}
function base64ToBuffer(base64) {
var binstr = atob(base64);
var buf = binaryStringToBuffer(binstr);
return buf;
}
window.Unibabel = {
utf8ToBinaryString: utf8ToBinaryString
, utf8ToBuffer: utf8ToBuffer
, utf8ToBase64: utf8ToBase64
, binaryStringToUtf8: binaryStringToUtf8
, bufferToUtf8: bufferToUtf8
, base64ToUtf8: base64ToUtf8
, bufferToBinaryString: bufferToBinaryString
, bufferToBase64: bufferToBase64
, binaryStringToBuffer: binaryStringToBuffer
, base64ToBuffer: base64ToBuffer
// compat
, strToUtf8Arr: utf8ToBuffer
, utf8ArrToStr: bufferToUtf8
, arrToBase64: bufferToBase64
, base64ToArr: base64ToBuffer
};
}());