Skip to content

Commit 0f1f572

Browse files
HarshithaKPtargos
authored andcommitted
doc: clarify length param in buffer.write
`buffer.write` documentation has an incaccuracy w.r.t the `length` parameter: It says default number of bytes written is `buf.length - offset`. Change it to: If the buffer has sufficient space from the offset, the string is written upto `length`. If the buffer is short in space, only `buf.length - offset` bytes are written. Refs: #32104 (comment) PR-URL: #32119 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent e501ba2 commit 0f1f572

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

doc/api/buffer.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1994,8 +1994,8 @@ added: v0.1.90
19941994
* `string` {string} String to write to `buf`.
19951995
* `offset` {integer} Number of bytes to skip before starting to write `string`.
19961996
**Default:** `0`.
1997-
* `length` {integer} Maximum number of bytes to write. **Default:**
1998-
`buf.length - offset`.
1997+
* `length` {integer} Maximum number of bytes to write (written bytes will not
1998+
exceed `buf.length - offset`). **Default:** `buf.length - offset`.
19991999
* `encoding` {string} The character encoding of `string`. **Default:** `'utf8'`.
20002000
* Returns: {integer} Number of bytes written.
20012001

@@ -2011,6 +2011,13 @@ const len = buf.write('\u00bd + \u00bc = \u00be', 0);
20112011

20122012
console.log(`${len} bytes: ${buf.toString('utf8', 0, len)}`);
20132013
// Prints: 12 bytes: ½ + ¼ = ¾
2014+
2015+
const buffer = Buffer.alloc(10);
2016+
2017+
const length = buffer.write('abcd', 8);
2018+
2019+
console.log(`${length} bytes: ${buffer.toString('utf8', 8, 10)}`);
2020+
// Prints: 2 bytes : ab
20142021
```
20152022

20162023
### `buf.writeBigInt64BE(value[, offset])`

0 commit comments

Comments
 (0)