Skip to content

Commit

Permalink
Buffer, refactor: use encoding.encode to build Bufer from string.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Jun 19, 2023
1 parent f7adbc0 commit 2373c4c
Show file tree
Hide file tree
Showing 2 changed files with 254 additions and 248 deletions.
22 changes: 13 additions & 9 deletions fibjs/scripts/buffer.js
Expand Up @@ -306,15 +306,19 @@ class Buffer extends Uint8Array {
super(allocPool, offset, bufferOrLength);
}
else if (typeof bufferOrLength === 'string') {
var byte_length = Buffer.byteLength(bufferOrLength, byte_offset);

let offset = getPool(byte_length);
if (offset === -1)
super(byte_length);
else
super(allocPool, offset, byte_length);

this.write(bufferOrLength, byte_offset);
var codec = byte_offset;
if (codec === undefined || codec === 'utf8' || codec === 'utf-8' || codec == 'ascii' || codec === 'binary') {
var byte_length = Buffer.byteLength(bufferOrLength, codec);

let offset = getPool(byte_length);
if (offset === -1)
super(byte_length);
else
super(allocPool, offset, byte_length);

this.write(bufferOrLength, codec);
} else
super(encoding.decode(bufferOrLength, codec).buffer);
}
else if (bufferOrLength instanceof Uint8Array || Array.isArray(bufferOrLength))
super(bufferOrLength);
Expand Down

0 comments on commit 2373c4c

Please sign in to comment.