1 | /* |
||
2 | * Copyright (c) 2018 Rafael da Silva Rocha. |
||
3 | * |
||
4 | * Permission is hereby granted, free of charge, to any person obtaining |
||
5 | * a copy of this software and associated documentation files (the |
||
6 | * "Software"), to deal in the Software without restriction, including |
||
7 | * without limitation the rights to use, copy, modify, merge, publish, |
||
8 | * distribute, sublicense, and/or sell copies of the Software, and to |
||
9 | * permit persons to whom the Software is furnished to do so, subject to |
||
10 | * the following conditions: |
||
11 | * |
||
12 | * The above copyright notice and this permission notice shall be |
||
13 | * included in all copies or substantial portions of the Software. |
||
14 | * |
||
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
||
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
||
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
||
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
||
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
||
22 | * |
||
23 | */ |
||
24 | |||
25 | /** |
||
26 | * @fileoverview Functions to serialize and deserialize UTF-8 strings. |
||
27 | * @see https://github.com/rochars/utf8-buffer |
||
28 | * @see https://encoding.spec.whatwg.org/#the-encoding |
||
29 | * @see https://encoding.spec.whatwg.org/#utf-8-encoder |
||
30 | */ |
||
31 | |||
32 | /** @module utf8-buffer */ |
||
33 | |||
34 | /** |
||
35 | * Read a string of UTF-8 characters from a byte buffer. |
||
36 | * Invalid characters are replaced with 'REPLACEMENT CHARACTER' (U+FFFD). |
||
37 | * @see https://encoding.spec.whatwg.org/#the-encoding |
||
38 | * @see https://stackoverflow.com/a/34926911 |
||
39 | * @param {!Uint8Array|!Array<number>} buffer A byte buffer. |
||
40 | * @param {number=} start The buffer index to start reading. |
||
41 | * @param {?number=} end The buffer index to stop reading. |
||
42 | * Assumes the buffer length if undefined. |
||
43 | * @return {string} |
||
44 | */ |
||
45 | export function unpack(buffer, start=0, end=buffer.length) { |
||
46 | /** @type {string} */ |
||
47 | let str = ''; |
||
48 | for(let index = start; index < end;) { |
||
49 | /** @type {number} */ |
||
50 | let lowerBoundary = 0x80; |
||
51 | /** @type {number} */ |
||
52 | let upperBoundary = 0xBF; |
||
53 | /** @type {boolean} */ |
||
54 | let replace = false; |
||
55 | /** @type {number} */ |
||
56 | let charCode = buffer[index++]; |
||
57 | if (charCode >= 0x00 && charCode <= 0x7F) { |
||
58 | str += String.fromCharCode(charCode); |
||
59 | } else { |
||
60 | /** @type {number} */ |
||
61 | let count = 0; |
||
62 | if (charCode >= 0xC2 && charCode <= 0xDF) { |
||
63 | count = 1; |
||
64 | } else if (charCode >= 0xE0 && charCode <= 0xEF ) { |
||
65 | count = 2; |
||
66 | if (buffer[index] === 0xE0) { |
||
67 | lowerBoundary = 0xA0; |
||
68 | } |
||
69 | if (buffer[index] === 0xED) { |
||
70 | upperBoundary = 0x9F; |
||
71 | } |
||
72 | } else if (charCode >= 0xF0 && charCode <= 0xF4 ) { |
||
73 | count = 3; |
||
74 | if (buffer[index] === 0xF0) { |
||
75 | lowerBoundary = 0x90; |
||
76 | } |
||
77 | if (buffer[index] === 0xF4) { |
||
78 | upperBoundary = 0x8F; |
||
79 | } |
||
80 | } else { |
||
81 | replace = true; |
||
82 | } |
||
83 | charCode = charCode & (1 << (8 - count - 1)) - 1; |
||
84 | for (let i = 0; i < count; i++) { |
||
85 | if (buffer[index] < lowerBoundary || buffer[index] > upperBoundary) { |
||
86 | replace = true; |
||
87 | } |
||
88 | charCode = (charCode << 6) | (buffer[index] & 0x3f); |
||
89 | index++; |
||
90 | } |
||
91 | if (replace) { |
||
92 | str += String.fromCharCode(0xFFFD); |
||
93 | } |
||
94 | else if (charCode <= 0xffff) { |
||
95 | str += String.fromCharCode(charCode); |
||
96 | } else { |
||
97 | charCode -= 0x10000; |
||
98 | str += String.fromCharCode( |
||
99 | ((charCode >> 10) & 0x3ff) + 0xd800, |
||
100 | (charCode & 0x3ff) + 0xdc00); |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | return str; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Write a string of UTF-8 characters to a byte buffer. |
||
109 | * @see https://encoding.spec.whatwg.org/#utf-8-encoder |
||
110 | * @param {string} str The string to pack. |
||
111 | * @param {!Uint8Array|!Array<number>} buffer The buffer to pack the string to. |
||
112 | * @param {number=} index The buffer index to start writing. |
||
113 | * @return {number} The next index to write in the buffer. |
||
114 | */ |
||
115 | export function pack(str, buffer, index=0) { |
||
116 | for (let i = 0, len = str.length; i < len; i++) { |
||
117 | /** @type {number} */ |
||
118 | let codePoint = str.codePointAt(i); |
||
119 | if (codePoint < 128) { |
||
120 | buffer[index] = codePoint; |
||
121 | index++; |
||
122 | } else { |
||
123 | /** @type {number} */ |
||
124 | let count = 0; |
||
125 | /** @type {number} */ |
||
126 | let offset = 0; |
||
127 | if (codePoint <= 0x07FF) { |
||
128 | count = 1; |
||
129 | offset = 0xC0; |
||
130 | } else if(codePoint <= 0xFFFF) { |
||
131 | count = 2; |
||
132 | offset = 0xE0; |
||
133 | } else if(codePoint <= 0x10FFFF) { |
||
134 | count = 3; |
||
135 | offset = 0xF0; |
||
136 | i++; |
||
0 ignored issues
–
show
Complexity
Coding Style
introduced
by
![]() |
|||
137 | } |
||
138 | buffer[index] = (codePoint >> (6 * count)) + offset; |
||
139 | index++; |
||
140 | while (count > 0) { |
||
141 | buffer[index] = 0x80 | (codePoint >> (6 * (count - 1)) & 0x3F); |
||
142 | index++; |
||
143 | count--; |
||
144 | } |
||
145 | } |
||
146 | } |
||
147 | return index; |
||
148 | } |
||
149 |