Skip to content

Commit 9e18923

Browse files
committed
Improve buffer docs to toString and copy
Improve documentation for buffer.toString and buffer.copy per issues nodejs#8859 and nodejs#8857.
1 parent cfcb1de commit 9e18923

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

doc/api/buffer.markdown

+30-11
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,20 @@ The method will not write partial characters.
187187
* `start` Number, Optional, Default: 0
188188
* `end` Number, Optional, Default: `buffer.length`
189189

190-
Decodes and returns a string from buffer data encoded with `encoding`
191-
(defaults to `'utf8'`) beginning at `start` (defaults to `0`) and ending at
192-
`end` (defaults to `buffer.length`).
193-
194-
See `buffer.write()` example, above.
190+
Decodes and returns a string from buffer data encoded using the specified
191+
character set encoding. If `encoding` is `undefined` or
192+
`null`, then `encoding` defaults to `'utf8'`. The `start` and `end`
193+
parameters default to `0` and `buffer.length` when not specified or
194+
passed as `undefined`/`NaN`.
195195

196+
buf = new Buffer(26);
197+
for (var i = 0 ; i < 26 ; i++) {
198+
buf[i] = i + 97; // 97 is ASCII a
199+
}
200+
buf.toString('ascii'); // outputs: abcdefghijklmnopqrstuvwxyz
201+
buf.toString('ascii',0,5); // outputs: abcde
202+
buf.toString('utf8',0,5); // outputs: abcde
203+
buf.toString(undefined,0,5); // encoding defaults to 'utf8', outputs abcde
196204

197205
### buf.toJSON()
198206

@@ -259,12 +267,10 @@ the same as the `otherBuffer` in sort order.
259267
* `sourceStart` Number, Optional, Default: 0
260268
* `sourceEnd` Number, Optional, Default: `buffer.length`
261269

262-
Does copy between buffers. The source and target regions can be overlapped.
263-
`targetStart` and `sourceStart` default to `0`.
264-
`sourceEnd` defaults to `buffer.length`.
265-
266-
All values passed that are `undefined`/`NaN` or are out of bounds are set equal
267-
to their respective defaults.
270+
Copies data from a region of this buffer to a region in the target buffer even
271+
if the target memory region overlaps with the source. If not specified or passed
272+
as `undefined`/`NaN`, the `targetStart` and `sourceStart` parameters default
273+
to `0` and `sourceEnd` defaults to `buffer.length`.
268274

269275
Example: build two Buffers, then copy `buf1` from byte 16 through byte 19
270276
into `buf2`, starting at the 8th byte in `buf2`.
@@ -282,6 +288,19 @@ into `buf2`, starting at the 8th byte in `buf2`.
282288

283289
// !!!!!!!!qrst!!!!!!!!!!!!!
284290

291+
Example: Build a single buffer, then copy data from one region to an overlapping
292+
region in the same buffer
293+
294+
buf = new Buffer(26);
295+
296+
for (var i = 0 ; i < 26 ; i++) {
297+
buf[i] = i + 97; // 97 is ASCII a
298+
}
299+
300+
buf.copy(buf, 0, 4, 10);
301+
console.log(buf.toString('ascii'));
302+
303+
// efghijghijklmnopqrstuvwxyz
285304

286305
### buf.slice([start], [end])
287306

0 commit comments

Comments
 (0)