@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/cjs/index.d.cts
CHANGED
|
@@ -3,8 +3,26 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import type { TypedArray } from './utils.cjs';
|
|
5
5
|
import { Endian } from './enum.cjs';
|
|
6
|
+
import { TextDecode, TextEncode } from './encoding.cjs';
|
|
6
7
|
export { Endian };
|
|
7
8
|
export type { TypedArray };
|
|
9
|
+
export interface Options {
|
|
10
|
+
/**
|
|
11
|
+
* @property {number} [pageSize]
|
|
12
|
+
* @description 缓存页大小
|
|
13
|
+
*/
|
|
14
|
+
pageSize?: number;
|
|
15
|
+
/**
|
|
16
|
+
* @property {TextEncode} [encode]
|
|
17
|
+
* @description 文本编码函数
|
|
18
|
+
*/
|
|
19
|
+
encode?: TextEncode;
|
|
20
|
+
/**
|
|
21
|
+
* @property {TextDecode} [decode]
|
|
22
|
+
* @description 文本解码函数
|
|
23
|
+
*/
|
|
24
|
+
decode?: TextDecode;
|
|
25
|
+
}
|
|
8
26
|
/**
|
|
9
27
|
* @function endianness
|
|
10
28
|
* @description 获取系统默认字节序
|
|
@@ -22,19 +40,19 @@ export declare class Buffer {
|
|
|
22
40
|
* @param {number} [length] 缓冲区初始字节大小
|
|
23
41
|
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
24
42
|
*/
|
|
25
|
-
constructor(length?: number,
|
|
43
|
+
constructor(length?: number, options?: Options);
|
|
26
44
|
/**
|
|
27
45
|
* @constructor
|
|
28
46
|
* @param {Uint8Array} bytes 缓冲区初始字节数据
|
|
29
47
|
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
30
48
|
*/
|
|
31
|
-
constructor(bytes: TypedArray,
|
|
49
|
+
constructor(bytes: TypedArray, options?: Options);
|
|
32
50
|
/**
|
|
33
51
|
* @constructor
|
|
34
52
|
* @param {ArrayBuffer} buffer 缓冲区初始缓冲数据
|
|
35
53
|
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
36
54
|
*/
|
|
37
|
-
constructor(buffer: ArrayBuffer,
|
|
55
|
+
constructor(buffer: ArrayBuffer, options?: Options);
|
|
38
56
|
/**
|
|
39
57
|
* @public
|
|
40
58
|
* @property {number} offset
|
|
@@ -289,6 +307,12 @@ export declare class Buffer {
|
|
|
289
307
|
* @returns {IterableIterator<number>}
|
|
290
308
|
*/
|
|
291
309
|
values(): IterableIterator<number>;
|
|
310
|
+
/**
|
|
311
|
+
* @method iterator
|
|
312
|
+
* @description 迭代器
|
|
313
|
+
* @returns {IterableIterator<number>}
|
|
314
|
+
*/
|
|
315
|
+
[Symbol.iterator](): IterableIterator<number>;
|
|
292
316
|
/**
|
|
293
317
|
* @override
|
|
294
318
|
* @method toString
|
|
@@ -296,10 +320,4 @@ export declare class Buffer {
|
|
|
296
320
|
* @returns {string}
|
|
297
321
|
*/
|
|
298
322
|
toString(): string;
|
|
299
|
-
/**
|
|
300
|
-
* @method iterator
|
|
301
|
-
* @description 迭代器
|
|
302
|
-
* @returns {IterableIterator<number>}
|
|
303
|
-
*/
|
|
304
|
-
[Symbol.iterator](): IterableIterator<number>;
|
|
305
323
|
}
|
package/cjs/utils.cjs
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
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
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* @module
|
|
11
|
+
* @module binary
|
|
12
12
|
*/
|
|
13
13
|
/**
|
|
14
14
|
* @type {string[]}
|
|
@@ -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/esm/encoding.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
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
|
+
import { encodingInvalid } from './errors.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @module encoding
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @function encodeSBSC
|
|
17
|
+
* @description 单字节字符编码
|
|
18
|
+
* @param {string} content 文本内容
|
|
19
|
+
* @param {number} maxCode 最大编码
|
|
20
|
+
* @returns {Uint8Array}
|
|
21
|
+
*/
|
|
22
|
+
function encodeSBSC(content, maxCode) {
|
|
23
|
+
const bytes = [];
|
|
24
|
+
for (const character of content) {
|
|
25
|
+
const code = character.codePointAt(0);
|
|
26
|
+
// If gt max code, push "?".
|
|
27
|
+
bytes.push(code == null || code > maxCode ? 63 : code);
|
|
28
|
+
}
|
|
29
|
+
return new Uint8Array(bytes);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* @function swapEndian
|
|
33
|
+
* @description 翻转字节序
|
|
34
|
+
* @param {number} value 待翻转字节序的值
|
|
35
|
+
* @returns {number}
|
|
36
|
+
*/
|
|
37
|
+
function swapEndian(value) {
|
|
38
|
+
return ((value & 0xff) << 8) | ((value >> 8) & 0xff);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @function encodeUTF16
|
|
42
|
+
* @param {string} input 待编码字符串
|
|
43
|
+
* @param {boolean} [littleEndian] 是否使用小端字节序
|
|
44
|
+
* @returns {Uint8Array}
|
|
45
|
+
*/
|
|
46
|
+
function encodeUTF16(input, littleEndian) {
|
|
47
|
+
let offset = 0;
|
|
48
|
+
// 分配内存
|
|
49
|
+
const codes = new Uint16Array(input.length);
|
|
50
|
+
for (const char of input) {
|
|
51
|
+
const code = char.codePointAt(0);
|
|
52
|
+
if (code > 0xffff) {
|
|
53
|
+
// 代理对处理
|
|
54
|
+
const high = 0xd800 | ((code - 0x10000) >> 10);
|
|
55
|
+
const low = 0xdc00 | (code & 0x3ff);
|
|
56
|
+
codes[offset++] = littleEndian ? high : swapEndian(high);
|
|
57
|
+
codes[offset++] = littleEndian ? low : swapEndian(low);
|
|
58
|
+
} else {
|
|
59
|
+
codes[offset++] = littleEndian ? code : swapEndian(code);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return new Uint8Array(codes.buffer, 0, offset * 2);
|
|
63
|
+
}
|
|
64
|
+
function encode(content, encoding) {
|
|
65
|
+
switch (encoding.toLowerCase()) {
|
|
66
|
+
case 'ascii':
|
|
67
|
+
return encodeSBSC(content, 0x7f);
|
|
68
|
+
case 'latin1':
|
|
69
|
+
return encodeSBSC(content, 0xff);
|
|
70
|
+
case 'utf8':
|
|
71
|
+
case 'utf-8':
|
|
72
|
+
return new TextEncoder().encode(content);
|
|
73
|
+
case 'utf16le':
|
|
74
|
+
case 'utf-16le':
|
|
75
|
+
return encodeUTF16(content, true);
|
|
76
|
+
case 'utf16be':
|
|
77
|
+
case 'utf-16be':
|
|
78
|
+
return encodeUTF16(content, false);
|
|
79
|
+
default:
|
|
80
|
+
throw new Error(encodingInvalid(encoding));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function decode(bytes, encoding) {
|
|
84
|
+
try {
|
|
85
|
+
return new TextDecoder(encoding).decode(bytes);
|
|
86
|
+
} catch {
|
|
87
|
+
throw new Error(encodingInvalid(encoding));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export { decode, encode };
|
package/esm/enum.js
CHANGED
package/esm/errors.d.ts
CHANGED
package/esm/errors.js
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
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
/**
|
|
14
14
|
* @function encodingInvalid
|
|
15
15
|
* @description 未支持的编码格式
|
|
16
|
-
* @param encoding 编码格式
|
|
16
|
+
* @param {string} encoding 编码格式
|
|
17
17
|
*/
|
|
18
18
|
function encodingInvalid(encoding) {
|
|
19
19
|
return 'unsupported encoding ' + encoding;
|
package/esm/index.d.ts
CHANGED
|
@@ -3,8 +3,26 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import type { TypedArray } from './utils.js';
|
|
5
5
|
import { Endian } from './enum.js';
|
|
6
|
+
import { TextDecode, TextEncode } from './encoding.js';
|
|
6
7
|
export { Endian };
|
|
7
8
|
export type { TypedArray };
|
|
9
|
+
export interface Options {
|
|
10
|
+
/**
|
|
11
|
+
* @property {number} [pageSize]
|
|
12
|
+
* @description 缓存页大小
|
|
13
|
+
*/
|
|
14
|
+
pageSize?: number;
|
|
15
|
+
/**
|
|
16
|
+
* @property {TextEncode} [encode]
|
|
17
|
+
* @description 文本编码函数
|
|
18
|
+
*/
|
|
19
|
+
encode?: TextEncode;
|
|
20
|
+
/**
|
|
21
|
+
* @property {TextDecode} [decode]
|
|
22
|
+
* @description 文本解码函数
|
|
23
|
+
*/
|
|
24
|
+
decode?: TextDecode;
|
|
25
|
+
}
|
|
8
26
|
/**
|
|
9
27
|
* @function endianness
|
|
10
28
|
* @description 获取系统默认字节序
|
|
@@ -22,19 +40,19 @@ export declare class Buffer {
|
|
|
22
40
|
* @param {number} [length] 缓冲区初始字节大小
|
|
23
41
|
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
24
42
|
*/
|
|
25
|
-
constructor(length?: number,
|
|
43
|
+
constructor(length?: number, options?: Options);
|
|
26
44
|
/**
|
|
27
45
|
* @constructor
|
|
28
46
|
* @param {Uint8Array} bytes 缓冲区初始字节数据
|
|
29
47
|
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
30
48
|
*/
|
|
31
|
-
constructor(bytes: TypedArray,
|
|
49
|
+
constructor(bytes: TypedArray, options?: Options);
|
|
32
50
|
/**
|
|
33
51
|
* @constructor
|
|
34
52
|
* @param {ArrayBuffer} buffer 缓冲区初始缓冲数据
|
|
35
53
|
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
36
54
|
*/
|
|
37
|
-
constructor(buffer: ArrayBuffer,
|
|
55
|
+
constructor(buffer: ArrayBuffer, options?: Options);
|
|
38
56
|
/**
|
|
39
57
|
* @public
|
|
40
58
|
* @property {number} offset
|
|
@@ -289,6 +307,12 @@ export declare class Buffer {
|
|
|
289
307
|
* @returns {IterableIterator<number>}
|
|
290
308
|
*/
|
|
291
309
|
values(): IterableIterator<number>;
|
|
310
|
+
/**
|
|
311
|
+
* @method iterator
|
|
312
|
+
* @description 迭代器
|
|
313
|
+
* @returns {IterableIterator<number>}
|
|
314
|
+
*/
|
|
315
|
+
[Symbol.iterator](): IterableIterator<number>;
|
|
292
316
|
/**
|
|
293
317
|
* @override
|
|
294
318
|
* @method toString
|
|
@@ -296,10 +320,4 @@ export declare class Buffer {
|
|
|
296
320
|
* @returns {string}
|
|
297
321
|
*/
|
|
298
322
|
toString(): string;
|
|
299
|
-
/**
|
|
300
|
-
* @method iterator
|
|
301
|
-
* @description 迭代器
|
|
302
|
-
* @returns {IterableIterator<number>}
|
|
303
|
-
*/
|
|
304
|
-
[Symbol.iterator](): IterableIterator<number>;
|
|
305
323
|
}
|
package/esm/index.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
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
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import { mapping } from './Binary.js';
|
|
11
10
|
import { unknownEndianness, readOverflow, offsetInvalid, lengthInvalid, readLengthInvalid } from './errors.js';
|
|
12
|
-
import {
|
|
11
|
+
import { mapping } from './binary.js';
|
|
13
12
|
import { Endian } from './enum.js';
|
|
13
|
+
import { encode, decode } from './encoding.js';
|
|
14
14
|
import { isTypedArray, makeUint8Array, isNaturalNumber } from './utils.js';
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -47,14 +47,14 @@ class Buffer {
|
|
|
47
47
|
#offset = 0;
|
|
48
48
|
// 已使用字节长度
|
|
49
49
|
#length = 0;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
constructor(input = 0, pageSize = 4096) {
|
|
50
|
+
// 文本编码方法
|
|
51
|
+
#encode;
|
|
52
|
+
// 文本解码方法
|
|
53
|
+
#decode;
|
|
54
|
+
constructor(input = 0, options = {}) {
|
|
56
55
|
let length;
|
|
57
56
|
let bytes;
|
|
57
|
+
const { pageSize = 4096 } = options;
|
|
58
58
|
if (isTypedArray(input)) {
|
|
59
59
|
length = input.byteLength;
|
|
60
60
|
bytes = makeUint8Array(length, pageSize);
|
|
@@ -74,6 +74,8 @@ class Buffer {
|
|
|
74
74
|
this.#bytes = bytes;
|
|
75
75
|
this.#length = length;
|
|
76
76
|
this.#pageSize = pageSize;
|
|
77
|
+
this.#encode = options.encode ?? encode;
|
|
78
|
+
this.#decode = options.decode ?? decode;
|
|
77
79
|
this.#dataView = new DataView(bytes.buffer);
|
|
78
80
|
}
|
|
79
81
|
/**
|
|
@@ -327,7 +329,7 @@ class Buffer {
|
|
|
327
329
|
if (input instanceof Uint8Array) {
|
|
328
330
|
bytes = input.subarray(start, end);
|
|
329
331
|
} else {
|
|
330
|
-
bytes = encode(input, start);
|
|
332
|
+
bytes = this.#encode(input, start ?? 'utf-8');
|
|
331
333
|
}
|
|
332
334
|
const { length } = bytes;
|
|
333
335
|
if (length > 0) {
|
|
@@ -481,8 +483,8 @@ class Buffer {
|
|
|
481
483
|
this.#assertRead(offset);
|
|
482
484
|
const bytes = this.#bytes.slice(this.#offset, offset);
|
|
483
485
|
this.#seek(offset);
|
|
484
|
-
if (
|
|
485
|
-
return decode(bytes, encoding);
|
|
486
|
+
if (encoding != null) {
|
|
487
|
+
return this.#decode(bytes, encoding);
|
|
486
488
|
}
|
|
487
489
|
return bytes;
|
|
488
490
|
}
|
|
@@ -495,8 +497,11 @@ class Buffer {
|
|
|
495
497
|
* @returns {Buffer}
|
|
496
498
|
*/
|
|
497
499
|
slice(start, end) {
|
|
498
|
-
|
|
499
|
-
|
|
500
|
+
return new Buffer(this.bytes.slice(start, end), {
|
|
501
|
+
encode: this.#encode,
|
|
502
|
+
decode: this.#decode,
|
|
503
|
+
pageSize: this.#pageSize
|
|
504
|
+
});
|
|
500
505
|
}
|
|
501
506
|
/**
|
|
502
507
|
* @public
|
|
@@ -508,7 +513,7 @@ class Buffer {
|
|
|
508
513
|
* @returns {this}
|
|
509
514
|
*/
|
|
510
515
|
copyWithin(target, start, end) {
|
|
511
|
-
this
|
|
516
|
+
this.bytes.copyWithin(target, start, end);
|
|
512
517
|
return this;
|
|
513
518
|
}
|
|
514
519
|
/**
|
|
@@ -535,6 +540,14 @@ class Buffer {
|
|
|
535
540
|
yield bytes[i];
|
|
536
541
|
}
|
|
537
542
|
}
|
|
543
|
+
/**
|
|
544
|
+
* @method iterator
|
|
545
|
+
* @description 迭代器
|
|
546
|
+
* @returns {IterableIterator<number>}
|
|
547
|
+
*/
|
|
548
|
+
[Symbol.iterator]() {
|
|
549
|
+
return this.values();
|
|
550
|
+
}
|
|
538
551
|
/**
|
|
539
552
|
* @override
|
|
540
553
|
* @method toString
|
|
@@ -544,23 +557,13 @@ class Buffer {
|
|
|
544
557
|
toString() {
|
|
545
558
|
// 二进制编码字符串
|
|
546
559
|
let binary = '';
|
|
547
|
-
// 提前获取 bytes,防止重复计算
|
|
548
|
-
const bytes = this.bytes;
|
|
549
560
|
// 获取二进制编码
|
|
550
|
-
for (const byte of
|
|
561
|
+
for (const byte of this) {
|
|
551
562
|
binary += mapping[byte];
|
|
552
563
|
}
|
|
553
564
|
// 返回二进制编码
|
|
554
565
|
return binary;
|
|
555
566
|
}
|
|
556
|
-
/**
|
|
557
|
-
* @method iterator
|
|
558
|
-
* @description 迭代器
|
|
559
|
-
* @returns {IterableIterator<number>}
|
|
560
|
-
*/
|
|
561
|
-
[Symbol.iterator]() {
|
|
562
|
-
return this.values();
|
|
563
|
-
}
|
|
564
567
|
}
|
|
565
568
|
|
|
566
569
|
export { Buffer, Endian, endianness };
|
package/esm/utils.js
CHANGED
package/package.json
CHANGED
package/cjs/Encoding/UTF8.cjs
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @package @nuintun/buffer
|
|
3
|
-
* @license MIT
|
|
4
|
-
* @version 0.3.2
|
|
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
|
-
/**
|
|
13
|
-
* @module UTF8
|
|
14
|
-
*/
|
|
15
|
-
// 编码器实例
|
|
16
|
-
const encoder = new TextEncoder();
|
|
17
|
-
// 解码器实例
|
|
18
|
-
const decoder = new TextDecoder();
|
|
19
|
-
/**
|
|
20
|
-
* @function encode
|
|
21
|
-
* @param {string} input
|
|
22
|
-
* @returns {Uint8Array}
|
|
23
|
-
*/
|
|
24
|
-
const encode = encoder.encode.bind(encoder);
|
|
25
|
-
/**
|
|
26
|
-
* @function decode
|
|
27
|
-
* @param {BufferSource} input
|
|
28
|
-
* @returns {string}
|
|
29
|
-
*/
|
|
30
|
-
const decode = decoder.decode.bind(decoder);
|
|
31
|
-
|
|
32
|
-
exports.decode = decode;
|
|
33
|
-
exports.encode = encode;
|
package/cjs/Encoding/UTF8.d.cts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module UTF8
|
|
3
|
-
*/
|
|
4
|
-
/**
|
|
5
|
-
* @function encode
|
|
6
|
-
* @param {string} input
|
|
7
|
-
* @returns {Uint8Array}
|
|
8
|
-
*/
|
|
9
|
-
export declare const encode: (input?: string) => Uint8Array;
|
|
10
|
-
/**
|
|
11
|
-
* @function decode
|
|
12
|
-
* @param {BufferSource} input
|
|
13
|
-
* @returns {string}
|
|
14
|
-
*/
|
|
15
|
-
export declare const decode: (input?: BufferSource) => string;
|
package/cjs/Encoding/Unicode.cjs
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @package @nuintun/buffer
|
|
3
|
-
* @license MIT
|
|
4
|
-
* @version 0.3.2
|
|
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
|
-
/**
|
|
13
|
-
* @module Unicode
|
|
14
|
-
*/
|
|
15
|
-
/**
|
|
16
|
-
* @function encode
|
|
17
|
-
* @param {string} input
|
|
18
|
-
* @param {TypeArray} Buffer
|
|
19
|
-
* @returns {Uint8Array}
|
|
20
|
-
*/
|
|
21
|
-
function encode(input, TypeArray) {
|
|
22
|
-
const { length } = input;
|
|
23
|
-
const array = new TypeArray(length);
|
|
24
|
-
for (let i = 0; i < length; i++) {
|
|
25
|
-
array[i] = input.codePointAt(i) || 0;
|
|
26
|
-
}
|
|
27
|
-
return new Uint8Array(array.buffer);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* @function decode
|
|
31
|
-
* @param {BufferSource} input
|
|
32
|
-
* @param {TypeArray} Buffer
|
|
33
|
-
* @returns {string}
|
|
34
|
-
*/
|
|
35
|
-
function decode(input, TypeArray) {
|
|
36
|
-
let result = '';
|
|
37
|
-
const array = new TypeArray(ArrayBuffer.isView(input) ? input.buffer : input);
|
|
38
|
-
for (const code of array) {
|
|
39
|
-
result += String.fromCodePoint(code);
|
|
40
|
-
}
|
|
41
|
-
return result;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
exports.decode = decode;
|
|
45
|
-
exports.encode = encode;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module Unicode
|
|
3
|
-
*/
|
|
4
|
-
export type TypeArray = typeof Uint16Array | typeof Uint32Array;
|
|
5
|
-
/**
|
|
6
|
-
* @function encode
|
|
7
|
-
* @param {string} input
|
|
8
|
-
* @param {TypeArray} Buffer
|
|
9
|
-
* @returns {Uint8Array}
|
|
10
|
-
*/
|
|
11
|
-
export declare function encode(input: string, TypeArray: TypeArray): Uint8Array;
|
|
12
|
-
/**
|
|
13
|
-
* @function decode
|
|
14
|
-
* @param {BufferSource} input
|
|
15
|
-
* @param {TypeArray} Buffer
|
|
16
|
-
* @returns {string}
|
|
17
|
-
*/
|
|
18
|
-
export declare function decode(input: BufferSource, TypeArray: TypeArray): string;
|
package/cjs/Encoding/index.cjs
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @package @nuintun/buffer
|
|
3
|
-
* @license MIT
|
|
4
|
-
* @version 0.3.2
|
|
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 UTF8 = require('./UTF8.cjs');
|
|
13
|
-
const errors = require('../errors.cjs');
|
|
14
|
-
const Unicode = require('./Unicode.cjs');
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @module Encoding
|
|
18
|
-
*/
|
|
19
|
-
/**
|
|
20
|
-
* @function encode
|
|
21
|
-
* @description 用指定编码编码字符串
|
|
22
|
-
* @param {string} input 需要编码的字符串
|
|
23
|
-
* @param {string} [encoding] 字符串编码
|
|
24
|
-
* @returns {Uint8Array}
|
|
25
|
-
*/
|
|
26
|
-
function encode(input, encoding = 'UTF8') {
|
|
27
|
-
switch (encoding.toUpperCase()) {
|
|
28
|
-
case 'UTF8':
|
|
29
|
-
case 'UTF-8':
|
|
30
|
-
return UTF8.encode(input);
|
|
31
|
-
case 'UTF16':
|
|
32
|
-
case 'UTF-16':
|
|
33
|
-
return Unicode.encode(input, Uint16Array);
|
|
34
|
-
case 'UTF32':
|
|
35
|
-
case 'UTF-32':
|
|
36
|
-
return Unicode.encode(input, Uint32Array);
|
|
37
|
-
default:
|
|
38
|
-
throw new TypeError(errors.encodingInvalid(encoding));
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* @function decode
|
|
43
|
-
* @description 用指定编码解码字符串数据
|
|
44
|
-
* @param {BufferSource} input 需要解码的字符串数据
|
|
45
|
-
* @param {string} [encoding] 字符串编码
|
|
46
|
-
* @returns {string}
|
|
47
|
-
*/
|
|
48
|
-
function decode(input, encoding = 'UTF8') {
|
|
49
|
-
switch (encoding.toUpperCase()) {
|
|
50
|
-
case 'UTF8':
|
|
51
|
-
case 'UTF-8':
|
|
52
|
-
return UTF8.decode(input);
|
|
53
|
-
case 'UTF16':
|
|
54
|
-
case 'UTF-16':
|
|
55
|
-
return Unicode.decode(input, Uint16Array);
|
|
56
|
-
case 'UTF32':
|
|
57
|
-
case 'UTF-32':
|
|
58
|
-
return Unicode.decode(input, Uint32Array);
|
|
59
|
-
default:
|
|
60
|
-
throw new TypeError(errors.encodingInvalid(encoding));
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
exports.decode = decode;
|
|
65
|
-
exports.encode = encode;
|
package/cjs/Encoding/index.d.cts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module Encoding
|
|
3
|
-
*/
|
|
4
|
-
/**
|
|
5
|
-
* @function encode
|
|
6
|
-
* @description 用指定编码编码字符串
|
|
7
|
-
* @param {string} input 需要编码的字符串
|
|
8
|
-
* @param {string} [encoding] 字符串编码
|
|
9
|
-
* @returns {Uint8Array}
|
|
10
|
-
*/
|
|
11
|
-
export declare function encode(input: string, encoding?: string): Uint8Array;
|
|
12
|
-
/**
|
|
13
|
-
* @function decode
|
|
14
|
-
* @description 用指定编码解码字符串数据
|
|
15
|
-
* @param {BufferSource} input 需要解码的字符串数据
|
|
16
|
-
* @param {string} [encoding] 字符串编码
|
|
17
|
-
* @returns {string}
|
|
18
|
-
*/
|
|
19
|
-
export declare function decode(input: BufferSource, encoding?: string): string;
|