86 lines
2.6 KiB
TypeScript
86 lines
2.6 KiB
TypeScript
export module Encoding.UTF8 {
|
|
|
|
export function GetBytes(str: string) {
|
|
let len = str.length, resPos = -1;
|
|
let resArr = new Uint8Array(len * 3);
|
|
for (let point = 0, nextcode = 0, i = 0; i !== len;) {
|
|
point = str.charCodeAt(i), i += 1;
|
|
if (point >= 0xD800 && point <= 0xDBFF) {
|
|
if (i === len) {
|
|
resArr[resPos += 1] = 0xef;
|
|
resArr[resPos += 1] = 0xbf;
|
|
resArr[resPos += 1] = 0xbd;
|
|
break;
|
|
}
|
|
|
|
nextcode = str.charCodeAt(i);
|
|
if (nextcode >= 0xDC00 && nextcode <= 0xDFFF) {
|
|
point = (point - 0xD800) * 0x400 + nextcode - 0xDC00 + 0x10000;
|
|
i += 1;
|
|
if (point > 0xffff) {
|
|
resArr[resPos += 1] = (0x1e << 3) | (point >>> 18);
|
|
resArr[resPos += 1] = (0x2 << 6) | ((point >>> 12) & 0x3f);
|
|
resArr[resPos += 1] = (0x2 << 6) | ((point >>> 6) & 0x3f);
|
|
resArr[resPos += 1] = (0x2 << 6) | (point & 0x3f);
|
|
continue;
|
|
}
|
|
} else {
|
|
resArr[resPos += 1] = 0xef;
|
|
resArr[resPos += 1] = 0xbf;
|
|
resArr[resPos += 1] = 0xbd;
|
|
continue;
|
|
}
|
|
}
|
|
if (point <= 0x007f) {
|
|
resArr[resPos += 1] = (0x0 << 7) | point;
|
|
} else if (point <= 0x07ff) {
|
|
resArr[resPos += 1] = (0x6 << 5) | (point >>> 6);
|
|
resArr[resPos += 1] = (0x2 << 6) | (point & 0x3f);
|
|
} else {
|
|
resArr[resPos += 1] = (0xe << 4) | (point >>> 12);
|
|
resArr[resPos += 1] = (0x2 << 6) | ((point >>> 6) & 0x3f);
|
|
resArr[resPos += 1] = (0x2 << 6) | (point & 0x3f);
|
|
}
|
|
}
|
|
return resArr.subarray(0, resPos + 1);
|
|
}
|
|
|
|
export function GetString(array: Uint8Array) {
|
|
let str = "";
|
|
let i = 0, len = array.length;
|
|
while (i < len) {
|
|
let c = array[i++];
|
|
switch (c >> 4) {
|
|
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
|
|
str += String.fromCharCode(c);
|
|
break;
|
|
case 12: case 13:
|
|
str += String.fromCharCode(((c & 0x1F) << 6) | (array[i++] & 0x3F));
|
|
break;
|
|
case 14:
|
|
str += String.fromCharCode(((c & 0x0F) << 12) | ((array[i++] & 0x3F) << 6) | ((array[i++] & 0x3F) << 0));
|
|
break;
|
|
}
|
|
}
|
|
return str;
|
|
}
|
|
|
|
export function b64EncodeUnicode(str) {
|
|
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
|
|
return String.fromCharCode('0x' + p1);
|
|
}));
|
|
}
|
|
export function b64DecodeUnicode(str) {
|
|
return decodeURIComponent(atob(str).split('').map(function (c) {
|
|
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
|
|
}).join(''));
|
|
}
|
|
export function isBase64(str) {
|
|
if (str === '' || str.trim() === '') { return false; }
|
|
try {
|
|
return btoa(atob(str)) == str;
|
|
} catch (err) {
|
|
return false;
|
|
}
|
|
}
|
|
} |