@@ -187,12 +187,20 @@ The method will not write partial characters.
187
187
* ` start ` Number, Optional, Default: 0
188
188
* ` end ` Number, Optional, Default: ` buffer.length `
189
189
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 ` .
195
195
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
196
204
197
205
### buf.toJSON()
198
206
@@ -259,12 +267,10 @@ the same as the `otherBuffer` in sort order.
259
267
* ` sourceStart ` Number, Optional, Default: 0
260
268
* ` sourceEnd ` Number, Optional, Default: ` buffer.length `
261
269
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 ` .
268
274
269
275
Example: build two Buffers, then copy ` buf1 ` from byte 16 through byte 19
270
276
into ` buf2 ` , starting at the 8th byte in ` buf2 ` .
@@ -282,6 +288,19 @@ into `buf2`, starting at the 8th byte in `buf2`.
282
288
283
289
// !!!!!!!!qrst!!!!!!!!!!!!!
284
290
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
285
304
286
305
### buf.slice([ start] , [ end] )
287
306
0 commit comments