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