Skip to content

Commit acacf85

Browse files
Trotttrivikr
authored andcommitted
doc: remove example labels from buffer.md
Remove instances of `Example:` that introduce code that is self-evidently example code. Move descriptive text about examples into comments in the code. Wrap adjacent text to 80 characters. PR-URL: #19582 Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent e821071 commit acacf85

File tree

1 file changed

+22
-73
lines changed

1 file changed

+22
-73
lines changed

doc/api/buffer.md

+22-73
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ impact* on performance. Use of the `--zero-fill-buffers` option is recommended
120120
only when necessary to enforce that newly allocated `Buffer` instances cannot
121121
contain potentially sensitive data.
122122

123-
Example:
124-
125123
```txt
126124
$ node --zero-fill-buffers
127125
> Buffer.allocUnsafe(5);
@@ -157,8 +155,6 @@ such as UTF-8, UCS2, Base64, or even Hex-encoded data. It is possible to
157155
convert back and forth between `Buffer` instances and ordinary JavaScript strings
158156
by using an explicit character encoding.
159157

160-
Example:
161-
162158
```js
163159
const buf = Buffer.from('hello world', 'ascii');
164160

@@ -229,8 +225,6 @@ elements, and not as a byte array of the target type. That is,
229225
It is possible to create a new `Buffer` that shares the same allocated memory as
230226
a [`TypedArray`] instance by using the TypeArray object's `.buffer` property.
231227

232-
Example:
233-
234228
```js
235229
const arr = new Uint16Array(2);
236230

@@ -259,8 +253,6 @@ Note that when creating a `Buffer` using a [`TypedArray`]'s `.buffer`, it is
259253
possible to use only a portion of the underlying [`ArrayBuffer`] by passing in
260254
`byteOffset` and `length` parameters.
261255

262-
Example:
263-
264256
```js
265257
const arr = new Uint16Array(20);
266258
const buf = Buffer.from(arr.buffer, 0, 16);
@@ -289,8 +281,6 @@ function:
289281
`Buffer` instances can be iterated over using the [`ECMAScript 2015`] (ES6) `for..of`
290282
syntax.
291283

292-
Example:
293-
294284
```js
295285
const buf = Buffer.from([1, 2, 3]);
296286

@@ -329,8 +319,6 @@ changes:
329319

330320
Allocates a new `Buffer` using an `array` of octets.
331321

332-
Example:
333-
334322
```js
335323
// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'
336324
const buf = new Buffer([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
@@ -370,8 +358,6 @@ share the same allocated memory as the [`TypedArray`].
370358
The optional `byteOffset` and `length` arguments specify a memory range within
371359
the `arrayBuffer` that will be shared by the `Buffer`.
372360

373-
Example:
374-
375361
```js
376362
const arr = new Uint16Array(2);
377363

@@ -409,8 +395,6 @@ changes:
409395

410396
Copies the passed `buffer` data onto a new `Buffer` instance.
411397

412-
Example:
413-
414398
```js
415399
const buf1 = new Buffer('buffer');
416400
const buf2 = new Buffer(buf1);
@@ -453,8 +437,6 @@ created in this way is *not initialized*. The contents of a newly created
453437
[`Buffer.alloc(size)`][`Buffer.alloc()`] instead to initialize a `Buffer`
454438
to zeroes.
455439

456-
Example:
457-
458440
```js
459441
const buf = new Buffer(10);
460442

@@ -522,8 +504,6 @@ changes:
522504
Allocates a new `Buffer` of `size` bytes. If `fill` is `undefined`, the
523505
`Buffer` will be *zero-filled*.
524506

525-
Example:
526-
527507
```js
528508
const buf = Buffer.alloc(5);
529509

@@ -538,8 +518,6 @@ thrown. A zero-length `Buffer` will be created if `size` is 0.
538518
If `fill` is specified, the allocated `Buffer` will be initialized by calling
539519
[`buf.fill(fill)`][`buf.fill()`].
540520

541-
Example:
542-
543521
```js
544522
const buf = Buffer.alloc(5, 'a');
545523

@@ -550,8 +528,6 @@ console.log(buf);
550528
If both `fill` and `encoding` are specified, the allocated `Buffer` will be
551529
initialized by calling [`buf.fill(fill, encoding)`][`buf.fill()`].
552530

553-
Example:
554-
555531
```js
556532
const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');
557533

@@ -585,8 +561,6 @@ initialized*. The contents of the newly created `Buffer` are unknown and
585561
*may contain sensitive data*. Use [`Buffer.alloc()`] instead to initialize
586562
`Buffer` instances to zeroes.
587563

588-
Example:
589-
590564
```js
591565
const buf = Buffer.allocUnsafe(10);
592566

@@ -643,8 +617,6 @@ memory from a pool for an indeterminate amount of time, it may be appropriate
643617
to create an un-pooled `Buffer` instance using `Buffer.allocUnsafeSlow()` then
644618
copy out the relevant bits.
645619

646-
Example:
647-
648620
```js
649621
// Need to keep around a few small chunks of memory
650622
const store = [];
@@ -694,8 +666,6 @@ For `'base64'` and `'hex'`, this function assumes valid input. For strings that
694666
contain non-Base64/Hex-encoded data (e.g. whitespace), the return value might be
695667
greater than the length of a `Buffer` created from the string.
696668

697-
Example:
698-
699669
```js
700670
const str = '\u00bd + \u00bc = \u00be';
701671

@@ -724,8 +694,6 @@ Compares `buf1` to `buf2` typically for the purpose of sorting arrays of
724694
`Buffer` instances. This is equivalent to calling
725695
[`buf1.compare(buf2)`][`buf.compare()`].
726696

727-
Example:
728-
729697
```js
730698
const buf1 = Buffer.from('1234');
731699
const buf2 = Buffer.from('0123');
@@ -765,9 +733,9 @@ If `totalLength` is provided, it is coerced to an unsigned integer. If the
765733
combined length of the `Buffer`s in `list` exceeds `totalLength`, the result is
766734
truncated to `totalLength`.
767735

768-
Example: Create a single `Buffer` from a list of three `Buffer` instances
769-
770736
```js
737+
// Create a single `Buffer` from a list of three `Buffer` instances.
738+
771739
const buf1 = Buffer.alloc(10);
772740
const buf2 = Buffer.alloc(14);
773741
const buf3 = Buffer.alloc(18);
@@ -793,8 +761,6 @@ added: v5.10.0
793761

794762
Allocates a new `Buffer` using an `array` of octets.
795763

796-
Example:
797-
798764
```js
799765
// Creates a new Buffer containing UTF-8 bytes of the string 'buffer'
800766
const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
@@ -818,8 +784,6 @@ memory. For example, when passed a reference to the `.buffer` property of a
818784
[`TypedArray`] instance, the newly created `Buffer` will share the same
819785
allocated memory as the [`TypedArray`].
820786

821-
Example:
822-
823787
```js
824788
const arr = new Uint16Array(2);
825789

@@ -842,8 +806,6 @@ console.log(buf);
842806
The optional `byteOffset` and `length` arguments specify a memory range within
843807
the `arrayBuffer` that will be shared by the `Buffer`.
844808

845-
Example:
846-
847809
```js
848810
const ab = new ArrayBuffer(10);
849811
const buf = Buffer.from(ab, 0, 2);
@@ -864,8 +826,6 @@ added: v5.10.0
864826

865827
Copies the passed `buffer` data onto a new `Buffer` instance.
866828

867-
Example:
868-
869829
```js
870830
const buf1 = Buffer.from('buffer');
871831
const buf2 = Buffer.from(buf1);
@@ -984,9 +944,9 @@ This operator is inherited from `Uint8Array`, so its behavior on out-of-bounds
984944
access is the same as `UInt8Array` - that is, getting returns `undefined` and
985945
setting does nothing.
986946

987-
Example: Copy an ASCII string into a `Buffer`, one byte at a time
988-
989947
```js
948+
// Copy an ASCII string into a `Buffer` one byte at a time.
949+
990950
const str = 'Node.js';
991951
const buf = Buffer.allocUnsafe(str.length);
992952

@@ -1098,10 +1058,8 @@ added: v0.1.90
10981058
Copies data from a region of `buf` to a region in `target` even if the `target`
10991059
memory region overlaps with `buf`.
11001060

1101-
Example: Create two `Buffer` instances, `buf1` and `buf2`, and copy `buf1` from
1102-
byte 16 through byte 19 into `buf2`, starting at the 8th byte in `buf2`
1103-
11041061
```js
1062+
// Create two `Buffer` instances.
11051063
const buf1 = Buffer.allocUnsafe(26);
11061064
const buf2 = Buffer.allocUnsafe(26).fill('!');
11071065

@@ -1110,16 +1068,17 @@ for (let i = 0; i < 26; i++) {
11101068
buf1[i] = i + 97;
11111069
}
11121070

1071+
// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`
11131072
buf1.copy(buf2, 8, 16, 20);
11141073

11151074
console.log(buf2.toString('ascii', 0, 25));
11161075
// Prints: !!!!!!!!qrst!!!!!!!!!!!!!
11171076
```
11181077

1119-
Example: Create a single `Buffer` and copy data from one region to an
1120-
overlapping region within the same `Buffer`
1121-
11221078
```js
1079+
// Create a `Buffer` and copy data from one region to an overlapping region
1080+
// within the same `Buffer`.
1081+
11231082
const buf = Buffer.allocUnsafe(26);
11241083

11251084
for (let i = 0; i < 26; i++) {
@@ -1143,9 +1102,9 @@ added: v1.1.0
11431102
Creates and returns an [iterator] of `[index, byte]` pairs from the contents of
11441103
`buf`.
11451104

1146-
Example: Log the entire contents of a `Buffer`
1147-
11481105
```js
1106+
// Log the entire contents of a `Buffer`.
1107+
11491108
const buf = Buffer.from('buffer');
11501109

11511110
for (const pair of buf.entries()) {
@@ -1217,9 +1176,9 @@ Fills `buf` with the specified `value`. If the `offset` and `end` are not given,
12171176
the entire `buf` will be filled. This is meant to be a small simplification to
12181177
allow the creation and filling of a `Buffer` to be done on a single line.
12191178

1220-
Example: Fill a `Buffer` with the ASCII character `'h'`
1221-
12221179
```js
1180+
// Fill a `Buffer` with the ASCII character 'h'.
1181+
12231182
const b = Buffer.allocUnsafe(50).fill('h');
12241183

12251184
console.log(b.toString());
@@ -1231,9 +1190,9 @@ console.log(b.toString());
12311190
If the final write of a `fill()` operation falls on a multi-byte character,
12321191
then only the first bytes of that character that fit into `buf` are written.
12331192

1234-
Example: Fill a `Buffer` with a two-byte character
1235-
12361193
```js
1194+
// Fill a `Buffer` with a two-byte character.
1195+
12371196
console.log(Buffer.allocUnsafe(3).fill('\u0222'));
12381197
// Prints: <Buffer c8 a2 c8>
12391198
```
@@ -1374,8 +1333,6 @@ added: v1.1.0
13741333

13751334
Creates and returns an [iterator] of `buf` keys (indices).
13761335

1377-
Example:
1378-
13791336
```js
13801337
const buf = Buffer.from('buffer');
13811338

@@ -1476,9 +1433,9 @@ added: v0.1.90
14761433
Returns the amount of memory allocated for `buf` in bytes. Note that this
14771434
does not necessarily reflect the amount of "usable" data within `buf`.
14781435

1479-
Example: Create a `Buffer` and write a shorter ASCII string to it
1480-
14811436
```js
1437+
// Create a `Buffer` and write a shorter ASCII string to it.
1438+
14821439
const buf = Buffer.alloc(1234);
14831440

14841441
console.log(buf.length);
@@ -1850,10 +1807,10 @@ that of `end` equal to [`buf.length`].
18501807
Modifying the new `Buffer` slice will modify the memory in the original `Buffer`
18511808
because the allocated memory of the two objects overlap.
18521809

1853-
Example: Create a `Buffer` with the ASCII alphabet, take a slice, and then modify
1854-
one byte from the original `Buffer`
1855-
18561810
```js
1811+
// Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte
1812+
// from the original `Buffer`.
1813+
18571814
const buf1 = Buffer.allocUnsafe(26);
18581815

18591816
for (let i = 0; i < 26; i++) {
@@ -1985,8 +1942,6 @@ added: v0.9.2
19851942
Returns a JSON representation of `buf`. [`JSON.stringify()`] implicitly calls
19861943
this function when stringifying a `Buffer` instance.
19871944

1988-
Example:
1989-
19901945
```js
19911946
const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5]);
19921947
const json = JSON.stringify(buf);
@@ -2096,8 +2051,6 @@ The `length` parameter is the number of bytes to write. If `buf` did not contain
20962051
enough space to fit the entire string, only a partial amount of `string` will
20972052
be written. However, partially encoded characters will not be written.
20982053

2099-
Example:
2100-
21012054
```js
21022055
const buf = Buffer.allocUnsafe(256);
21032056

@@ -2517,8 +2470,6 @@ In the case where a developer may need to retain a small chunk of memory from a
25172470
pool for an indeterminate amount of time, it may be appropriate to create an
25182471
un-pooled `Buffer` instance using `SlowBuffer` then copy out the relevant bits.
25192472

2520-
Example:
2521-
25222473
```js
25232474
// Need to keep around a few small chunks of memory
25242475
const store = [];
@@ -2553,10 +2504,8 @@ Allocates a new `Buffer` of `size` bytes. If the `size` is larger than
25532504
thrown. A zero-length `Buffer` will be created if `size` is 0.
25542505

25552506
The underlying memory for `SlowBuffer` instances is *not initialized*. The
2556-
contents of a newly created `SlowBuffer` are unknown and may contain
2557-
sensitive data. Use [`buf.fill(0)`][`buf.fill()`] to initialize a `SlowBuffer` to zeroes.
2558-
2559-
Example:
2507+
contents of a newly created `SlowBuffer` are unknown and may contain sensitive
2508+
data. Use [`buf.fill(0)`][`buf.fill()`] to initialize a `SlowBuffer` to zeroes.
25602509

25612510
```js
25622511
const { SlowBuffer } = require('buffer');

0 commit comments

Comments
 (0)