123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.toUtf8CodePoints = exports.toUtf8String = exports._toUtf8String = exports._toEscapedUtf8String = exports.toUtf8Bytes = exports.Utf8ErrorFuncs = exports.Utf8ErrorReason = exports.UnicodeNormalizationForm = void 0;
- var bytes_1 = require("@ethersproject/bytes");
- var logger_1 = require("@ethersproject/logger");
- var _version_1 = require("./_version");
- var logger = new logger_1.Logger(_version_1.version);
- var UnicodeNormalizationForm;
- (function (UnicodeNormalizationForm) {
- UnicodeNormalizationForm["current"] = "";
- UnicodeNormalizationForm["NFC"] = "NFC";
- UnicodeNormalizationForm["NFD"] = "NFD";
- UnicodeNormalizationForm["NFKC"] = "NFKC";
- UnicodeNormalizationForm["NFKD"] = "NFKD";
- })(UnicodeNormalizationForm = exports.UnicodeNormalizationForm || (exports.UnicodeNormalizationForm = {}));
- ;
- var Utf8ErrorReason;
- (function (Utf8ErrorReason) {
-
-
- Utf8ErrorReason["UNEXPECTED_CONTINUE"] = "unexpected continuation byte";
-
-
- Utf8ErrorReason["BAD_PREFIX"] = "bad codepoint prefix";
-
-
- Utf8ErrorReason["OVERRUN"] = "string overrun";
-
-
- Utf8ErrorReason["MISSING_CONTINUE"] = "missing continuation byte";
-
-
-
- Utf8ErrorReason["OUT_OF_RANGE"] = "out of UTF-8 range";
-
-
-
- Utf8ErrorReason["UTF16_SURROGATE"] = "UTF-16 surrogate";
-
-
-
- Utf8ErrorReason["OVERLONG"] = "overlong representation";
- })(Utf8ErrorReason = exports.Utf8ErrorReason || (exports.Utf8ErrorReason = {}));
- ;
- function errorFunc(reason, offset, bytes, output, badCodepoint) {
- return logger.throwArgumentError("invalid codepoint at offset " + offset + "; " + reason, "bytes", bytes);
- }
- function ignoreFunc(reason, offset, bytes, output, badCodepoint) {
-
- if (reason === Utf8ErrorReason.BAD_PREFIX || reason === Utf8ErrorReason.UNEXPECTED_CONTINUE) {
- var i = 0;
- for (var o = offset + 1; o < bytes.length; o++) {
- if (bytes[o] >> 6 !== 0x02) {
- break;
- }
- i++;
- }
- return i;
- }
-
-
- if (reason === Utf8ErrorReason.OVERRUN) {
- return bytes.length - offset - 1;
- }
-
- return 0;
- }
- function replaceFunc(reason, offset, bytes, output, badCodepoint) {
-
- if (reason === Utf8ErrorReason.OVERLONG) {
- output.push(badCodepoint);
- return 0;
- }
-
- output.push(0xfffd);
-
- return ignoreFunc(reason, offset, bytes, output, badCodepoint);
- }
- exports.Utf8ErrorFuncs = Object.freeze({
- error: errorFunc,
- ignore: ignoreFunc,
- replace: replaceFunc
- });
- function getUtf8CodePoints(bytes, onError) {
- if (onError == null) {
- onError = exports.Utf8ErrorFuncs.error;
- }
- bytes = (0, bytes_1.arrayify)(bytes);
- var result = [];
- var i = 0;
-
- while (i < bytes.length) {
- var c = bytes[i++];
-
- if (c >> 7 === 0) {
- result.push(c);
- continue;
- }
-
- var extraLength = null;
- var overlongMask = null;
-
- if ((c & 0xe0) === 0xc0) {
- extraLength = 1;
- overlongMask = 0x7f;
-
- }
- else if ((c & 0xf0) === 0xe0) {
- extraLength = 2;
- overlongMask = 0x7ff;
-
- }
- else if ((c & 0xf8) === 0xf0) {
- extraLength = 3;
- overlongMask = 0xffff;
- }
- else {
- if ((c & 0xc0) === 0x80) {
- i += onError(Utf8ErrorReason.UNEXPECTED_CONTINUE, i - 1, bytes, result);
- }
- else {
- i += onError(Utf8ErrorReason.BAD_PREFIX, i - 1, bytes, result);
- }
- continue;
- }
-
- if (i - 1 + extraLength >= bytes.length) {
- i += onError(Utf8ErrorReason.OVERRUN, i - 1, bytes, result);
- continue;
- }
-
- var res = c & ((1 << (8 - extraLength - 1)) - 1);
- for (var j = 0; j < extraLength; j++) {
- var nextChar = bytes[i];
-
- if ((nextChar & 0xc0) != 0x80) {
- i += onError(Utf8ErrorReason.MISSING_CONTINUE, i, bytes, result);
- res = null;
- break;
- }
- ;
- res = (res << 6) | (nextChar & 0x3f);
- i++;
- }
-
- if (res === null) {
- continue;
- }
-
- if (res > 0x10ffff) {
- i += onError(Utf8ErrorReason.OUT_OF_RANGE, i - 1 - extraLength, bytes, result, res);
- continue;
- }
-
- if (res >= 0xd800 && res <= 0xdfff) {
- i += onError(Utf8ErrorReason.UTF16_SURROGATE, i - 1 - extraLength, bytes, result, res);
- continue;
- }
-
- if (res <= overlongMask) {
- i += onError(Utf8ErrorReason.OVERLONG, i - 1 - extraLength, bytes, result, res);
- continue;
- }
- result.push(res);
- }
- return result;
- }
- function toUtf8Bytes(str, form) {
- if (form === void 0) { form = UnicodeNormalizationForm.current; }
- if (form != UnicodeNormalizationForm.current) {
- logger.checkNormalize();
- str = str.normalize(form);
- }
- var result = [];
- for (var i = 0; i < str.length; i++) {
- var c = str.charCodeAt(i);
- if (c < 0x80) {
- result.push(c);
- }
- else if (c < 0x800) {
- result.push((c >> 6) | 0xc0);
- result.push((c & 0x3f) | 0x80);
- }
- else if ((c & 0xfc00) == 0xd800) {
- i++;
- var c2 = str.charCodeAt(i);
- if (i >= str.length || (c2 & 0xfc00) !== 0xdc00) {
- throw new Error("invalid utf-8 string");
- }
-
- var pair = 0x10000 + ((c & 0x03ff) << 10) + (c2 & 0x03ff);
- result.push((pair >> 18) | 0xf0);
- result.push(((pair >> 12) & 0x3f) | 0x80);
- result.push(((pair >> 6) & 0x3f) | 0x80);
- result.push((pair & 0x3f) | 0x80);
- }
- else {
- result.push((c >> 12) | 0xe0);
- result.push(((c >> 6) & 0x3f) | 0x80);
- result.push((c & 0x3f) | 0x80);
- }
- }
- return (0, bytes_1.arrayify)(result);
- }
- exports.toUtf8Bytes = toUtf8Bytes;
- ;
- function escapeChar(value) {
- var hex = ("0000" + value.toString(16));
- return "\\u" + hex.substring(hex.length - 4);
- }
- function _toEscapedUtf8String(bytes, onError) {
- return '"' + getUtf8CodePoints(bytes, onError).map(function (codePoint) {
- if (codePoint < 256) {
- switch (codePoint) {
- case 8: return "\\b";
- case 9: return "\\t";
- case 10: return "\\n";
- case 13: return "\\r";
- case 34: return "\\\"";
- case 92: return "\\\\";
- }
- if (codePoint >= 32 && codePoint < 127) {
- return String.fromCharCode(codePoint);
- }
- }
- if (codePoint <= 0xffff) {
- return escapeChar(codePoint);
- }
- codePoint -= 0x10000;
- return escapeChar(((codePoint >> 10) & 0x3ff) + 0xd800) + escapeChar((codePoint & 0x3ff) + 0xdc00);
- }).join("") + '"';
- }
- exports._toEscapedUtf8String = _toEscapedUtf8String;
- function _toUtf8String(codePoints) {
- return codePoints.map(function (codePoint) {
- if (codePoint <= 0xffff) {
- return String.fromCharCode(codePoint);
- }
- codePoint -= 0x10000;
- return String.fromCharCode((((codePoint >> 10) & 0x3ff) + 0xd800), ((codePoint & 0x3ff) + 0xdc00));
- }).join("");
- }
- exports._toUtf8String = _toUtf8String;
- function toUtf8String(bytes, onError) {
- return _toUtf8String(getUtf8CodePoints(bytes, onError));
- }
- exports.toUtf8String = toUtf8String;
- function toUtf8CodePoints(str, form) {
- if (form === void 0) { form = UnicodeNormalizationForm.current; }
- return getUtf8CodePoints(toUtf8Bytes(str, form));
- }
- exports.toUtf8CodePoints = toUtf8CodePoints;
|