-
Notifications
You must be signed in to change notification settings - Fork 0
/
string16.src.js
64 lines (64 loc) · 2.56 KB
/
string16.src.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
!function(window){
"use-strict";
var testingArray=[0], incSize=1;
var Uint16Array = window["Uint16Array"], Uint8Array = window["Uint8Array"], btoa = window["btoa"], atob = window["atob"];
var getCharFromInt = window["String"]["fromCharCode"];
var HTTPRequest = null, objectURL = "";
var createObjectURL = window["URL"]["createObjectURL"], revokeObjectURL = window["URL"]["revokeObjectURL"];
var FileReaderSync = window["FileReaderSync"] && new window["FileReaderSync"];
try {
// test the max chunksize String.fromCharCode can work with
while (incSize <= 0x10000){
testingArray.length = incSize*2;
getCharFromInt.apply(null, testingArray);
incSize *= 2;
}
} catch(e) {}
testingArray.length = 0; // clean up memory
var decodeArray16 = function(Uint16ArrayCodes){
var res = "", i = 0, length = Uint16ArrayCodes.length;
while (i < length){
res += getCharFromInt.apply(null, Uint16ArrayCodes.slice(i, (i+=incSize)));
}
return res;
}
var encodeString16 = function(Uint16String){
var res = new Uint16Array(Uint16String.length), i = Uint16String.length;
while (i--) res[i] = Uint16String.charCodeAt(i);
return res;
}
function expandOut(x){
var char=x.charCodeAt(0);
return getCharFromInt(char&0xff) + getCharFromInt(char>>>8);
}
function contractIn(x){
return getCharFromInt(x.charCodeAt(0) | (x.charCodeAt(1) << 8));
}
/** @nocollapse */
window["string16"] = {
"arrayToString": decodeArray16,
"stringToArray": encodeString16,
"encode64": function(Uint16String){
return btoa(Uint16String.replace(/[^]/g, expandOut));
},
"decode64": function(Uint16String){
return atob(Uint16String).replace(/[^][^]/g, contractIn);
},
"readBlobSync": function(blob, responseType, encoding){
if (FileReaderSync) {
if (!responseType || responseType === "text") return FileReaderSync["readAsText"](blob, encoding);
if (responseType === "arrayBuffer") return FileReaderSync["readAsArrayBuffer"](blob);
if (responseType === "dataurl") return FileReaderSync["readAsDataURL"](blob);
}
if (HTTPRequest === null) HTTPRequest = new XMLHttpRequest;
objectURL = createObjectURL(blob);
HTTPRequest.open("GET", objectURL, false);
HTTPRequest["overrideMimeType"]("application/ocelot-stream" + (encoding?"; charset=" + encoding:""));
if (responseType && responseType !== "dataurl") HTTPRequest["responseType"] = responseType;
HTTPRequest.send();
revokeObjectURL(objectURL);
if (responseType === "dataurl") return atob(HTTPRequest["response"]);
return HTTPRequest["response"];
}
};
}(typeof global === "undefined" ? self : global);