aboutsummaryrefslogtreecommitdiff
path: root/node_modules/@webassemblyjs/utf8/src/decoder.js
diff options
context:
space:
mode:
authorruki <waruqi@gmail.com>2018-11-08 00:38:48 +0800
committerruki <waruqi@gmail.com>2018-11-07 21:53:09 +0800
commit26105034da4fcce7ac883c899d781f016559310d (patch)
treec459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/@webassemblyjs/utf8/src/decoder.js
parent2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff)
downloadxmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz
xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip
switch to vuepress
Diffstat (limited to 'node_modules/@webassemblyjs/utf8/src/decoder.js')
-rw-r--r--node_modules/@webassemblyjs/utf8/src/decoder.js86
1 files changed, 86 insertions, 0 deletions
diff --git a/node_modules/@webassemblyjs/utf8/src/decoder.js b/node_modules/@webassemblyjs/utf8/src/decoder.js
new file mode 100644
index 00000000..227ba3ad
--- /dev/null
+++ b/node_modules/@webassemblyjs/utf8/src/decoder.js
@@ -0,0 +1,86 @@
+function con(b) {
+ if ((b & 0xc0) === 0x80) {
+ return b & 0x3f;
+ } else {
+ throw new Error("invalid UTF-8 encoding");
+ }
+}
+
+function code(min, n) {
+ if (n < min || (0xd800 <= n && n < 0xe000) || n >= 0x10000) {
+ throw new Error("invalid UTF-8 encoding");
+ } else {
+ return n;
+ }
+}
+
+export function decode(bytes) {
+ return _decode(bytes)
+ .map(x => String.fromCharCode(x))
+ .join("");
+}
+
+function _decode(bytes) {
+ if (bytes.length === 0) {
+ return [];
+ }
+
+ /**
+ * 1 byte
+ */
+ {
+ const [b1, ...bs] = bytes;
+
+ if (b1 < 0x80) {
+ return [code(0x0, b1), ..._decode(bs)];
+ }
+
+ if (b1 < 0xc0) {
+ throw new Error("invalid UTF-8 encoding");
+ }
+ }
+
+ /**
+ * 2 bytes
+ */
+ {
+ const [b1, b2, ...bs] = bytes;
+
+ if (b1 < 0xe0) {
+ return [code(0x80, ((b1 & 0x1f) << 6) + con(b2)), ..._decode(bs)];
+ }
+ }
+
+ /**
+ * 3 bytes
+ */
+ {
+ const [b1, b2, b3, ...bs] = bytes;
+
+ if (b1 < 0xf0) {
+ return [
+ code(0x800, ((b1 & 0x0f) << 12) + (con(b2) << 6) + con(b3)),
+ ..._decode(bs)
+ ];
+ }
+ }
+
+ /**
+ * 4 bytes
+ */
+ {
+ const [b1, b2, b3, b4, ...bs] = bytes;
+
+ if (b1 < 0xf8) {
+ return [
+ code(
+ 0x10000,
+ ((((b1 & 0x07) << 18) + con(b2)) << 12) + (con(b3) << 6) + con(b4)
+ ),
+ ..._decode(bs)
+ ];
+ }
+ }
+
+ throw new Error("invalid UTF-8 encoding");
+}