From 2a00c5bca9ca6877f07cfabe2faaeef53edd5fe1 Mon Sep 17 00:00:00 2001 From: "FUJI Goro (gfx)" Date: Thu, 6 Jun 2019 22:43:22 +0900 Subject: [PATCH 1/6] doc: add Buffer#subarray() and add note about Uint8Array#slice() --- doc/api/buffer.md | 66 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index dfb18eeb5615c2..c1871e72045017 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -1896,19 +1896,7 @@ console.log(buf.readUIntBE(1, 6).toString(16)); // Throws ERR_OUT_OF_RANGE. ``` -### buf.slice([start[, end]]) - +### buf.subarray([start[, end]]) * `start` {integer} Where the new `Buffer` will start. **Default:** `0`. * `end` {integer} Where the new `Buffer` will end (not inclusive). @@ -1935,7 +1923,7 @@ for (let i = 0; i < 26; i++) { buf1[i] = i + 97; } -const buf2 = buf1.slice(0, 3); +const buf2 = buf1.subarray(0, 3); console.log(buf2.toString('ascii', 0, buf2.length)); // Prints: abc @@ -1952,17 +1940,55 @@ end of `buf` rather than the beginning. ```js const buf = Buffer.from('buffer'); -console.log(buf.slice(-6, -1).toString()); +console.log(buf.subarray(-6, -1).toString()); // Prints: buffe -// (Equivalent to buf.slice(0, 5).) +// (Equivalent to buf.subarray(0, 5).) -console.log(buf.slice(-6, -2).toString()); +console.log(buf.subarray(-6, -2).toString()); // Prints: buff -// (Equivalent to buf.slice(0, 4).) +// (Equivalent to buf.subarray(0, 4).) -console.log(buf.slice(-5, -2).toString()); +console.log(buf.subarray(-5, -2).toString()); // Prints: uff -// (Equivalent to buf.slice(1, 4).) +// (Equivalent to buf.subarray(1, 4).) +``` + +### buf.slice([start[, end]]) + + +* `start` {integer} Where the new `Buffer` will start. **Default:** `0`. +* `end` {integer} Where the new `Buffer` will end (not inclusive). + **Default:** [`buf.length`][]. +* Returns: {Buffer} + +Returns a new `Buffer` that references the same memory as the original, but +offset and cropped by the `start` and `end` indices. + +This is the same behavior as `buf.subarray()`. + +Note that this method is not compatible with the `Uint8Array#slice()`, which is a superclass of `Buffer`. If a copy of slice is needed, use `Uint8Array.prototype.slice` directly. + +```js +const buf = Buffer.from('buffer'); + +const copiedBuf = Uint8Array.prototype.slice.call(buf); +copiedBuf[0]++; +console.log(copiedBuf.toString()); +// Prints: cuffer + +console.log(buf.toString()); +// Prints: buffer ``` ### buf.swap16() From 07e7c7dd4632dab9b14011dca21cfc6ffc05595a Mon Sep 17 00:00:00 2001 From: FUJI Goro Date: Mon, 10 Jun 2019 15:17:52 +0900 Subject: [PATCH 2/6] Update doc/api/buffer.md Co-Authored-By: Anna Henningsen --- doc/api/buffer.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index c1871e72045017..632f59078d4f25 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -1977,7 +1977,9 @@ offset and cropped by the `start` and `end` indices. This is the same behavior as `buf.subarray()`. -Note that this method is not compatible with the `Uint8Array#slice()`, which is a superclass of `Buffer`. If a copy of slice is needed, use `Uint8Array.prototype.slice` directly. +Note that this method is not compatible with the `Uint8Array.prototype.slice()`, +which is a superclass of `Buffer`. If a copy of the slice is needed, use +`Uint8Array.prototype.slice` directly. ```js const buf = Buffer.from('buffer'); From 21e452fdcfc5e751fb7cf94ed7d726e1797ed51f Mon Sep 17 00:00:00 2001 From: FUJI Goro Date: Mon, 10 Jun 2019 15:18:24 +0900 Subject: [PATCH 3/6] Update doc/api/buffer.md Co-Authored-By: Anna Henningsen --- doc/api/buffer.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 632f59078d4f25..71a4d565a033a3 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -1897,6 +1897,9 @@ console.log(buf.readUIntBE(1, 6).toString(16)); ``` ### buf.subarray([start[, end]]) + * `start` {integer} Where the new `Buffer` will start. **Default:** `0`. * `end` {integer} Where the new `Buffer` will end (not inclusive). From 70271da9a98a2760ee40d2dce918bf23c2876ef4 Mon Sep 17 00:00:00 2001 From: FUJI Goro Date: Tue, 11 Jun 2019 09:36:07 +0900 Subject: [PATCH 4/6] Update doc/api/buffer.md Co-Authored-By: Rich Trott --- doc/api/buffer.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 71a4d565a033a3..02bfe0d6a5c9fa 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -1982,7 +1982,7 @@ This is the same behavior as `buf.subarray()`. Note that this method is not compatible with the `Uint8Array.prototype.slice()`, which is a superclass of `Buffer`. If a copy of the slice is needed, use -`Uint8Array.prototype.slice` directly. +`Uint8Array.prototype.slice()`. ```js const buf = Buffer.from('buffer'); From eb5efd92e7b7fc93528f26281c4f5be27b65f2e2 Mon Sep 17 00:00:00 2001 From: FUJI Goro Date: Tue, 11 Jun 2019 09:37:05 +0900 Subject: [PATCH 5/6] Update doc/api/buffer.md Co-Authored-By: Rich Trott --- doc/api/buffer.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 02bfe0d6a5c9fa..c2217b75eefef8 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -1980,7 +1980,7 @@ offset and cropped by the `start` and `end` indices. This is the same behavior as `buf.subarray()`. -Note that this method is not compatible with the `Uint8Array.prototype.slice()`, +This method is not compatible with the `Uint8Array.prototype.slice()`, which is a superclass of `Buffer`. If a copy of the slice is needed, use `Uint8Array.prototype.slice()`. From 54c4949d683411545e4ad4743fdfdf26355647d0 Mon Sep 17 00:00:00 2001 From: FUJI Goro Date: Tue, 11 Jun 2019 09:37:23 +0900 Subject: [PATCH 6/6] Update doc/api/buffer.md Co-Authored-By: Rich Trott --- doc/api/buffer.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index c2217b75eefef8..00dd366faefc98 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -1981,7 +1981,7 @@ offset and cropped by the `start` and `end` indices. This is the same behavior as `buf.subarray()`. This method is not compatible with the `Uint8Array.prototype.slice()`, -which is a superclass of `Buffer`. If a copy of the slice is needed, use +which is a superclass of `Buffer`. To copy the slice, use `Uint8Array.prototype.slice()`. ```js