@qevm/strings 5.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/README.md +47 -0
- package/lib/_version.d.ts +2 -0
- package/lib/_version.d.ts.map +1 -0
- package/lib/_version.js +5 -0
- package/lib/_version.js.map +1 -0
- package/lib/bytes32.d.ts +4 -0
- package/lib/bytes32.d.ts.map +1 -0
- package/lib/bytes32.js +35 -0
- package/lib/bytes32.js.map +1 -0
- package/lib/idna.d.ts +5 -0
- package/lib/idna.d.ts.map +1 -0
- package/lib/idna.js +189 -0
- package/lib/idna.js.map +1 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +17 -0
- package/lib/index.js.map +1 -0
- package/lib/utf8.d.ts +27 -0
- package/lib/utf8.d.ts.map +1 -0
- package/lib/utf8.js +257 -0
- package/lib/utf8.js.map +1 -0
- package/package.json +29 -0
- package/src.ts/_version.ts +1 -0
- package/src.ts/bytes32.ts +35 -0
- package/src.ts/idna.ts +207 -0
- package/src.ts/index.ts +23 -0
- package/src.ts/utf8.ts +295 -0
package/lib/utf8.js
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Utf8ErrorFuncs = exports.Utf8ErrorReason = exports.UnicodeNormalizationForm = void 0;
|
|
4
|
+
exports.toUtf8Bytes = toUtf8Bytes;
|
|
5
|
+
exports._toEscapedUtf8String = _toEscapedUtf8String;
|
|
6
|
+
exports._toUtf8String = _toUtf8String;
|
|
7
|
+
exports.toUtf8String = toUtf8String;
|
|
8
|
+
exports.toUtf8CodePoints = toUtf8CodePoints;
|
|
9
|
+
var bytes_1 = require("@qevm/bytes");
|
|
10
|
+
var logger_1 = require("@ethersproject/logger");
|
|
11
|
+
var _version_1 = require("./_version");
|
|
12
|
+
var logger = new logger_1.Logger(_version_1.version);
|
|
13
|
+
///////////////////////////////
|
|
14
|
+
var UnicodeNormalizationForm;
|
|
15
|
+
(function (UnicodeNormalizationForm) {
|
|
16
|
+
UnicodeNormalizationForm["current"] = "";
|
|
17
|
+
UnicodeNormalizationForm["NFC"] = "NFC";
|
|
18
|
+
UnicodeNormalizationForm["NFD"] = "NFD";
|
|
19
|
+
UnicodeNormalizationForm["NFKC"] = "NFKC";
|
|
20
|
+
UnicodeNormalizationForm["NFKD"] = "NFKD";
|
|
21
|
+
})(UnicodeNormalizationForm || (exports.UnicodeNormalizationForm = UnicodeNormalizationForm = {}));
|
|
22
|
+
;
|
|
23
|
+
var Utf8ErrorReason;
|
|
24
|
+
(function (Utf8ErrorReason) {
|
|
25
|
+
// A continuation byte was present where there was nothing to continue
|
|
26
|
+
// - offset = the index the codepoint began in
|
|
27
|
+
Utf8ErrorReason["UNEXPECTED_CONTINUE"] = "unexpected continuation byte";
|
|
28
|
+
// An invalid (non-continuation) byte to start a UTF-8 codepoint was found
|
|
29
|
+
// - offset = the index the codepoint began in
|
|
30
|
+
Utf8ErrorReason["BAD_PREFIX"] = "bad codepoint prefix";
|
|
31
|
+
// The string is too short to process the expected codepoint
|
|
32
|
+
// - offset = the index the codepoint began in
|
|
33
|
+
Utf8ErrorReason["OVERRUN"] = "string overrun";
|
|
34
|
+
// A missing continuation byte was expected but not found
|
|
35
|
+
// - offset = the index the continuation byte was expected at
|
|
36
|
+
Utf8ErrorReason["MISSING_CONTINUE"] = "missing continuation byte";
|
|
37
|
+
// The computed code point is outside the range for UTF-8
|
|
38
|
+
// - offset = start of this codepoint
|
|
39
|
+
// - badCodepoint = the computed codepoint; outside the UTF-8 range
|
|
40
|
+
Utf8ErrorReason["OUT_OF_RANGE"] = "out of UTF-8 range";
|
|
41
|
+
// UTF-8 strings may not contain UTF-16 surrogate pairs
|
|
42
|
+
// - offset = start of this codepoint
|
|
43
|
+
// - badCodepoint = the computed codepoint; inside the UTF-16 surrogate range
|
|
44
|
+
Utf8ErrorReason["UTF16_SURROGATE"] = "UTF-16 surrogate";
|
|
45
|
+
// The string is an overlong representation
|
|
46
|
+
// - offset = start of this codepoint
|
|
47
|
+
// - badCodepoint = the computed codepoint; already bounds checked
|
|
48
|
+
Utf8ErrorReason["OVERLONG"] = "overlong representation";
|
|
49
|
+
})(Utf8ErrorReason || (exports.Utf8ErrorReason = Utf8ErrorReason = {}));
|
|
50
|
+
;
|
|
51
|
+
function errorFunc(reason, offset, bytes, output, badCodepoint) {
|
|
52
|
+
return logger.throwArgumentError("invalid codepoint at offset ".concat(offset, "; ").concat(reason), "bytes", bytes);
|
|
53
|
+
}
|
|
54
|
+
function ignoreFunc(reason, offset, bytes, output, badCodepoint) {
|
|
55
|
+
// If there is an invalid prefix (including stray continuation), skip any additional continuation bytes
|
|
56
|
+
if (reason === Utf8ErrorReason.BAD_PREFIX || reason === Utf8ErrorReason.UNEXPECTED_CONTINUE) {
|
|
57
|
+
var i = 0;
|
|
58
|
+
for (var o = offset + 1; o < bytes.length; o++) {
|
|
59
|
+
if (bytes[o] >> 6 !== 0x02) {
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
i++;
|
|
63
|
+
}
|
|
64
|
+
return i;
|
|
65
|
+
}
|
|
66
|
+
// This byte runs us past the end of the string, so just jump to the end
|
|
67
|
+
// (but the first byte was read already read and therefore skipped)
|
|
68
|
+
if (reason === Utf8ErrorReason.OVERRUN) {
|
|
69
|
+
return bytes.length - offset - 1;
|
|
70
|
+
}
|
|
71
|
+
// Nothing to skip
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
function replaceFunc(reason, offset, bytes, output, badCodepoint) {
|
|
75
|
+
// Overlong representations are otherwise "valid" code points; just non-deistingtished
|
|
76
|
+
if (reason === Utf8ErrorReason.OVERLONG) {
|
|
77
|
+
output.push(badCodepoint);
|
|
78
|
+
return 0;
|
|
79
|
+
}
|
|
80
|
+
// Put the replacement character into the output
|
|
81
|
+
output.push(0xfffd);
|
|
82
|
+
// Otherwise, process as if ignoring errors
|
|
83
|
+
return ignoreFunc(reason, offset, bytes, output, badCodepoint);
|
|
84
|
+
}
|
|
85
|
+
// Common error handing strategies
|
|
86
|
+
exports.Utf8ErrorFuncs = Object.freeze({
|
|
87
|
+
error: errorFunc,
|
|
88
|
+
ignore: ignoreFunc,
|
|
89
|
+
replace: replaceFunc
|
|
90
|
+
});
|
|
91
|
+
// http://stackoverflow.com/questions/13356493/decode-utf-8-with-javascript#13691499
|
|
92
|
+
function getUtf8CodePoints(bytes, onError) {
|
|
93
|
+
if (onError == null) {
|
|
94
|
+
onError = exports.Utf8ErrorFuncs.error;
|
|
95
|
+
}
|
|
96
|
+
bytes = (0, bytes_1.arrayify)(bytes);
|
|
97
|
+
var result = [];
|
|
98
|
+
var i = 0;
|
|
99
|
+
// Invalid bytes are ignored
|
|
100
|
+
while (i < bytes.length) {
|
|
101
|
+
var c = bytes[i++];
|
|
102
|
+
// 0xxx xxxx
|
|
103
|
+
if (c >> 7 === 0) {
|
|
104
|
+
result.push(c);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
// Multibyte; how many bytes left for this character?
|
|
108
|
+
var extraLength = null;
|
|
109
|
+
var overlongMask = null;
|
|
110
|
+
// 110x xxxx 10xx xxxx
|
|
111
|
+
if ((c & 0xe0) === 0xc0) {
|
|
112
|
+
extraLength = 1;
|
|
113
|
+
overlongMask = 0x7f;
|
|
114
|
+
// 1110 xxxx 10xx xxxx 10xx xxxx
|
|
115
|
+
}
|
|
116
|
+
else if ((c & 0xf0) === 0xe0) {
|
|
117
|
+
extraLength = 2;
|
|
118
|
+
overlongMask = 0x7ff;
|
|
119
|
+
// 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx
|
|
120
|
+
}
|
|
121
|
+
else if ((c & 0xf8) === 0xf0) {
|
|
122
|
+
extraLength = 3;
|
|
123
|
+
overlongMask = 0xffff;
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
if ((c & 0xc0) === 0x80) {
|
|
127
|
+
i += onError(Utf8ErrorReason.UNEXPECTED_CONTINUE, i - 1, bytes, result);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
i += onError(Utf8ErrorReason.BAD_PREFIX, i - 1, bytes, result);
|
|
131
|
+
}
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
// Do we have enough bytes in our data?
|
|
135
|
+
if (i - 1 + extraLength >= bytes.length) {
|
|
136
|
+
i += onError(Utf8ErrorReason.OVERRUN, i - 1, bytes, result);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
// Remove the length prefix from the char
|
|
140
|
+
var res = c & ((1 << (8 - extraLength - 1)) - 1);
|
|
141
|
+
for (var j = 0; j < extraLength; j++) {
|
|
142
|
+
var nextChar = bytes[i];
|
|
143
|
+
// Invalid continuation byte
|
|
144
|
+
if ((nextChar & 0xc0) != 0x80) {
|
|
145
|
+
i += onError(Utf8ErrorReason.MISSING_CONTINUE, i, bytes, result);
|
|
146
|
+
res = null;
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
;
|
|
150
|
+
res = (res << 6) | (nextChar & 0x3f);
|
|
151
|
+
i++;
|
|
152
|
+
}
|
|
153
|
+
// See above loop for invalid continuation byte
|
|
154
|
+
if (res === null) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
// Maximum code point
|
|
158
|
+
if (res > 0x10ffff) {
|
|
159
|
+
i += onError(Utf8ErrorReason.OUT_OF_RANGE, i - 1 - extraLength, bytes, result, res);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
// Reserved for UTF-16 surrogate halves
|
|
163
|
+
if (res >= 0xd800 && res <= 0xdfff) {
|
|
164
|
+
i += onError(Utf8ErrorReason.UTF16_SURROGATE, i - 1 - extraLength, bytes, result, res);
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
// Check for overlong sequences (more bytes than needed)
|
|
168
|
+
if (res <= overlongMask) {
|
|
169
|
+
i += onError(Utf8ErrorReason.OVERLONG, i - 1 - extraLength, bytes, result, res);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
result.push(res);
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
// http://stackoverflow.com/questions/18729405/how-to-convert-utf8-string-to-byte-array
|
|
177
|
+
function toUtf8Bytes(str, form) {
|
|
178
|
+
if (form === void 0) { form = UnicodeNormalizationForm.current; }
|
|
179
|
+
if (form != UnicodeNormalizationForm.current) {
|
|
180
|
+
logger.checkNormalize();
|
|
181
|
+
str = str.normalize(form);
|
|
182
|
+
}
|
|
183
|
+
var result = [];
|
|
184
|
+
for (var i = 0; i < str.length; i++) {
|
|
185
|
+
var c = str.charCodeAt(i);
|
|
186
|
+
if (c < 0x80) {
|
|
187
|
+
result.push(c);
|
|
188
|
+
}
|
|
189
|
+
else if (c < 0x800) {
|
|
190
|
+
result.push((c >> 6) | 0xc0);
|
|
191
|
+
result.push((c & 0x3f) | 0x80);
|
|
192
|
+
}
|
|
193
|
+
else if ((c & 0xfc00) == 0xd800) {
|
|
194
|
+
i++;
|
|
195
|
+
var c2 = str.charCodeAt(i);
|
|
196
|
+
if (i >= str.length || (c2 & 0xfc00) !== 0xdc00) {
|
|
197
|
+
throw new Error("invalid utf-8 string");
|
|
198
|
+
}
|
|
199
|
+
// Surrogate Pair
|
|
200
|
+
var pair = 0x10000 + ((c & 0x03ff) << 10) + (c2 & 0x03ff);
|
|
201
|
+
result.push((pair >> 18) | 0xf0);
|
|
202
|
+
result.push(((pair >> 12) & 0x3f) | 0x80);
|
|
203
|
+
result.push(((pair >> 6) & 0x3f) | 0x80);
|
|
204
|
+
result.push((pair & 0x3f) | 0x80);
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
result.push((c >> 12) | 0xe0);
|
|
208
|
+
result.push(((c >> 6) & 0x3f) | 0x80);
|
|
209
|
+
result.push((c & 0x3f) | 0x80);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return (0, bytes_1.arrayify)(result);
|
|
213
|
+
}
|
|
214
|
+
;
|
|
215
|
+
function escapeChar(value) {
|
|
216
|
+
var hex = ("0000" + value.toString(16));
|
|
217
|
+
return "\\u" + hex.substring(hex.length - 4);
|
|
218
|
+
}
|
|
219
|
+
function _toEscapedUtf8String(bytes, onError) {
|
|
220
|
+
return '"' + getUtf8CodePoints(bytes, onError).map(function (codePoint) {
|
|
221
|
+
if (codePoint < 256) {
|
|
222
|
+
switch (codePoint) {
|
|
223
|
+
case 8: return "\\b";
|
|
224
|
+
case 9: return "\\t";
|
|
225
|
+
case 10: return "\\n";
|
|
226
|
+
case 13: return "\\r";
|
|
227
|
+
case 34: return "\\\"";
|
|
228
|
+
case 92: return "\\\\";
|
|
229
|
+
}
|
|
230
|
+
if (codePoint >= 32 && codePoint < 127) {
|
|
231
|
+
return String.fromCharCode(codePoint);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (codePoint <= 0xffff) {
|
|
235
|
+
return escapeChar(codePoint);
|
|
236
|
+
}
|
|
237
|
+
codePoint -= 0x10000;
|
|
238
|
+
return escapeChar(((codePoint >> 10) & 0x3ff) + 0xd800) + escapeChar((codePoint & 0x3ff) + 0xdc00);
|
|
239
|
+
}).join("") + '"';
|
|
240
|
+
}
|
|
241
|
+
function _toUtf8String(codePoints) {
|
|
242
|
+
return codePoints.map(function (codePoint) {
|
|
243
|
+
if (codePoint <= 0xffff) {
|
|
244
|
+
return String.fromCharCode(codePoint);
|
|
245
|
+
}
|
|
246
|
+
codePoint -= 0x10000;
|
|
247
|
+
return String.fromCharCode((((codePoint >> 10) & 0x3ff) + 0xd800), ((codePoint & 0x3ff) + 0xdc00));
|
|
248
|
+
}).join("");
|
|
249
|
+
}
|
|
250
|
+
function toUtf8String(bytes, onError) {
|
|
251
|
+
return _toUtf8String(getUtf8CodePoints(bytes, onError));
|
|
252
|
+
}
|
|
253
|
+
function toUtf8CodePoints(str, form) {
|
|
254
|
+
if (form === void 0) { form = UnicodeNormalizationForm.current; }
|
|
255
|
+
return getUtf8CodePoints(toUtf8Bytes(str, form));
|
|
256
|
+
}
|
|
257
|
+
//# sourceMappingURL=utf8.js.map
|
package/lib/utf8.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utf8.js","sourceRoot":"","sources":["../src.ts/utf8.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;AAyMb,kCAyCC;AAOD,oDAwBC;AAED,sCAWC;AAED,oCAEC;AAED,4CAEC;AApSD,qCAAkD;AAElD,gDAA+C;AAC/C,uCAAqC;AACrC,IAAM,MAAM,GAAG,IAAI,eAAM,CAAC,kBAAO,CAAC,CAAC;AAEnC,+BAA+B;AAE/B,IAAY,wBAMX;AAND,WAAY,wBAAwB;IAChC,wCAAa,CAAA;IACb,uCAAgB,CAAA;IAChB,uCAAgB,CAAA;IAChB,yCAAiB,CAAA;IACjB,yCAAiB,CAAA;AACrB,CAAC,EANW,wBAAwB,wCAAxB,wBAAwB,QAMnC;AAAA,CAAC;AAEF,IAAY,eA+BX;AA/BD,WAAY,eAAe;IACvB,sEAAsE;IACtE,8CAA8C;IAC9C,uEAAsD,CAAA;IAEtD,0EAA0E;IAC1E,8CAA8C;IAC9C,sDAA8C,CAAA;IAE9C,4DAA4D;IAC5D,8CAA8C;IAC9C,6CAAwC,CAAA;IAExC,yDAAyD;IACzD,6DAA6D;IAC7D,iEAAmD,CAAA;IAEnD,yDAAyD;IACzD,2CAA2C;IAC3C,mEAAmE;IACnE,sDAA4C,CAAA;IAE5C,uDAAuD;IACvD,2CAA2C;IAC3C,6EAA6E;IAC7E,uDAA0C,CAAA;IAE1C,2CAA2C;IAC3C,2CAA2C;IAC3C,kEAAkE;IAClE,uDAAiD,CAAA;AACrD,CAAC,EA/BW,eAAe,+BAAf,eAAe,QA+B1B;AAAA,CAAC;AAKF,SAAS,SAAS,CAAC,MAAuB,EAAE,MAAc,EAAE,KAAwB,E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,wBAAwB,CAAC,OAAO;IAC3G,OAAO,iBAAiB,CAAC,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC;AACrD,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": "Eugene Kuleshov",
|
|
3
|
+
"dependencies": {
|
|
4
|
+
"@qevm/bytes": "5.7.1",
|
|
5
|
+
"@ethersproject/constants": "^5.7.0",
|
|
6
|
+
"@ethersproject/logger": "^5.7.0"
|
|
7
|
+
},
|
|
8
|
+
"description": "String utility functions.",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"Ethereum",
|
|
11
|
+
"qethers",
|
|
12
|
+
"strings",
|
|
13
|
+
"utf8"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"main": "./lib/index.js",
|
|
17
|
+
"module": "./lib.esm/index.js",
|
|
18
|
+
"name": "@qevm/strings",
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
24
|
+
},
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"tarballHash": "0xbde9a4dad59a25a6b1956cf36146ddcceb251b39b2761ac4899b24876656c496",
|
|
27
|
+
"types": "./lib/index.d.ts",
|
|
28
|
+
"version": "5.7.0"
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const version = "strings/5.7.0";
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { HashZero } from "@ethersproject/constants";
|
|
4
|
+
import { arrayify, BytesLike, concat, hexlify } from "@qevm/bytes";
|
|
5
|
+
|
|
6
|
+
import { toUtf8Bytes, toUtf8String } from "./utf8";
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
export function formatBytes32String(text: string): string {
|
|
10
|
+
|
|
11
|
+
// Get the bytes
|
|
12
|
+
const bytes = toUtf8Bytes(text);
|
|
13
|
+
|
|
14
|
+
// Check we have room for null-termination
|
|
15
|
+
if (bytes.length > 31) { throw new Error("bytes32 string must be less than 32 bytes"); }
|
|
16
|
+
|
|
17
|
+
// Zero-pad (implicitly null-terminates)
|
|
18
|
+
return hexlify(concat([ bytes, HashZero ]).slice(0, 32));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function parseBytes32String(bytes: BytesLike): string {
|
|
22
|
+
const data = arrayify(bytes);
|
|
23
|
+
|
|
24
|
+
// Must be 32 bytes with a null-termination
|
|
25
|
+
if (data.length !== 32) { throw new Error("invalid bytes32 - not 32 bytes long"); }
|
|
26
|
+
if (data[31] !== 0) { throw new Error("invalid bytes32 string - no null terminator"); }
|
|
27
|
+
|
|
28
|
+
// Find the null termination
|
|
29
|
+
let length = 31;
|
|
30
|
+
while (data[length - 1] === 0) { length--; }
|
|
31
|
+
|
|
32
|
+
// Determine the string value
|
|
33
|
+
return toUtf8String(data.slice(0, length));
|
|
34
|
+
}
|
|
35
|
+
|
package/src.ts/idna.ts
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { toUtf8CodePoints, _toUtf8String, UnicodeNormalizationForm } from "./utf8";
|
|
4
|
+
|
|
5
|
+
type Ranged = {
|
|
6
|
+
l: number, // Lo value
|
|
7
|
+
h: number, // High value (less the lo)
|
|
8
|
+
d?: number, // Delta/stride (default: 1)
|
|
9
|
+
s?: number, // Shift (default: 1)
|
|
10
|
+
e?: Array<number> // Exceptions to skip
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type Table = { [ src: number ]: Array<number> };
|
|
14
|
+
|
|
15
|
+
function bytes2(data: string): Array<number> {
|
|
16
|
+
if ((data.length % 4) !== 0) { throw new Error("bad data"); }
|
|
17
|
+
let result = [];
|
|
18
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
19
|
+
result.push(parseInt(data.substring(i, i + 4), 16));
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function createTable(data: string, func?: (value: string) => Array<number>): Table {
|
|
25
|
+
if (!func) {
|
|
26
|
+
func = function(value: string) { return [ parseInt(value, 16) ]; }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let lo = 0;
|
|
30
|
+
|
|
31
|
+
let result: Table = { };
|
|
32
|
+
data.split(",").forEach((pair) => {
|
|
33
|
+
let comps = pair.split(":");
|
|
34
|
+
lo += parseInt(comps[0], 16);
|
|
35
|
+
result[lo] = func(comps[1]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function createRangeTable(data: string): Array<Ranged> {
|
|
42
|
+
let hi = 0;
|
|
43
|
+
return data.split(",").map((v) => {
|
|
44
|
+
let comps = v.split("-");
|
|
45
|
+
if (comps.length === 1) {
|
|
46
|
+
comps[1] = "0";
|
|
47
|
+
} else if (comps[1] === "") {
|
|
48
|
+
comps[1] = "1";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let lo = hi + parseInt(comps[0], 16);
|
|
52
|
+
hi = parseInt(comps[1], 16);
|
|
53
|
+
return { l: lo, h: hi };
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function matchMap(value: number, ranges: Array<Ranged>): Ranged {
|
|
58
|
+
let lo = 0;
|
|
59
|
+
for (let i = 0; i < ranges.length; i++) {
|
|
60
|
+
let range = ranges[i];
|
|
61
|
+
lo += range.l;
|
|
62
|
+
if (value >= lo && value <= lo + range.h && ((value - lo) % (range.d || 1)) === 0) {
|
|
63
|
+
if (range.e && range.e.indexOf(value - lo) !== -1) { continue; }
|
|
64
|
+
return range;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const Table_A_1_ranges = createRangeTable("221,13-1b,5f-,40-10,51-f,11-3,3-3,2-2,2-4,8,2,15,2d,28-8,88,48,27-,3-5,11-20,27-,8,28,3-5,12,18,b-a,1c-4,6-16,2-d,2-2,2,1b-4,17-9,8f-,10,f,1f-2,1c-34,33-14e,4,36-,13-,6-2,1a-f,4,9-,3-,17,8,2-2,5-,2,8-,3-,4-8,2-3,3,6-,16-6,2-,7-3,3-,17,8,3,3,3-,2,6-3,3-,4-a,5,2-6,10-b,4,8,2,4,17,8,3,6-,b,4,4-,2-e,2-4,b-10,4,9-,3-,17,8,3-,5-,9-2,3-,4-7,3-3,3,4-3,c-10,3,7-2,4,5-2,3,2,3-2,3-2,4-2,9,4-3,6-2,4,5-8,2-e,d-d,4,9,4,18,b,6-3,8,4,5-6,3-8,3-3,b-11,3,9,4,18,b,6-3,8,4,5-6,3-6,2,3-3,b-11,3,9,4,18,11-3,7-,4,5-8,2-7,3-3,b-11,3,13-2,19,a,2-,8-2,2-3,7,2,9-11,4-b,3b-3,1e-24,3,2-,3,2-,2-5,5,8,4,2,2-,3,e,4-,6,2,7-,b-,3-21,49,23-5,1c-3,9,25,10-,2-2f,23,6,3,8-2,5-5,1b-45,27-9,2a-,2-3,5b-4,45-4,53-5,8,40,2,5-,8,2,5-,28,2,5-,20,2,5-,8,2,5-,8,8,18,20,2,5-,8,28,14-5,1d-22,56-b,277-8,1e-2,52-e,e,8-a,18-8,15-b,e,4,3-b,5e-2,b-15,10,b-5,59-7,2b-555,9d-3,5b-5,17-,7-,27-,7-,9,2,2,2,20-,36,10,f-,7,14-,4,a,54-3,2-6,6-5,9-,1c-10,13-1d,1c-14,3c-,10-6,32-b,240-30,28-18,c-14,a0,115-,3,66-,b-76,5,5-,1d,24,2,5-2,2,8-,35-2,19,f-10,1d-3,311-37f,1b,5a-b,d7-19,d-3,41,57-,68-4,29-3,5f,29-37,2e-2,25-c,2c-2,4e-3,30,78-3,64-,20,19b7-49,51a7-59,48e-2,38-738,2ba5-5b,222f-,3c-94,8-b,6-4,1b,6,2,3,3,6d-20,16e-f,41-,37-7,2e-2,11-f,5-b,18-,b,14,5-3,6,88-,2,bf-2,7-,7-,7-,4-2,8,8-9,8-2ff,20,5-b,1c-b4,27-,27-cbb1,f7-9,28-2,b5-221,56,48,3-,2-,3-,5,d,2,5,3,42,5-,9,8,1d,5,6,2-2,8,153-3,123-3,33-27fd,a6da-5128,21f-5df,3-fffd,3-fffd,3-fffd,3-fffd,3-fffd,3-fffd,3-fffd,3-fffd,3-fffd,3-fffd,3-fffd,3,2-1d,61-ff7d");
|
|
71
|
+
|
|
72
|
+
// @TODO: Make this relative...
|
|
73
|
+
const Table_B_1_flags = "ad,34f,1806,180b,180c,180d,200b,200c,200d,2060,feff".split(",").map((v) => parseInt(v, 16));
|
|
74
|
+
|
|
75
|
+
const Table_B_2_ranges: Array<Ranged> = [
|
|
76
|
+
{ h: 25, s: 32, l: 65 },
|
|
77
|
+
{ h: 30, s: 32, e: [ 23 ], l: 127 },
|
|
78
|
+
{ h: 54, s: 1, e: [ 48 ], l: 64, d: 2 },
|
|
79
|
+
{ h: 14, s: 1, l: 57, d: 2 },
|
|
80
|
+
{ h: 44, s: 1, l: 17, d: 2 },
|
|
81
|
+
{ h: 10, s: 1, e: [ 2, 6, 8 ], l: 61, d: 2 },
|
|
82
|
+
{ h: 16, s: 1, l: 68, d: 2 },
|
|
83
|
+
{ h: 84, s: 1, e: [ 18, 24, 66 ], l: 19, d: 2 },
|
|
84
|
+
{ h: 26, s: 32, e: [ 17 ], l: 435 },
|
|
85
|
+
{ h: 22, s: 1, l: 71, d: 2 },
|
|
86
|
+
{ h: 15, s: 80, l: 40 },
|
|
87
|
+
{ h: 31, s: 32, l: 16 },
|
|
88
|
+
{ h: 32, s: 1, l: 80, d: 2 },
|
|
89
|
+
{ h: 52, s: 1, l: 42, d: 2 },
|
|
90
|
+
{ h: 12, s: 1, l: 55, d: 2 },
|
|
91
|
+
{ h: 40, s: 1, e: [ 38 ], l: 15, d: 2 },
|
|
92
|
+
{ h: 14, s: 1, l: 48, d: 2 },
|
|
93
|
+
{ h: 37, s: 48, l: 49 },
|
|
94
|
+
{ h: 148, s: 1, l: 6351, d: 2 },
|
|
95
|
+
{ h: 88, s: 1, l: 160, d: 2 },
|
|
96
|
+
{ h: 15, s: 16, l: 704 },
|
|
97
|
+
{ h: 25, s: 26, l: 854 },
|
|
98
|
+
{ h: 25, s: 32, l: 55915 },
|
|
99
|
+
{ h: 37, s: 40, l: 1247 },
|
|
100
|
+
{ h: 25, s: -119711, l: 53248 },
|
|
101
|
+
{ h: 25, s: -119763, l: 52 },
|
|
102
|
+
{ h: 25, s: -119815, l: 52 },
|
|
103
|
+
{ h: 25, s: -119867, e: [ 1, 4, 5, 7, 8, 11, 12, 17 ], l: 52 },
|
|
104
|
+
{ h: 25, s: -119919, l: 52 },
|
|
105
|
+
{ h: 24, s: -119971, e: [ 2, 7, 8, 17 ], l: 52 },
|
|
106
|
+
{ h: 24, s: -120023, e: [ 2, 7, 13, 15, 16, 17 ], l: 52 },
|
|
107
|
+
{ h: 25, s: -120075, l: 52 },
|
|
108
|
+
{ h: 25, s: -120127, l: 52 },
|
|
109
|
+
{ h: 25, s: -120179, l: 52 },
|
|
110
|
+
{ h: 25, s: -120231, l: 52 },
|
|
111
|
+
{ h: 25, s: -120283, l: 52 },
|
|
112
|
+
{ h: 25, s: -120335, l: 52 },
|
|
113
|
+
{ h: 24, s: -119543, e: [ 17 ], l: 56 },
|
|
114
|
+
{ h: 24, s: -119601, e: [ 17 ], l: 58 },
|
|
115
|
+
{ h: 24, s: -119659, e: [ 17 ], l: 58 },
|
|
116
|
+
{ h: 24, s: -119717, e: [ 17 ], l: 58 },
|
|
117
|
+
{ h: 24, s: -119775, e: [ 17 ], l: 58 }
|
|
118
|
+
];
|
|
119
|
+
const Table_B_2_lut_abs = createTable("b5:3bc,c3:ff,7:73,2:253,5:254,3:256,1:257,5:259,1:25b,3:260,1:263,2:269,1:268,5:26f,1:272,2:275,7:280,3:283,5:288,3:28a,1:28b,5:292,3f:195,1:1bf,29:19e,125:3b9,8b:3b2,1:3b8,1:3c5,3:3c6,1:3c0,1a:3ba,1:3c1,1:3c3,2:3b8,1:3b5,1bc9:3b9,1c:1f76,1:1f77,f:1f7a,1:1f7b,d:1f78,1:1f79,1:1f7c,1:1f7d,107:63,5:25b,4:68,1:68,1:68,3:69,1:69,1:6c,3:6e,4:70,1:71,1:72,1:72,1:72,7:7a,2:3c9,2:7a,2:6b,1:e5,1:62,1:63,3:65,1:66,2:6d,b:3b3,1:3c0,6:64,1b574:3b8,1a:3c3,20:3b8,1a:3c3,20:3b8,1a:3c3,20:3b8,1a:3c3,20:3b8,1a:3c3");
|
|
120
|
+
const Table_B_2_lut_rel = createTable("179:1,2:1,2:1,5:1,2:1,a:4f,a:1,8:1,2:1,2:1,3:1,5:1,3:1,4:1,2:1,3:1,4:1,8:2,1:1,2:2,1:1,2:2,27:2,195:26,2:25,1:25,1:25,2:40,2:3f,1:3f,33:1,11:-6,1:-9,1ac7:-3a,6d:-8,1:-8,1:-8,1:-8,1:-8,1:-8,1:-8,1:-8,9:-8,1:-8,1:-8,1:-8,1:-8,1:-8,b:-8,1:-8,1:-8,1:-8,1:-8,1:-8,1:-8,1:-8,9:-8,1:-8,1:-8,1:-8,1:-8,1:-8,1:-8,1:-8,9:-8,1:-8,1:-8,1:-8,1:-8,1:-8,c:-8,2:-8,2:-8,2:-8,9:-8,1:-8,1:-8,1:-8,1:-8,1:-8,1:-8,1:-8,49:-8,1:-8,1:-4a,1:-4a,d:-56,1:-56,1:-56,1:-56,d:-8,1:-8,f:-8,1:-8,3:-7");
|
|
121
|
+
const Table_B_2_complex = createTable("df:00730073,51:00690307,19:02BC006E,a7:006A030C,18a:002003B9,16:03B903080301,20:03C503080301,1d7:05650582,190f:00680331,1:00740308,1:0077030A,1:0079030A,1:006102BE,b6:03C50313,2:03C503130300,2:03C503130301,2:03C503130342,2a:1F0003B9,1:1F0103B9,1:1F0203B9,1:1F0303B9,1:1F0403B9,1:1F0503B9,1:1F0603B9,1:1F0703B9,1:1F0003B9,1:1F0103B9,1:1F0203B9,1:1F0303B9,1:1F0403B9,1:1F0503B9,1:1F0603B9,1:1F0703B9,1:1F2003B9,1:1F2103B9,1:1F2203B9,1:1F2303B9,1:1F2403B9,1:1F2503B9,1:1F2603B9,1:1F2703B9,1:1F2003B9,1:1F2103B9,1:1F2203B9,1:1F2303B9,1:1F2403B9,1:1F2503B9,1:1F2603B9,1:1F2703B9,1:1F6003B9,1:1F6103B9,1:1F6203B9,1:1F6303B9,1:1F6403B9,1:1F6503B9,1:1F6603B9,1:1F6703B9,1:1F6003B9,1:1F6103B9,1:1F6203B9,1:1F6303B9,1:1F6403B9,1:1F6503B9,1:1F6603B9,1:1F6703B9,3:1F7003B9,1:03B103B9,1:03AC03B9,2:03B10342,1:03B1034203B9,5:03B103B9,6:1F7403B9,1:03B703B9,1:03AE03B9,2:03B70342,1:03B7034203B9,5:03B703B9,6:03B903080300,1:03B903080301,3:03B90342,1:03B903080342,b:03C503080300,1:03C503080301,1:03C10313,2:03C50342,1:03C503080342,b:1F7C03B9,1:03C903B9,1:03CE03B9,2:03C90342,1:03C9034203B9,5:03C903B9,ac:00720073,5b:00B00063,6:00B00066,d:006E006F,a:0073006D,1:00740065006C,1:0074006D,124f:006800700061,2:00610075,2:006F0076,b:00700061,1:006E0061,1:03BC0061,1:006D0061,1:006B0061,1:006B0062,1:006D0062,1:00670062,3:00700066,1:006E0066,1:03BC0066,4:0068007A,1:006B0068007A,1:006D0068007A,1:00670068007A,1:00740068007A,15:00700061,1:006B00700061,1:006D00700061,1:006700700061,8:00700076,1:006E0076,1:03BC0076,1:006D0076,1:006B0076,1:006D0076,1:00700077,1:006E0077,1:03BC0077,1:006D0077,1:006B0077,1:006D0077,1:006B03C9,1:006D03C9,2:00620071,3:00632215006B0067,1:0063006F002E,1:00640062,1:00670079,2:00680070,2:006B006B,1:006B006D,9:00700068,2:00700070006D,1:00700072,2:00730076,1:00770062,c723:00660066,1:00660069,1:0066006C,1:006600660069,1:00660066006C,1:00730074,1:00730074,d:05740576,1:05740565,1:0574056B,1:057E0576,1:0574056D", bytes2);
|
|
122
|
+
|
|
123
|
+
const Table_C_ranges = createRangeTable("80-20,2a0-,39c,32,f71,18e,7f2-f,19-7,30-4,7-5,f81-b,5,a800-20ff,4d1-1f,110,fa-6,d174-7,2e84-,ffff-,ffff-,ffff-,ffff-,ffff-,ffff-,ffff-,ffff-,ffff-,ffff-,ffff-,ffff-,2,1f-5f,ff7f-20001");
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
function flatten(values: Array<Array<number>>): Array<number> {
|
|
127
|
+
return values.reduce((accum, value) => {
|
|
128
|
+
value.forEach((value) => { accum.push(value); });
|
|
129
|
+
return accum;
|
|
130
|
+
}, [ ]);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function _nameprepTableA1(codepoint: number): boolean {
|
|
134
|
+
return !!matchMap(codepoint, Table_A_1_ranges);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function _nameprepTableB2(codepoint: number): Array<number> {
|
|
138
|
+
let range = matchMap(codepoint, Table_B_2_ranges);
|
|
139
|
+
if (range) { return [ codepoint + range.s ]; }
|
|
140
|
+
|
|
141
|
+
let codes = Table_B_2_lut_abs[codepoint];
|
|
142
|
+
if (codes) { return codes; }
|
|
143
|
+
|
|
144
|
+
let shift = Table_B_2_lut_rel[codepoint];
|
|
145
|
+
if (shift) { return [ codepoint + shift[0] ]; }
|
|
146
|
+
|
|
147
|
+
let complex = Table_B_2_complex[codepoint];
|
|
148
|
+
if (complex) { return complex; }
|
|
149
|
+
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function _nameprepTableC(codepoint: number): boolean {
|
|
154
|
+
return !!matchMap(codepoint, Table_C_ranges);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function nameprep(value: string): string {
|
|
158
|
+
|
|
159
|
+
// This allows platforms with incomplete normalize to bypass
|
|
160
|
+
// it for very basic names which the built-in toLowerCase
|
|
161
|
+
// will certainly handle correctly
|
|
162
|
+
if (value.match(/^[a-z0-9-]*$/i) && value.length <= 59) { return value.toLowerCase(); }
|
|
163
|
+
|
|
164
|
+
// Get the code points (keeping the current normalization)
|
|
165
|
+
let codes = toUtf8CodePoints(value);
|
|
166
|
+
|
|
167
|
+
codes = flatten(codes.map((code) => {
|
|
168
|
+
// Substitute Table B.1 (Maps to Nothing)
|
|
169
|
+
if (Table_B_1_flags.indexOf(code) >= 0) { return [ ]; }
|
|
170
|
+
if (code >= 0xfe00 && code <= 0xfe0f) { return [ ]; }
|
|
171
|
+
|
|
172
|
+
// Substitute Table B.2 (Case Folding)
|
|
173
|
+
let codesTableB2 = _nameprepTableB2(code);
|
|
174
|
+
if (codesTableB2) { return codesTableB2; }
|
|
175
|
+
|
|
176
|
+
// No Substitution
|
|
177
|
+
return [ code ];
|
|
178
|
+
}));
|
|
179
|
+
|
|
180
|
+
// Normalize using form KC
|
|
181
|
+
codes = toUtf8CodePoints(_toUtf8String(codes), UnicodeNormalizationForm.NFKC);
|
|
182
|
+
|
|
183
|
+
// Prohibit Tables C.1.2, C.2.2, C.3, C.4, C.5, C.6, C.7, C.8, C.9
|
|
184
|
+
codes.forEach((code) => {
|
|
185
|
+
if (_nameprepTableC(code)) {
|
|
186
|
+
throw new Error("STRINGPREP_CONTAINS_PROHIBITED");
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// Prohibit Unassigned Code Points (Table A.1)
|
|
191
|
+
codes.forEach((code) => {
|
|
192
|
+
if (_nameprepTableA1(code)) {
|
|
193
|
+
throw new Error("STRINGPREP_CONTAINS_UNASSIGNED");
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// IDNA extras
|
|
198
|
+
let name = _toUtf8String(codes);
|
|
199
|
+
|
|
200
|
+
// IDNA: 4.2.3.1
|
|
201
|
+
if (name.substring(0, 1) === "-" || name.substring(2, 4) === "--" || name.substring(name.length - 1) === "-") {
|
|
202
|
+
throw new Error("invalid hyphen");
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return name;
|
|
206
|
+
}
|
|
207
|
+
|
package/src.ts/index.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { formatBytes32String, parseBytes32String } from "./bytes32";
|
|
4
|
+
import { nameprep } from "./idna";
|
|
5
|
+
import { _toEscapedUtf8String, toUtf8Bytes, toUtf8CodePoints, toUtf8String, UnicodeNormalizationForm, Utf8ErrorFunc, Utf8ErrorFuncs, Utf8ErrorReason } from "./utf8";
|
|
6
|
+
|
|
7
|
+
export {
|
|
8
|
+
_toEscapedUtf8String,
|
|
9
|
+
toUtf8Bytes,
|
|
10
|
+
toUtf8CodePoints,
|
|
11
|
+
toUtf8String,
|
|
12
|
+
|
|
13
|
+
Utf8ErrorFunc,
|
|
14
|
+
Utf8ErrorFuncs,
|
|
15
|
+
Utf8ErrorReason,
|
|
16
|
+
|
|
17
|
+
UnicodeNormalizationForm,
|
|
18
|
+
|
|
19
|
+
formatBytes32String,
|
|
20
|
+
parseBytes32String,
|
|
21
|
+
|
|
22
|
+
nameprep
|
|
23
|
+
}
|