Skip to content

Commit dc63ef8

Browse files
committed
doc: clarify buffer api documentation
Better wording for start and end parameters, also document .length should be considered readonly. RE: nodejs#8857, nodejs#8859, nodejs#8913 PR: nodejs#8910 PR-URL: nodejs#8910 Signed-off-by: Timothy J Fontaine <[email protected]>
1 parent f5cdc4c commit dc63ef8

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

doc/api/buffer.markdown

+42-9
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,19 @@ next time `buf.write()` is called.
118118
* `start` Number, Optional, Default: 0
119119
* `end` Number, Optional, Default: `buffer.length`
120120

121-
Decodes and returns a string from buffer data encoded with `encoding`
122-
(defaults to `'utf8'`) beginning at `start` (defaults to `0`) and ending at
123-
`end` (defaults to `buffer.length`).
121+
Decodes and returns a string from buffer data encoded using the specified
122+
character set encoding. If `encoding` is `undefined` or `null`, then `encoding`
123+
defaults to `'utf8'. The `start` and `end` parameters default to `0` and
124+
`buffer.length` when `undefined`.
125+
126+
buf = new Buffer(26);
127+
for (var i = 0 ; i < 26 ; i++) {
128+
buf[i] = i + 97; // 97 is ASCII a
129+
}
130+
buf.toString('ascii'); // outputs: abcdefghijklmnopqrstuvwxyz
131+
buf.toString('ascii',0,5); // outputs: abcde
132+
buf.toString('utf8',0,5); // outputs: abcde
133+
buf.toString(undefined,0,5); // encoding defaults to 'utf8', outputs abcde
124134

125135
See `buffer.write()` example, above.
126136

@@ -228,19 +238,28 @@ buffer object. It does not change when the contents of the buffer are changed.
228238
// 1234
229239
// 1234
230240

241+
While the `length` property is not immutable, changing the value of `length`
242+
can result in undefined and inconsistent behavior. Applications that wish to
243+
modify the length of a buffer should therefore treat `length` as read-only and
244+
use `buf.slice` to create a new buffer.
245+
246+
buf = new Buffer(10);
247+
buf.write("abcdefghj", 0, "ascii");
248+
console.log(buf.length); // 10
249+
buf = buf.slice(0,5);
250+
console.log(buf.length); // 5
251+
231252
### buf.copy(targetBuffer, [targetStart], [sourceStart], [sourceEnd])
232253

233254
* `targetBuffer` Buffer object - Buffer to copy into
234255
* `targetStart` Number, Optional, Default: 0
235256
* `sourceStart` Number, Optional, Default: 0
236257
* `sourceEnd` Number, Optional, Default: `buffer.length`
237258

238-
Does copy between buffers. The source and target regions can be overlapped.
239-
`targetStart` and `sourceStart` default to `0`.
240-
`sourceEnd` defaults to `buffer.length`.
241-
242-
All values passed that are `undefined`/`NaN` or are out of bounds are set equal
243-
to their respective defaults.
259+
Copies data from a region of this buffer to a region in the target buffer even
260+
if the target memory region overlaps with the source. If `undefined` the
261+
`targetStart` and `sourceStart` parameters default to `0` while `sourceEnd`
262+
defaults to `buffer.length`.
244263

245264
Example: build two Buffers, then copy `buf1` from byte 16 through byte 19
246265
into `buf2`, starting at the 8th byte in `buf2`.
@@ -258,6 +277,20 @@ into `buf2`, starting at the 8th byte in `buf2`.
258277

259278
// !!!!!!!!qrst!!!!!!!!!!!!!
260279

280+
Example: Build a single buffer, then copy data from one region to an overlapping
281+
region in the same buffer
282+
283+
buf = new Buffer(26);
284+
285+
for (var i = 0 ; i < 26 ; i++) {
286+
buf[i] = i + 97; // 97 is ASCII a
287+
}
288+
289+
buf.copy(buf, 0, 4, 10);
290+
console.log(buf.toString());
291+
292+
// efghijghijklmnopqrstuvwxyz
293+
261294

262295
### buf.slice([start], [end])
263296

0 commit comments

Comments
 (0)