Skip to content

Commit

Permalink
doc: clarify length param in buffer.write
Browse files Browse the repository at this point in the history
`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 <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
HarshithaKP authored and targos committed Apr 22, 2020
1 parent 39be571 commit b4ba9b8
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -1994,8 +1994,8 @@ added: v0.1.90
* `string` {string} String to write to `buf`.
* `offset` {integer} Number of bytes to skip before starting to write `string`.
**Default:** `0`.
* `length` {integer} Maximum number of bytes to write. **Default:**
`buf.length - offset`.
* `length` {integer} Maximum number of bytes to write (written bytes will not
exceed `buf.length - offset`). **Default:** `buf.length - offset`.
* `encoding` {string} The character encoding of `string`. **Default:** `'utf8'`.
* Returns: {integer} Number of bytes written.

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

console.log(`${len} bytes: ${buf.toString('utf8', 0, len)}`);
// Prints: 12 bytes: ½ + ¼ = ¾

const buffer = Buffer.alloc(10);

const length = buffer.write('abcd', 8);

console.log(`${length} bytes: ${buffer.toString('utf8', 8, 10)}`);
// Prints: 2 bytes : ab
```

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

0 comments on commit b4ba9b8

Please sign in to comment.