@@ -1896,18 +1896,9 @@ console.log(buf.readUIntBE(1, 6).toString(16));
1896
1896
// Throws ERR_OUT_OF_RANGE.
1897
1897
```
1898
1898
1899
- ### buf.slice ([ start[ , end]] )
1899
+ ### buf.subarray ([ start[ , end]] )
1900
1900
<!-- 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
1911
1902
-->
1912
1903
1913
1904
* ` start ` {integer} Where the new ` Buffer ` will start. ** Default:** ` 0 ` .
@@ -1935,7 +1926,7 @@ for (let i = 0; i < 26; i++) {
1935
1926
buf1[i] = i + 97 ;
1936
1927
}
1937
1928
1938
- const buf2 = buf1 .slice (0 , 3 );
1929
+ const buf2 = buf1 .subarray (0 , 3 );
1939
1930
1940
1931
console .log (buf2 .toString (' ascii' , 0 , buf2 .length ));
1941
1932
// Prints: abc
@@ -1952,17 +1943,57 @@ end of `buf` rather than the beginning.
1952
1943
``` js
1953
1944
const buf = Buffer .from (' buffer' );
1954
1945
1955
- console .log (buf .slice (- 6 , - 1 ).toString ());
1946
+ console .log (buf .subarray (- 6 , - 1 ).toString ());
1956
1947
// Prints: buffe
1957
- // (Equivalent to buf.slice (0, 5).)
1948
+ // (Equivalent to buf.subarray (0, 5).)
1958
1949
1959
- console .log (buf .slice (- 6 , - 2 ).toString ());
1950
+ console .log (buf .subarray (- 6 , - 2 ).toString ());
1960
1951
// Prints: buff
1961
- // (Equivalent to buf.slice (0, 4).)
1952
+ // (Equivalent to buf.subarray (0, 4).)
1962
1953
1963
- console .log (buf .slice (- 5 , - 2 ).toString ());
1954
+ console .log (buf .subarray (- 5 , - 2 ).toString ());
1964
1955
// 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
1966
1997
```
1967
1998
1968
1999
### buf.swap16()
0 commit comments