Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
taichunmin committed May 5, 2024
1 parent cc213d6 commit 8c4a6b9
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -679,13 +688,19 @@ 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)
for (let i = 0; i < buf.length; i++) buf[i] = HEX_CHAR.get(hex[i * 2]) << 4 | HEX_CHAR.get(hex[i * 2 + 1])
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}`)
Expand All @@ -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')
Expand Down Expand Up @@ -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)
Expand All @@ -780,6 +807,9 @@ export class Buffer extends Uint8Array {
}
}

/**
* @group Static Methods
*/
static packCalcSize (format: string): number
static packCalcSize (items: PackFormat['items']): number

Expand All @@ -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
Expand All @@ -822,6 +853,9 @@ export class Buffer extends Uint8Array {
return buf
}

/**
* @group Static Methods
*/
static unpack <T extends any[]> (buf: Buffer, format: string): T {
const { littleEndian, items } = Buffer.packParseFormat(format)
const lenRequired = Buffer.packCalcSize(items)
Expand Down

0 comments on commit 8c4a6b9

Please sign in to comment.