0% found this document useful (0 votes)
28 views

Targetstart Targetend Sourcestart Sourceend Target Buf

The document discusses Buffer.compare(), Buffer.copy(), and Buffer.entries() methods in Node.js. Buffer.compare() compares two Buffers and returns a number indicating their relationship. Buffer.copy() copies data from one Buffer to another, even if they overlap. Buffer.entries() returns an iterator object that yields key-value pairs for each element in the Buffer.

Uploaded by

Troll Troll
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Targetstart Targetend Sourcestart Sourceend Target Buf

The document discusses Buffer.compare(), Buffer.copy(), and Buffer.entries() methods in Node.js. Buffer.compare() compares two Buffers and returns a number indicating their relationship. Buffer.copy() copies data from one Buffer to another, even if they overlap. Buffer.entries() returns an iterator object that yields key-value pairs for each element in the Buffer.

Uploaded by

Troll Troll
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

The optional 

targetStart, targetEnd, sourceStart, and sourceEnd arguments can be used to limit the


comparison to specific ranges within target and buf respectively.
const buf1 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]);
const buf2 = Buffer.from([5, 6, 7, 8, 9, 1, 2, 3, 4]);

console.log(buf1.compare(buf2, 5, 9, 0, 4));
// Prints: 0
console.log(buf1.compare(buf2, 0, 6, 4));
// Prints: -1
console.log(buf1.compare(buf2, 5, 6, 5));
// Prints: 1

ERR_OUT_OF_RANGE is thrown if targetStart < 0, sourceStart < 0, targetEnd > target.byteLength,


or sourceEnd > source.byteLength.

buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])#


Added in: v0.1.90
target <Buffer> | <Uint8Array> A Buffer or Uint8Array to copy into.
targetStart <integer> The offset within target at which to begin writing. Default: 0.
sourceStart <integer> The offset within buf from which to begin copying. Default: 0.
sourceEnd <integer> The offset within buf at which to stop copying (not
inclusive). Default: buf.length.
Returns: <integer> The number of bytes copied.
Copies data from a region of buf to a region in target, even if the target memory region overlaps
with buf.
TypedArray#set() performs the same operation, and is available for all TypedArrays,
including Node.js Buffers, although it takes different function arguments.
// Create two `Buffer` instances.
const buf1 = Buffer.allocUnsafe(26);
const buf2 = Buffer.allocUnsafe(26).fill('!');

for (let i = 0; i < 26; i++) {


// 97 is the decimal ASCII value for 'a'.
buf1[i] = i + 97;
}
// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.
buf1.copy(buf2, 8, 16, 20);
// This is equivalent to:
// buf2.set(buf1.subarray(16, 20), 8);

console.log(buf2.toString('ascii', 0, 25));
// Prints: !!!!!!!!qrst!!!!!!!!!!!!!
// Create a `Buffer` and copy data from one region to an overlapping region
// within the same `Buffer`.

const buf = Buffer.allocUnsafe(26);

for (let i = 0; i < 26; i++) {


// 97 is the decimal ASCII value for 'a'.
buf[i] = i + 97;
}

buf.copy(buf, 0, 4, 10);

console.log(buf.toString());
// Prints: efghijghijklmnopqrstuvwxyz

buf.entries()#
Added in: v1.1.0

You might also like