From 8c4a6b9021445d8a4387867a5e8819421b79ecb0 Mon Sep 17 00:00:00 2001 From: taichunmin Date: Sun, 5 May 2024 19:27:54 +0800 Subject: [PATCH] save --- lib/buffer.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lib/buffer.ts b/lib/buffer.ts index 13c58a2..9014ce5 100644 --- a/lib/buffer.ts +++ b/lib/buffer.ts @@ -635,12 +635,18 @@ export class Buffer extends Uint8Array { throw new TypeError(`Invalid type of value: ${typeof val}`) } + /** + * @group Static Methods + */ static fromAsciiString (ascii: string): Buffer { const buf = new Buffer(ascii.length) for (let i = 0; i < ascii.length; i++) buf[i] = ascii.charCodeAt(i) & 0xFF return buf } + /** + * @group Static Methods + */ static fromBase64String (base64: string): Buffer { base64 = base64.replace(/[^A-Za-z0-9/_+-]/g, '') const tmp1 = base64.length @@ -660,6 +666,9 @@ export class Buffer extends Uint8Array { return tmp1 < base64.length ? buf.subarray(0, tmp1 - base64.length) : buf } + /** + * @group Static Methods + */ static fromBase64urlString (base64: string): Buffer { base64 = base64.replace(/[^A-Za-z0-9/_+-]/g, '') const tmp1 = base64.length @@ -679,6 +688,9 @@ export class Buffer extends Uint8Array { return tmp1 < base64.length ? buf.subarray(0, tmp1 - base64.length) : buf } + /** + * @group Static Methods + */ static fromHexString (hex: string): Buffer { hex = hex.replace(/[^0-9A-Fa-f]/g, '') const buf = new Buffer(hex.length >>> 1) @@ -686,6 +698,9 @@ export class Buffer extends Uint8Array { return buf } + /** + * @group Static Methods + */ static fromString (str: string, encoding: KeyOfEncoding = 'utf8'): Buffer { encoding = _.toLower(encoding) as KeyOfEncoding if (!Buffer.isEncoding(encoding)) throw new TypeError(`Unknown encoding: ${encoding as string}`) @@ -706,16 +721,25 @@ export class Buffer extends Uint8Array { return fromStringFns[encoding](str) } + /** + * @group Static Methods + */ static fromUcs2String (ucs2: string): Buffer { const buf = new Buffer(ucs2.length * 2) for (let i = 0; i < ucs2.length; i++) buf.writeUInt16LE(ucs2.charCodeAt(i), i * 2) return buf } + /** + * @group Static Methods + */ static fromUtf8String (utf8: string): Buffer { return Buffer.fromView(new TextEncoder().encode(utf8)) } + /** + * @group Static Methods + */ static fromView (view: ArrayBufferView, offset?: number, length?: number): Buffer static fromView (view: any, offset: number = 0, length?: number): Buffer { if (!ArrayBuffer.isView(view)) throw new TypeError('invalid view') @@ -765,6 +789,9 @@ export class Buffer extends Uint8Array { return encoding in Encoding } + /** + * @group Static Methods + */ static packParseFormat (format: string): PackFormat { if (!_.isString(format)) throw new TypeError('Invalid type of format') const matched = /^([@=<>!]?)((?:\d*[xcbB?hHiIlLqQefdsp])+)$/.exec(format) @@ -780,6 +807,9 @@ export class Buffer extends Uint8Array { } } + /** + * @group Static Methods + */ static packCalcSize (format: string): number static packCalcSize (items: PackFormat['items']): number @@ -796,6 +826,7 @@ export class Buffer extends Uint8Array { /** * Return a bytes object containing the values packed according to the format string format. The arguments must match the values required by the format exactly. + * @group Static Methods */ static pack (format: string, ...vals: any[]): Buffer static pack (buf: Buffer, format: string, ...vals: any[]): Buffer @@ -822,6 +853,9 @@ export class Buffer extends Uint8Array { return buf } + /** + * @group Static Methods + */ static unpack (buf: Buffer, format: string): T { const { littleEndian, items } = Buffer.packParseFormat(format) const lenRequired = Buffer.packCalcSize(items)