@nuintun/buffer 0.3.2 → 0.5.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/README.md +63 -40
- package/cjs/{Binary.cjs → binary.cjs} +2 -2
- package/{esm/Binary.d.ts → cjs/binary.d.cts} +1 -1
- package/cjs/encoding.cjs +94 -0
- package/cjs/encoding.d.cts +25 -0
- package/cjs/enum.cjs +1 -1
- package/cjs/errors.cjs +2 -2
- package/cjs/errors.d.cts +1 -1
- package/cjs/index.cjs +32 -29
- package/cjs/index.d.cts +27 -9
- package/cjs/utils.cjs +1 -1
- package/{cjs/Binary.d.cts → esm/binary.d.ts} +1 -1
- package/esm/{Binary.js → binary.js} +2 -2
- package/esm/encoding.d.ts +25 -0
- package/esm/encoding.js +91 -0
- package/esm/enum.js +1 -1
- package/esm/errors.d.ts +1 -1
- package/esm/errors.js +2 -2
- package/esm/index.d.ts +27 -9
- package/esm/index.js +29 -26
- package/esm/utils.js +1 -1
- package/package.json +1 -1
- package/cjs/Encoding/UTF8.cjs +0 -33
- package/cjs/Encoding/UTF8.d.cts +0 -15
- package/cjs/Encoding/Unicode.cjs +0 -45
- package/cjs/Encoding/Unicode.d.cts +0 -18
- package/cjs/Encoding/index.cjs +0 -65
- package/cjs/Encoding/index.d.cts +0 -19
- package/esm/Encoding/UTF8.d.ts +0 -15
- package/esm/Encoding/UTF8.js +0 -30
- package/esm/Encoding/Unicode.d.ts +0 -18
- package/esm/Encoding/Unicode.js +0 -42
- package/esm/Encoding/index.d.ts +0 -19
- package/esm/Encoding/index.js +0 -62
package/README.md
CHANGED
|
@@ -18,11 +18,43 @@
|
|
|
18
18
|
/**
|
|
19
19
|
* @module Buffer
|
|
20
20
|
*/
|
|
21
|
+
|
|
22
|
+
export type TypedArray =
|
|
23
|
+
| Int8Array
|
|
24
|
+
| Int16Array
|
|
25
|
+
| Int32Array
|
|
26
|
+
| Uint8Array
|
|
27
|
+
| Uint16Array
|
|
28
|
+
| Uint32Array
|
|
29
|
+
| Float32Array
|
|
30
|
+
| Float64Array
|
|
31
|
+
| BigInt64Array
|
|
32
|
+
| BigUint64Array
|
|
33
|
+
| Uint8ClampedArray;
|
|
34
|
+
|
|
21
35
|
export declare enum Endian {
|
|
22
36
|
Big = 0,
|
|
23
37
|
Little = 1
|
|
24
38
|
}
|
|
25
39
|
|
|
40
|
+
export interface Options {
|
|
41
|
+
/**
|
|
42
|
+
* @property {number} [pageSize]
|
|
43
|
+
* @description 缓存页大小
|
|
44
|
+
*/
|
|
45
|
+
pageSize?: number;
|
|
46
|
+
/**
|
|
47
|
+
* @property {TextEncode} [encode]
|
|
48
|
+
* @description 文本编码函数
|
|
49
|
+
*/
|
|
50
|
+
encode?: TextEncode;
|
|
51
|
+
/**
|
|
52
|
+
* @property {TextDecode} [decode]
|
|
53
|
+
* @description 文本解码函数
|
|
54
|
+
*/
|
|
55
|
+
decode?: TextDecode;
|
|
56
|
+
}
|
|
57
|
+
|
|
26
58
|
/**
|
|
27
59
|
* @function endianness
|
|
28
60
|
* @description 获取系统默认字节序
|
|
@@ -37,17 +69,22 @@ export declare function endianness(): Endian;
|
|
|
37
69
|
export declare class Buffer {
|
|
38
70
|
/**
|
|
39
71
|
* @constructor
|
|
40
|
-
* @param {number} [length]
|
|
72
|
+
* @param {number} [length] 缓冲区初始字节大小
|
|
41
73
|
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
42
74
|
*/
|
|
43
|
-
constructor(length?: number,
|
|
75
|
+
constructor(length?: number, options?: Options);
|
|
44
76
|
/**
|
|
45
77
|
* @constructor
|
|
46
|
-
* @param {Uint8Array} bytes
|
|
78
|
+
* @param {Uint8Array} bytes 缓冲区初始字节数据
|
|
47
79
|
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
48
80
|
*/
|
|
49
|
-
constructor(bytes
|
|
50
|
-
|
|
81
|
+
constructor(bytes: TypedArray, options?: Options);
|
|
82
|
+
/**
|
|
83
|
+
* @constructor
|
|
84
|
+
* @param {ArrayBuffer} buffer 缓冲区初始缓冲数据
|
|
85
|
+
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
86
|
+
*/
|
|
87
|
+
constructor(buffer: ArrayBuffer, options?: Options);
|
|
51
88
|
/**
|
|
52
89
|
* @public
|
|
53
90
|
* @property {number} offset
|
|
@@ -55,7 +92,6 @@ export declare class Buffer {
|
|
|
55
92
|
* @description 下一次调用读写方法时将在此位置开始读写
|
|
56
93
|
*/
|
|
57
94
|
set offset(offset: number);
|
|
58
|
-
|
|
59
95
|
/**
|
|
60
96
|
* @public
|
|
61
97
|
* @property {number} offset
|
|
@@ -63,7 +99,6 @@ export declare class Buffer {
|
|
|
63
99
|
* @returns {number}
|
|
64
100
|
*/
|
|
65
101
|
get offset(): number;
|
|
66
|
-
|
|
67
102
|
/**
|
|
68
103
|
* @public
|
|
69
104
|
* @property {number} length
|
|
@@ -72,7 +107,6 @@ export declare class Buffer {
|
|
|
72
107
|
* @description 如果将长度设置为大于当前长度的值,则用零填充字节数组的右侧
|
|
73
108
|
*/
|
|
74
109
|
set length(length: number);
|
|
75
|
-
|
|
76
110
|
/**
|
|
77
111
|
* @public
|
|
78
112
|
* @property {number} length
|
|
@@ -80,23 +114,20 @@ export declare class Buffer {
|
|
|
80
114
|
* @returns {number}
|
|
81
115
|
*/
|
|
82
116
|
get length(): number;
|
|
83
|
-
|
|
84
117
|
/**
|
|
85
118
|
* @public
|
|
86
119
|
* @property {ArrayBuffer} buffer
|
|
87
|
-
* @description
|
|
120
|
+
* @description 获取全部 ArrayBuffer 原始缓冲区
|
|
88
121
|
* @returns {ArrayBuffer}
|
|
89
122
|
*/
|
|
90
123
|
get buffer(): ArrayBuffer;
|
|
91
|
-
|
|
92
124
|
/**
|
|
93
125
|
* @public
|
|
94
126
|
* @property {Uint8Array} bytes
|
|
95
|
-
* @description
|
|
127
|
+
* @description 获取已写入 Uint8Array 原始缓冲区
|
|
96
128
|
* @returns {Uint8Array}
|
|
97
129
|
*/
|
|
98
130
|
get bytes(): Uint8Array;
|
|
99
|
-
|
|
100
131
|
/**
|
|
101
132
|
* @public
|
|
102
133
|
* @method writeInt8
|
|
@@ -104,7 +135,6 @@ export declare class Buffer {
|
|
|
104
135
|
* @param {number} value 介于 -128 和 127 之间的整数
|
|
105
136
|
*/
|
|
106
137
|
writeInt8(value: number): void;
|
|
107
|
-
|
|
108
138
|
/**
|
|
109
139
|
* @public
|
|
110
140
|
* @method writeUint8
|
|
@@ -112,14 +142,12 @@ export declare class Buffer {
|
|
|
112
142
|
* @param {number} value 介于 0 和 255 之间的整数
|
|
113
143
|
*/
|
|
114
144
|
writeUint8(value: number): void;
|
|
115
|
-
|
|
116
145
|
/**
|
|
117
146
|
* @method writeBoolean
|
|
118
147
|
* @description 在缓冲区中写入布尔值,true 写 1,false写 0
|
|
119
148
|
* @param {boolean} value 布尔值
|
|
120
149
|
*/
|
|
121
150
|
writeBoolean(value: boolean): void;
|
|
122
|
-
|
|
123
151
|
/**
|
|
124
152
|
* @method writeInt16
|
|
125
153
|
* @description 在缓冲区中写入一个 16 位有符号整数
|
|
@@ -127,7 +155,6 @@ export declare class Buffer {
|
|
|
127
155
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
128
156
|
*/
|
|
129
157
|
writeInt16(value: number, littleEndian?: boolean): void;
|
|
130
|
-
|
|
131
158
|
/**
|
|
132
159
|
* @method writeUint16
|
|
133
160
|
* @description 在缓冲区中写入一个 16 位无符号整数
|
|
@@ -135,7 +162,6 @@ export declare class Buffer {
|
|
|
135
162
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
136
163
|
*/
|
|
137
164
|
writeUint16(value: number, littleEndian?: boolean): void;
|
|
138
|
-
|
|
139
165
|
/**
|
|
140
166
|
* @method writeInt32
|
|
141
167
|
* @description 在缓冲区中写入一个有符号的 32 位有符号整数
|
|
@@ -143,7 +169,6 @@ export declare class Buffer {
|
|
|
143
169
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
144
170
|
*/
|
|
145
171
|
writeInt32(value: number, littleEndian?: boolean): void;
|
|
146
|
-
|
|
147
172
|
/**
|
|
148
173
|
* @method writeUint32
|
|
149
174
|
* @description 在缓冲区中写入一个无符号的 32 位无符号整数
|
|
@@ -151,15 +176,13 @@ export declare class Buffer {
|
|
|
151
176
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
152
177
|
*/
|
|
153
178
|
writeUint32(value: number, littleEndian?: boolean): void;
|
|
154
|
-
|
|
155
179
|
/**
|
|
156
180
|
* @method writeInt64
|
|
157
|
-
* @description
|
|
181
|
+
* @description 在缓冲区中写入一个 64 位有符号整数
|
|
158
182
|
* @param {bigint} value 要写入的 32 位有符号整数
|
|
159
183
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
160
184
|
*/
|
|
161
185
|
writeInt64(value: bigint, littleEndian?: boolean): void;
|
|
162
|
-
|
|
163
186
|
/**
|
|
164
187
|
* @method writeUint64
|
|
165
188
|
* @description 在缓冲区中写入一个无符号的 64 位无符号整数
|
|
@@ -167,7 +190,6 @@ export declare class Buffer {
|
|
|
167
190
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
168
191
|
*/
|
|
169
192
|
writeUint64(value: bigint, littleEndian?: boolean): void;
|
|
170
|
-
|
|
171
193
|
/**
|
|
172
194
|
* @method writeFloat32
|
|
173
195
|
* @description 在缓冲区中写入一个 IEEE 754 单精度 32 位浮点数
|
|
@@ -175,7 +197,6 @@ export declare class Buffer {
|
|
|
175
197
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
176
198
|
*/
|
|
177
199
|
writeFloat32(value: number, littleEndian?: boolean): void;
|
|
178
|
-
|
|
179
200
|
/**
|
|
180
201
|
* @method writeFloat64
|
|
181
202
|
* @description 在缓冲区中写入一个 IEEE 754 双精度 64 位浮点数
|
|
@@ -183,7 +204,6 @@ export declare class Buffer {
|
|
|
183
204
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
184
205
|
*/
|
|
185
206
|
writeFloat64(value: number, littleEndian?: boolean): void;
|
|
186
|
-
|
|
187
207
|
/**
|
|
188
208
|
* @method write
|
|
189
209
|
* @description 将字符串用指定编码写入字节流
|
|
@@ -199,28 +219,24 @@ export declare class Buffer {
|
|
|
199
219
|
* @param {number} [end] Uint8Array 对象结束索引
|
|
200
220
|
*/
|
|
201
221
|
write(bytes: Uint8Array, start?: number, end?: number): void;
|
|
202
|
-
|
|
203
222
|
/**
|
|
204
223
|
* @method readInt8
|
|
205
224
|
* @description 从缓冲区中读取有符号的整数
|
|
206
225
|
* @returns {number} 介于 -128 和 127 之间的整数
|
|
207
226
|
*/
|
|
208
227
|
readInt8(): number;
|
|
209
|
-
|
|
210
228
|
/**
|
|
211
229
|
* @method readUint8
|
|
212
230
|
* @description 从缓冲区中读取无符号的整数
|
|
213
231
|
* @returns {number} 介于 0 和 255 之间的无符号整数
|
|
214
232
|
*/
|
|
215
233
|
readUint8(): number;
|
|
216
|
-
|
|
217
234
|
/**
|
|
218
235
|
* @method readBoolean
|
|
219
236
|
* @description 从缓冲区中读取布尔值
|
|
220
237
|
* @returns {boolean} 如果字节非零,则返回 true,否则返回 false
|
|
221
238
|
*/
|
|
222
239
|
readBoolean(): boolean;
|
|
223
|
-
|
|
224
240
|
/**
|
|
225
241
|
* @method readInt16
|
|
226
242
|
* @description 从缓冲区中读取一个 16 位有符号整数
|
|
@@ -228,7 +244,6 @@ export declare class Buffer {
|
|
|
228
244
|
* @returns {number} 介于 -32768 和 32767 之间的 16 位有符号整数
|
|
229
245
|
*/
|
|
230
246
|
readInt16(littleEndian?: boolean): number;
|
|
231
|
-
|
|
232
247
|
/**
|
|
233
248
|
* @method readUint16
|
|
234
249
|
* @description 从缓冲区中读取一个 16 位无符号整数
|
|
@@ -236,7 +251,6 @@ export declare class Buffer {
|
|
|
236
251
|
* @returns {number} 介于 0 和 65535 之间的 16 位无符号整数
|
|
237
252
|
*/
|
|
238
253
|
readUint16(littleEndian?: boolean): number;
|
|
239
|
-
|
|
240
254
|
/**
|
|
241
255
|
* @method readInt32
|
|
242
256
|
* @description 从缓冲区中读取一个 32 位有符号整数
|
|
@@ -244,7 +258,6 @@ export declare class Buffer {
|
|
|
244
258
|
* @returns {number} 介于 -2147483648 和 2147483647 之间的 32 位有符号整数
|
|
245
259
|
*/
|
|
246
260
|
readInt32(littleEndian?: boolean): number;
|
|
247
|
-
|
|
248
261
|
/**
|
|
249
262
|
* @method readUint32
|
|
250
263
|
* @description 从缓冲区中读取一个 32 位无符号整数
|
|
@@ -252,7 +265,6 @@ export declare class Buffer {
|
|
|
252
265
|
* @returns {number} 介于 0 和 4294967295 之间的 32 位无符号整数
|
|
253
266
|
*/
|
|
254
267
|
readUint32(littleEndian?: boolean): number;
|
|
255
|
-
|
|
256
268
|
/**
|
|
257
269
|
* @method readInt64
|
|
258
270
|
* @description 从缓冲区中读取一个 64 位有符号整数
|
|
@@ -260,7 +272,6 @@ export declare class Buffer {
|
|
|
260
272
|
* @returns {bigint} 介于 -9223372036854775808 和 9223372036854775807 之间的 64 位有符号整数
|
|
261
273
|
*/
|
|
262
274
|
readInt64(littleEndian?: boolean): bigint;
|
|
263
|
-
|
|
264
275
|
/**
|
|
265
276
|
* @method readUint64
|
|
266
277
|
* @description 从缓冲区中读取一个 64 位无符号整数
|
|
@@ -268,7 +279,6 @@ export declare class Buffer {
|
|
|
268
279
|
* @returns {bigint} 介于 0 和 18446744073709551615 之间的 64 位无符号整数
|
|
269
280
|
*/
|
|
270
281
|
readUint64(littleEndian?: boolean): bigint;
|
|
271
|
-
|
|
272
282
|
/**
|
|
273
283
|
* @method readFloat32
|
|
274
284
|
* @description 从缓冲区中读取一个 IEEE 754 单精度 32 位浮点数
|
|
@@ -276,7 +286,6 @@ export declare class Buffer {
|
|
|
276
286
|
* @returns {number} 单精度 32 位浮点数
|
|
277
287
|
*/
|
|
278
288
|
readFloat32(littleEndian?: boolean): number;
|
|
279
|
-
|
|
280
289
|
/**
|
|
281
290
|
* @method readFloat64
|
|
282
291
|
* @description 从缓冲区中读取一个 IEEE 754 双精度 64 位浮点数
|
|
@@ -284,7 +293,6 @@ export declare class Buffer {
|
|
|
284
293
|
* @returns {number} 双精度 64 位浮点数
|
|
285
294
|
*/
|
|
286
295
|
readFloat64(littleEndian?: boolean): number;
|
|
287
|
-
|
|
288
296
|
/**
|
|
289
297
|
* @method read
|
|
290
298
|
* @description 从缓冲区中读取指定长度的 Uint8Array 对象
|
|
@@ -300,7 +308,6 @@ export declare class Buffer {
|
|
|
300
308
|
* @returns {string} 指定编码的字符串
|
|
301
309
|
*/
|
|
302
310
|
read(length: number, encoding: string): string;
|
|
303
|
-
|
|
304
311
|
/**
|
|
305
312
|
* @public
|
|
306
313
|
* @method slice
|
|
@@ -310,7 +317,6 @@ export declare class Buffer {
|
|
|
310
317
|
* @returns {Buffer}
|
|
311
318
|
*/
|
|
312
319
|
slice(start?: number, end?: number): Buffer;
|
|
313
|
-
|
|
314
320
|
/**
|
|
315
321
|
* @public
|
|
316
322
|
* @method copyWithin
|
|
@@ -321,7 +327,24 @@ export declare class Buffer {
|
|
|
321
327
|
* @returns {this}
|
|
322
328
|
*/
|
|
323
329
|
copyWithin(target: number, start: number, end?: number): this;
|
|
324
|
-
|
|
330
|
+
/**
|
|
331
|
+
* @method entries
|
|
332
|
+
* @description 获取迭代器
|
|
333
|
+
* @returns {IterableIterator<[number, number]>}
|
|
334
|
+
*/
|
|
335
|
+
entries(): IterableIterator<[number, number]>;
|
|
336
|
+
/**
|
|
337
|
+
* @method values
|
|
338
|
+
* @description 获取迭代器
|
|
339
|
+
* @returns {IterableIterator<number>}
|
|
340
|
+
*/
|
|
341
|
+
values(): IterableIterator<number>;
|
|
342
|
+
/**
|
|
343
|
+
* @method iterator
|
|
344
|
+
* @description 迭代器
|
|
345
|
+
* @returns {IterableIterator<number>}
|
|
346
|
+
*/
|
|
347
|
+
[Symbol.iterator](): IterableIterator<number>;
|
|
325
348
|
/**
|
|
326
349
|
* @override
|
|
327
350
|
* @method toString
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package @nuintun/buffer
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.
|
|
4
|
+
* @version 0.5.0
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A buffer tool for javascript.
|
|
7
7
|
* @see https://github.com/nuintun/Buffer#readme
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
'use strict';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* @module
|
|
13
|
+
* @module binary
|
|
14
14
|
*/
|
|
15
15
|
/**
|
|
16
16
|
* @type {string[]}
|
package/cjs/encoding.cjs
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @package @nuintun/buffer
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @version 0.5.0
|
|
5
|
+
* @author nuintun <nuintun@qq.com>
|
|
6
|
+
* @description A buffer tool for javascript.
|
|
7
|
+
* @see https://github.com/nuintun/Buffer#readme
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
const errors = require('./errors.cjs');
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @module encoding
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* @function encodeSBSC
|
|
19
|
+
* @description 单字节字符编码
|
|
20
|
+
* @param {string} content 文本内容
|
|
21
|
+
* @param {number} maxCode 最大编码
|
|
22
|
+
* @returns {Uint8Array}
|
|
23
|
+
*/
|
|
24
|
+
function encodeSBSC(content, maxCode) {
|
|
25
|
+
const bytes = [];
|
|
26
|
+
for (const character of content) {
|
|
27
|
+
const code = character.codePointAt(0);
|
|
28
|
+
// If gt max code, push "?".
|
|
29
|
+
bytes.push(code == null || code > maxCode ? 63 : code);
|
|
30
|
+
}
|
|
31
|
+
return new Uint8Array(bytes);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* @function swapEndian
|
|
35
|
+
* @description 翻转字节序
|
|
36
|
+
* @param {number} value 待翻转字节序的值
|
|
37
|
+
* @returns {number}
|
|
38
|
+
*/
|
|
39
|
+
function swapEndian(value) {
|
|
40
|
+
return ((value & 0xff) << 8) | ((value >> 8) & 0xff);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* @function encodeUTF16
|
|
44
|
+
* @param {string} input 待编码字符串
|
|
45
|
+
* @param {boolean} [littleEndian] 是否使用小端字节序
|
|
46
|
+
* @returns {Uint8Array}
|
|
47
|
+
*/
|
|
48
|
+
function encodeUTF16(input, littleEndian) {
|
|
49
|
+
let offset = 0;
|
|
50
|
+
// 分配内存
|
|
51
|
+
const codes = new Uint16Array(input.length);
|
|
52
|
+
for (const char of input) {
|
|
53
|
+
const code = char.codePointAt(0);
|
|
54
|
+
if (code > 0xffff) {
|
|
55
|
+
// 代理对处理
|
|
56
|
+
const high = 0xd800 | ((code - 0x10000) >> 10);
|
|
57
|
+
const low = 0xdc00 | (code & 0x3ff);
|
|
58
|
+
codes[offset++] = littleEndian ? high : swapEndian(high);
|
|
59
|
+
codes[offset++] = littleEndian ? low : swapEndian(low);
|
|
60
|
+
} else {
|
|
61
|
+
codes[offset++] = littleEndian ? code : swapEndian(code);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return new Uint8Array(codes.buffer, 0, offset * 2);
|
|
65
|
+
}
|
|
66
|
+
function encode(content, encoding) {
|
|
67
|
+
switch (encoding.toLowerCase()) {
|
|
68
|
+
case 'ascii':
|
|
69
|
+
return encodeSBSC(content, 0x7f);
|
|
70
|
+
case 'latin1':
|
|
71
|
+
return encodeSBSC(content, 0xff);
|
|
72
|
+
case 'utf8':
|
|
73
|
+
case 'utf-8':
|
|
74
|
+
return new TextEncoder().encode(content);
|
|
75
|
+
case 'utf16le':
|
|
76
|
+
case 'utf-16le':
|
|
77
|
+
return encodeUTF16(content, true);
|
|
78
|
+
case 'utf16be':
|
|
79
|
+
case 'utf-16be':
|
|
80
|
+
return encodeUTF16(content, false);
|
|
81
|
+
default:
|
|
82
|
+
throw new Error(errors.encodingInvalid(encoding));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function decode(bytes, encoding) {
|
|
86
|
+
try {
|
|
87
|
+
return new TextDecoder(encoding).decode(bytes);
|
|
88
|
+
} catch {
|
|
89
|
+
throw new Error(errors.encodingInvalid(encoding));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
exports.decode = decode;
|
|
94
|
+
exports.encode = encode;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module encoding
|
|
3
|
+
*/
|
|
4
|
+
export interface TextEncode {
|
|
5
|
+
/**
|
|
6
|
+
* @function encode
|
|
7
|
+
* @description 用指定编码编码字符串
|
|
8
|
+
* @param {string} content 待编码文本
|
|
9
|
+
* @param {string} encoding 编码类型
|
|
10
|
+
* @returns {Uint8Array}
|
|
11
|
+
*/
|
|
12
|
+
(content: string, encoding: string): Uint8Array;
|
|
13
|
+
}
|
|
14
|
+
export interface TextDecode {
|
|
15
|
+
/**
|
|
16
|
+
* @function decode
|
|
17
|
+
* @description 用指定编码解码字节数组
|
|
18
|
+
* @param {Uint8Array} bytes 待解码字节数组
|
|
19
|
+
* @param {string} encoding 编码类型
|
|
20
|
+
* @returns {string}
|
|
21
|
+
*/
|
|
22
|
+
(bytes: Uint8Array, encoding: string): string;
|
|
23
|
+
}
|
|
24
|
+
export declare function encode(content: string, encoding: string): Uint8Array;
|
|
25
|
+
export declare function decode(bytes: Uint8Array, encoding: string): string;
|
package/cjs/enum.cjs
CHANGED
package/cjs/errors.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package @nuintun/buffer
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.
|
|
4
|
+
* @version 0.5.0
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A buffer tool for javascript.
|
|
7
7
|
* @see https://github.com/nuintun/Buffer#readme
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
/**
|
|
16
16
|
* @function encodingInvalid
|
|
17
17
|
* @description 未支持的编码格式
|
|
18
|
-
* @param encoding 编码格式
|
|
18
|
+
* @param {string} encoding 编码格式
|
|
19
19
|
*/
|
|
20
20
|
function encodingInvalid(encoding) {
|
|
21
21
|
return 'unsupported encoding ' + encoding;
|
package/cjs/errors.d.cts
CHANGED
package/cjs/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package @nuintun/buffer
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.
|
|
4
|
+
* @version 0.5.0
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A buffer tool for javascript.
|
|
7
7
|
* @see https://github.com/nuintun/Buffer#readme
|
|
@@ -9,10 +9,10 @@
|
|
|
9
9
|
|
|
10
10
|
'use strict';
|
|
11
11
|
|
|
12
|
-
const Binary = require('./Binary.cjs');
|
|
13
12
|
const errors = require('./errors.cjs');
|
|
14
|
-
const
|
|
13
|
+
const binary = require('./binary.cjs');
|
|
15
14
|
const _enum = require('./enum.cjs');
|
|
15
|
+
const encoding = require('./encoding.cjs');
|
|
16
16
|
const utils = require('./utils.cjs');
|
|
17
17
|
|
|
18
18
|
/**
|
|
@@ -49,14 +49,14 @@ class Buffer {
|
|
|
49
49
|
#offset = 0;
|
|
50
50
|
// 已使用字节长度
|
|
51
51
|
#length = 0;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
constructor(input = 0, pageSize = 4096) {
|
|
52
|
+
// 文本编码方法
|
|
53
|
+
#encode;
|
|
54
|
+
// 文本解码方法
|
|
55
|
+
#decode;
|
|
56
|
+
constructor(input = 0, options = {}) {
|
|
58
57
|
let length;
|
|
59
58
|
let bytes;
|
|
59
|
+
const { pageSize = 4096 } = options;
|
|
60
60
|
if (utils.isTypedArray(input)) {
|
|
61
61
|
length = input.byteLength;
|
|
62
62
|
bytes = utils.makeUint8Array(length, pageSize);
|
|
@@ -76,6 +76,8 @@ class Buffer {
|
|
|
76
76
|
this.#bytes = bytes;
|
|
77
77
|
this.#length = length;
|
|
78
78
|
this.#pageSize = pageSize;
|
|
79
|
+
this.#encode = options.encode ?? encoding.encode;
|
|
80
|
+
this.#decode = options.decode ?? encoding.decode;
|
|
79
81
|
this.#dataView = new DataView(bytes.buffer);
|
|
80
82
|
}
|
|
81
83
|
/**
|
|
@@ -329,7 +331,7 @@ class Buffer {
|
|
|
329
331
|
if (input instanceof Uint8Array) {
|
|
330
332
|
bytes = input.subarray(start, end);
|
|
331
333
|
} else {
|
|
332
|
-
bytes =
|
|
334
|
+
bytes = this.#encode(input, start ?? 'utf-8');
|
|
333
335
|
}
|
|
334
336
|
const { length } = bytes;
|
|
335
337
|
if (length > 0) {
|
|
@@ -483,8 +485,8 @@ class Buffer {
|
|
|
483
485
|
this.#assertRead(offset);
|
|
484
486
|
const bytes = this.#bytes.slice(this.#offset, offset);
|
|
485
487
|
this.#seek(offset);
|
|
486
|
-
if (
|
|
487
|
-
return
|
|
488
|
+
if (encoding != null) {
|
|
489
|
+
return this.#decode(bytes, encoding);
|
|
488
490
|
}
|
|
489
491
|
return bytes;
|
|
490
492
|
}
|
|
@@ -497,8 +499,11 @@ class Buffer {
|
|
|
497
499
|
* @returns {Buffer}
|
|
498
500
|
*/
|
|
499
501
|
slice(start, end) {
|
|
500
|
-
|
|
501
|
-
|
|
502
|
+
return new Buffer(this.bytes.slice(start, end), {
|
|
503
|
+
encode: this.#encode,
|
|
504
|
+
decode: this.#decode,
|
|
505
|
+
pageSize: this.#pageSize
|
|
506
|
+
});
|
|
502
507
|
}
|
|
503
508
|
/**
|
|
504
509
|
* @public
|
|
@@ -510,7 +515,7 @@ class Buffer {
|
|
|
510
515
|
* @returns {this}
|
|
511
516
|
*/
|
|
512
517
|
copyWithin(target, start, end) {
|
|
513
|
-
this
|
|
518
|
+
this.bytes.copyWithin(target, start, end);
|
|
514
519
|
return this;
|
|
515
520
|
}
|
|
516
521
|
/**
|
|
@@ -537,6 +542,14 @@ class Buffer {
|
|
|
537
542
|
yield bytes[i];
|
|
538
543
|
}
|
|
539
544
|
}
|
|
545
|
+
/**
|
|
546
|
+
* @method iterator
|
|
547
|
+
* @description 迭代器
|
|
548
|
+
* @returns {IterableIterator<number>}
|
|
549
|
+
*/
|
|
550
|
+
[Symbol.iterator]() {
|
|
551
|
+
return this.values();
|
|
552
|
+
}
|
|
540
553
|
/**
|
|
541
554
|
* @override
|
|
542
555
|
* @method toString
|
|
@@ -545,23 +558,13 @@ class Buffer {
|
|
|
545
558
|
*/
|
|
546
559
|
toString() {
|
|
547
560
|
// 二进制编码字符串
|
|
548
|
-
let binary = '';
|
|
549
|
-
// 提前获取 bytes,防止重复计算
|
|
550
|
-
const bytes = this.bytes;
|
|
561
|
+
let binary$1 = '';
|
|
551
562
|
// 获取二进制编码
|
|
552
|
-
for (const byte of
|
|
553
|
-
binary +=
|
|
563
|
+
for (const byte of this) {
|
|
564
|
+
binary$1 += binary.mapping[byte];
|
|
554
565
|
}
|
|
555
566
|
// 返回二进制编码
|
|
556
|
-
return binary;
|
|
557
|
-
}
|
|
558
|
-
/**
|
|
559
|
-
* @method iterator
|
|
560
|
-
* @description 迭代器
|
|
561
|
-
* @returns {IterableIterator<number>}
|
|
562
|
-
*/
|
|
563
|
-
[Symbol.iterator]() {
|
|
564
|
-
return this.values();
|
|
567
|
+
return binary$1;
|
|
565
568
|
}
|
|
566
569
|
}
|
|
567
570
|
|