source: imaps-frontend/node_modules/@webassemblyjs/utf8/src/decoder.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 1.3 KB
Line 
1function con(b) {
2 if ((b & 0xc0) === 0x80) {
3 return b & 0x3f;
4 } else {
5 throw new Error("invalid UTF-8 encoding");
6 }
7}
8
9function code(min, n) {
10 if (n < min || (0xd800 <= n && n < 0xe000) || n >= 0x10000) {
11 throw new Error("invalid UTF-8 encoding");
12 } else {
13 return n;
14 }
15}
16
17export function decode(bytes) {
18 return _decode(bytes)
19 .map((x) => String.fromCharCode(x))
20 .join("");
21}
22
23function _decode(bytes) {
24 const result = [];
25 while (bytes.length > 0) {
26 const b1 = bytes[0];
27 if (b1 < 0x80) {
28 result.push(code(0x0, b1));
29 bytes = bytes.slice(1);
30 continue;
31 }
32
33 if (b1 < 0xc0) {
34 throw new Error("invalid UTF-8 encoding");
35 }
36
37 const b2 = bytes[1];
38 if (b1 < 0xe0) {
39 result.push(code(0x80, ((b1 & 0x1f) << 6) + con(b2)));
40 bytes = bytes.slice(2);
41 continue;
42 }
43
44 const b3 = bytes[2];
45 if (b1 < 0xf0) {
46 result.push(code(0x800, ((b1 & 0x0f) << 12) + (con(b2) << 6) + con(b3)));
47 bytes = bytes.slice(3);
48 continue;
49 }
50
51 const b4 = bytes[3];
52 if (b1 < 0xf8) {
53 result.push(
54 code(
55 0x10000,
56 ((((b1 & 0x07) << 18) + con(b2)) << 12) + (con(b3) << 6) + con(b4)
57 )
58 );
59 bytes = bytes.slice(4);
60 continue;
61 }
62
63 throw new Error("invalid UTF-8 encoding");
64 }
65
66 return result;
67}
Note: See TracBrowser for help on using the repository browser.