Skip to content

Commit 8c27755

Browse files
gfxBridgeAR
authored andcommitted
doc: add Buffer#subarray() and add note about Uint8Array#slice()
PR-URL: #28101 Refs: #28087 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 4d6262f commit 8c27755

File tree

1 file changed

+49
-18
lines changed

1 file changed

+49
-18
lines changed

doc/api/buffer.md

+49-18
Original file line numberDiff line numberDiff line change
@@ -1896,18 +1896,9 @@ console.log(buf.readUIntBE(1, 6).toString(16));
18961896
// Throws ERR_OUT_OF_RANGE.
18971897
```
18981898

1899-
### buf.slice([start[, end]])
1899+
### buf.subarray([start[, end]])
19001900
<!-- YAML
1901-
added: v0.3.0
1902-
changes:
1903-
- version: v7.1.0, v6.9.2
1904-
pr-url: https://github.com/nodejs/node/pull/9341
1905-
description: Coercing the offsets to integers now handles values outside
1906-
the 32-bit integer range properly.
1907-
- version: v7.0.0
1908-
pr-url: https://github.com/nodejs/node/pull/9101
1909-
description: All offsets are now coerced to integers before doing any
1910-
calculations with them.
1901+
added: v3.0.0
19111902
-->
19121903

19131904
* `start` {integer} Where the new `Buffer` will start. **Default:** `0`.
@@ -1935,7 +1926,7 @@ for (let i = 0; i < 26; i++) {
19351926
buf1[i] = i + 97;
19361927
}
19371928

1938-
const buf2 = buf1.slice(0, 3);
1929+
const buf2 = buf1.subarray(0, 3);
19391930

19401931
console.log(buf2.toString('ascii', 0, buf2.length));
19411932
// Prints: abc
@@ -1952,17 +1943,57 @@ end of `buf` rather than the beginning.
19521943
```js
19531944
const buf = Buffer.from('buffer');
19541945

1955-
console.log(buf.slice(-6, -1).toString());
1946+
console.log(buf.subarray(-6, -1).toString());
19561947
// Prints: buffe
1957-
// (Equivalent to buf.slice(0, 5).)
1948+
// (Equivalent to buf.subarray(0, 5).)
19581949

1959-
console.log(buf.slice(-6, -2).toString());
1950+
console.log(buf.subarray(-6, -2).toString());
19601951
// Prints: buff
1961-
// (Equivalent to buf.slice(0, 4).)
1952+
// (Equivalent to buf.subarray(0, 4).)
19621953

1963-
console.log(buf.slice(-5, -2).toString());
1954+
console.log(buf.subarray(-5, -2).toString());
19641955
// Prints: uff
1965-
// (Equivalent to buf.slice(1, 4).)
1956+
// (Equivalent to buf.subarray(1, 4).)
1957+
```
1958+
1959+
### buf.slice([start[, end]])
1960+
<!-- YAML
1961+
added: v0.3.0
1962+
changes:
1963+
- version: v7.1.0, v6.9.2
1964+
pr-url: https://github.com/nodejs/node/pull/9341
1965+
description: Coercing the offsets to integers now handles values outside
1966+
the 32-bit integer range properly.
1967+
- version: v7.0.0
1968+
pr-url: https://github.com/nodejs/node/pull/9101
1969+
description: All offsets are now coerced to integers before doing any
1970+
calculations with them.
1971+
-->
1972+
1973+
* `start` {integer} Where the new `Buffer` will start. **Default:** `0`.
1974+
* `end` {integer} Where the new `Buffer` will end (not inclusive).
1975+
**Default:** [`buf.length`][].
1976+
* Returns: {Buffer}
1977+
1978+
Returns a new `Buffer` that references the same memory as the original, but
1979+
offset and cropped by the `start` and `end` indices.
1980+
1981+
This is the same behavior as `buf.subarray()`.
1982+
1983+
This method is not compatible with the `Uint8Array.prototype.slice()`,
1984+
which is a superclass of `Buffer`. To copy the slice, use
1985+
`Uint8Array.prototype.slice()`.
1986+
1987+
```js
1988+
const buf = Buffer.from('buffer');
1989+
1990+
const copiedBuf = Uint8Array.prototype.slice.call(buf);
1991+
copiedBuf[0]++;
1992+
console.log(copiedBuf.toString());
1993+
// Prints: cuffer
1994+
1995+
console.log(buf.toString());
1996+
// Prints: buffer
19661997
```
19671998

19681999
### buf.swap16()

0 commit comments

Comments
 (0)