diff options
author | Harshitha KP <harshi46@in.ibm.com> | 2020-03-06 09:10:40 +0300 |
---|---|---|
committer | Beth Griggs <Bethany.Griggs@uk.ibm.com> | 2020-04-07 18:30:11 +0300 |
commit | f11c7a61bf81a36bcf98a2fb751441cea47378cc (patch) | |
tree | 8816aec20ee199967fcb94664eafec72ded7baa2 /doc | |
parent | b40943835938d06c2d8f3ab1fcbfbafc5caaa917 (diff) |
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: https://github.com/nodejs/node/pull/32104#discussion_r388524733
PR-URL: https://github.com/nodejs/node/pull/32119
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'doc')
-rw-r--r-- | doc/api/buffer.md | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/doc/api/buffer.md b/doc/api/buffer.md index bffa5c18a9d..28f306f09ab 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -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. @@ -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])` |