2018-06-13 22:39:39 +03:00
|
|
|
'use strict';
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2018-06-17 23:47:28 +03:00
|
|
|
var bytes_1 = require("./bytes");
|
2018-06-13 22:39:39 +03:00
|
|
|
var UnicodeNormalizationForm;
|
|
|
|
(function (UnicodeNormalizationForm) {
|
|
|
|
UnicodeNormalizationForm["current"] = "";
|
|
|
|
UnicodeNormalizationForm["NFC"] = "NFC";
|
|
|
|
UnicodeNormalizationForm["NFD"] = "NFD";
|
|
|
|
UnicodeNormalizationForm["NFKC"] = "NFKC";
|
|
|
|
UnicodeNormalizationForm["NFKD"] = "NFKD";
|
|
|
|
})(UnicodeNormalizationForm = exports.UnicodeNormalizationForm || (exports.UnicodeNormalizationForm = {}));
|
|
|
|
;
|
2017-02-24 22:57:46 +03:00
|
|
|
// http://stackoverflow.com/questions/18729405/how-to-convert-utf8-string-to-byte-array
|
2018-06-13 22:39:39 +03:00
|
|
|
function toUtf8Bytes(str, form) {
|
|
|
|
if (form === void 0) { form = UnicodeNormalizationForm.current; }
|
|
|
|
if (form != UnicodeNormalizationForm.current) {
|
2018-06-21 03:29:54 +03:00
|
|
|
str = str.normalize(form);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2017-02-24 22:57:46 +03:00
|
|
|
var result = [];
|
|
|
|
var offset = 0;
|
|
|
|
for (var i = 0; i < str.length; i++) {
|
|
|
|
var c = str.charCodeAt(i);
|
|
|
|
if (c < 128) {
|
|
|
|
result[offset++] = c;
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
else if (c < 2048) {
|
2017-02-24 22:57:46 +03:00
|
|
|
result[offset++] = (c >> 6) | 192;
|
|
|
|
result[offset++] = (c & 63) | 128;
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
else if (((c & 0xFC00) == 0xD800) && (i + 1) < str.length && ((str.charCodeAt(i + 1) & 0xFC00) == 0xDC00)) {
|
2017-02-24 22:57:46 +03:00
|
|
|
// Surrogate Pair
|
|
|
|
c = 0x10000 + ((c & 0x03FF) << 10) + (str.charCodeAt(++i) & 0x03FF);
|
|
|
|
result[offset++] = (c >> 18) | 240;
|
|
|
|
result[offset++] = ((c >> 12) & 63) | 128;
|
|
|
|
result[offset++] = ((c >> 6) & 63) | 128;
|
|
|
|
result[offset++] = (c & 63) | 128;
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
else {
|
2017-02-24 22:57:46 +03:00
|
|
|
result[offset++] = (c >> 12) | 224;
|
|
|
|
result[offset++] = ((c >> 6) & 63) | 128;
|
|
|
|
result[offset++] = (c & 63) | 128;
|
|
|
|
}
|
|
|
|
}
|
2018-06-17 23:47:28 +03:00
|
|
|
return bytes_1.arrayify(result);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
exports.toUtf8Bytes = toUtf8Bytes;
|
|
|
|
;
|
2017-02-24 22:57:46 +03:00
|
|
|
// http://stackoverflow.com/questions/13356493/decode-utf-8-with-javascript#13691499
|
2018-06-13 22:39:39 +03:00
|
|
|
function toUtf8String(bytes) {
|
2018-06-17 23:47:28 +03:00
|
|
|
bytes = bytes_1.arrayify(bytes);
|
2017-02-24 22:57:46 +03:00
|
|
|
var result = '';
|
|
|
|
var i = 0;
|
|
|
|
// Invalid bytes are ignored
|
2018-06-13 22:39:39 +03:00
|
|
|
while (i < bytes.length) {
|
2017-02-24 22:57:46 +03:00
|
|
|
var c = bytes[i++];
|
|
|
|
if (c >> 7 == 0) {
|
|
|
|
// 0xxx xxxx
|
|
|
|
result += String.fromCharCode(c);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Invalid starting byte
|
2018-06-13 22:39:39 +03:00
|
|
|
if (c >> 6 == 0x02) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-24 22:57:46 +03:00
|
|
|
// Multibyte; how many bytes left for thus character?
|
|
|
|
var extraLength = null;
|
|
|
|
if (c >> 5 == 0x06) {
|
|
|
|
extraLength = 1;
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
else if (c >> 4 == 0x0e) {
|
2017-02-24 22:57:46 +03:00
|
|
|
extraLength = 2;
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
else if (c >> 3 == 0x1e) {
|
2017-02-24 22:57:46 +03:00
|
|
|
extraLength = 3;
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
else if (c >> 2 == 0x3e) {
|
2017-02-24 22:57:46 +03:00
|
|
|
extraLength = 4;
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
else if (c >> 1 == 0x7e) {
|
2017-02-24 22:57:46 +03:00
|
|
|
extraLength = 5;
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
else {
|
2017-02-24 22:57:46 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Do we have enough bytes in our data?
|
|
|
|
if (i + extraLength > bytes.length) {
|
|
|
|
// If there is an invalid unprocessed byte, try to continue
|
|
|
|
for (; i < bytes.length; i++) {
|
2018-06-13 22:39:39 +03:00
|
|
|
if (bytes[i] >> 6 != 0x02) {
|
|
|
|
break;
|
|
|
|
}
|
2017-02-24 22:57:46 +03:00
|
|
|
}
|
2018-06-13 22:39:39 +03:00
|
|
|
if (i != bytes.length)
|
|
|
|
continue;
|
2017-02-24 22:57:46 +03:00
|
|
|
// All leftover bytes are valid.
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// Remove the UTF-8 prefix from the char (res)
|
|
|
|
var res = c & ((1 << (8 - extraLength - 1)) - 1);
|
|
|
|
var count;
|
|
|
|
for (count = 0; count < extraLength; count++) {
|
|
|
|
var nextChar = bytes[i++];
|
|
|
|
// Is the char valid multibyte part?
|
2018-06-13 22:39:39 +03:00
|
|
|
if (nextChar >> 6 != 0x02) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
;
|
2017-02-24 22:57:46 +03:00
|
|
|
res = (res << 6) | (nextChar & 0x3f);
|
|
|
|
}
|
|
|
|
if (count != extraLength) {
|
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (res <= 0xffff) {
|
|
|
|
result += String.fromCharCode(res);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
res -= 0x10000;
|
|
|
|
result += String.fromCharCode(((res >> 10) & 0x3ff) + 0xd800, (res & 0x3ff) + 0xdc00);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2018-06-13 22:39:39 +03:00
|
|
|
exports.toUtf8String = toUtf8String;
|