buffer: use fast API for writing one-byte strings

PR-URL: https://github.com/nodejs/node/pull/54310
PR-URL: https://github.com/nodejs/node/pull/54311
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
Robert Nagy 2024-08-15 03:01:05 +02:00 committed by GitHub
parent 75d25bc6db
commit ccf05ef751
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 125 additions and 9 deletions

View file

@ -23,13 +23,13 @@ const {
hexSlice,
ucs2Slice,
utf8Slice,
asciiWrite,
asciiWriteStatic,
base64Write,
base64urlWrite,
latin1Write,
latin1WriteStatic,
hexWrite,
ucs2Write,
utf8Write,
utf8WriteStatic,
getZeroFillToggle,
} = internalBinding('buffer');
@ -1036,13 +1036,37 @@ function addBufferPrototypeMethods(proto) {
proto.hexSlice = hexSlice;
proto.ucs2Slice = ucs2Slice;
proto.utf8Slice = utf8Slice;
proto.asciiWrite = asciiWrite;
proto.asciiWrite = function(string, offset = 0, length = this.byteLength) {
if (offset < 0 || offset > this.byteLength) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
}
if (length < 0 || length > this.byteLength - offset) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
}
return asciiWriteStatic(this, string, offset, length);
};
proto.base64Write = base64Write;
proto.base64urlWrite = base64urlWrite;
proto.latin1Write = latin1Write;
proto.latin1Write = function(string, offset = 0, length = this.byteLength) {
if (offset < 0 || offset > this.byteLength) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
}
if (length < 0 || length > this.byteLength - offset) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
}
return latin1WriteStatic(this, string, offset, length);
};
proto.hexWrite = hexWrite;
proto.ucs2Write = ucs2Write;
proto.utf8Write = utf8Write;
proto.utf8Write = function(string, offset = 0, length = this.byteLength) {
if (offset < 0 || offset > this.byteLength) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
}
if (length < 0 || length > this.byteLength - offset) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
}
return utf8WriteStatic(this, string, offset, length);
};
}
// This would better be placed in internal/worker/io.js, but that doesn't work