Skip to content

Commit 4db7848

Browse files
Trotttargos
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 f07e820 commit 4db7848

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

@@ -514,8 +496,6 @@ changes:
514496
Allocates a new `Buffer` of `size` bytes. If `fill` is `undefined`, the
515497
`Buffer` will be *zero-filled*.
516498

517-
Example:
518-
519499
```js
520500
const buf = Buffer.alloc(5);
521501

@@ -530,8 +510,6 @@ thrown. A zero-length `Buffer` will be created if `size` is 0.
530510
If `fill` is specified, the allocated `Buffer` will be initialized by calling
531511
[`buf.fill(fill)`][`buf.fill()`].
532512

533-
Example:
534-
535513
```js
536514
const buf = Buffer.alloc(5, 'a');
537515

@@ -542,8 +520,6 @@ console.log(buf);
542520
If both `fill` and `encoding` are specified, the allocated `Buffer` will be
543521
initialized by calling [`buf.fill(fill, encoding)`][`buf.fill()`].
544522

545-
Example:
546-
547523
```js
548524
const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');
549525

@@ -577,8 +553,6 @@ initialized*. The contents of the newly created `Buffer` are unknown and
577553
*may contain sensitive data*. Use [`Buffer.alloc()`] instead to initialize
578554
`Buffer` instances to zeroes.
579555

580-
Example:
581-
582556
```js
583557
const buf = Buffer.allocUnsafe(10);
584558

@@ -635,8 +609,6 @@ memory from a pool for an indeterminate amount of time, it may be appropriate
635609
to create an un-pooled `Buffer` instance using `Buffer.allocUnsafeSlow()` then
636610
copy out the relevant bits.
637611

638-
Example:
639-
640612
```js
641613
// Need to keep around a few small chunks of memory
642614
const store = [];
@@ -686,8 +658,6 @@ For `'base64'` and `'hex'`, this function assumes valid input. For strings that
686658
contain non-Base64/Hex-encoded data (e.g. whitespace), the return value might be
687659
greater than the length of a `Buffer` created from the string.
688660

689-
Example:
690-
691661
```js
692662
const str = '\u00bd + \u00bc = \u00be';
693663

@@ -716,8 +686,6 @@ Compares `buf1` to `buf2` typically for the purpose of sorting arrays of
716686
`Buffer` instances. This is equivalent to calling
717687
[`buf1.compare(buf2)`][`buf.compare()`].
718688

719-
Example:
720-
721689
```js
722690
const buf1 = Buffer.from('1234');
723691
const buf2 = Buffer.from('0123');
@@ -757,9 +725,9 @@ If `totalLength` is provided, it is coerced to an unsigned integer. If the
757725
combined length of the `Buffer`s in `list` exceeds `totalLength`, the result is
758726
truncated to `totalLength`.
759727

760-
Example: Create a single `Buffer` from a list of three `Buffer` instances
761-
762728
```js
729+
// Create a single `Buffer` from a list of three `Buffer` instances.
730+
763731
const buf1 = Buffer.alloc(10);
764732
const buf2 = Buffer.alloc(14);
765733
const buf3 = Buffer.alloc(18);
@@ -785,8 +753,6 @@ added: v5.10.0
785753

786754
Allocates a new `Buffer` using an `array` of octets.
787755

788-
Example:
789-
790756
```js
791757
// Creates a new Buffer containing UTF-8 bytes of the string 'buffer'
792758
const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
@@ -810,8 +776,6 @@ memory. For example, when passed a reference to the `.buffer` property of a
810776
[`TypedArray`] instance, the newly created `Buffer` will share the same
811777
allocated memory as the [`TypedArray`].
812778

813-
Example:
814-
815779
```js
816780
const arr = new Uint16Array(2);
817781

@@ -834,8 +798,6 @@ console.log(buf);
834798
The optional `byteOffset` and `length` arguments specify a memory range within
835799
the `arrayBuffer` that will be shared by the `Buffer`.
836800

837-
Example:
838-
839801
```js
840802
const ab = new ArrayBuffer(10);
841803
const buf = Buffer.from(ab, 0, 2);
@@ -856,8 +818,6 @@ added: v5.10.0
856818

857819
Copies the passed `buffer` data onto a new `Buffer` instance.
858820

859-
Example:
860-
861821
```js
862822
const buf1 = Buffer.from('buffer');
863823
const buf2 = Buffer.from(buf1);
@@ -976,9 +936,9 @@ This operator is inherited from `Uint8Array`, so its behavior on out-of-bounds
976936
access is the same as `UInt8Array` - that is, getting returns `undefined` and
977937
setting does nothing.
978938

979-
Example: Copy an ASCII string into a `Buffer`, one byte at a time
980-
981939
```js
940+
// Copy an ASCII string into a `Buffer` one byte at a time.
941+
982942
const str = 'Node.js';
983943
const buf = Buffer.allocUnsafe(str.length);
984944

@@ -1090,10 +1050,8 @@ added: v0.1.90
10901050
Copies data from a region of `buf` to a region in `target` even if the `target`
10911051
memory region overlaps with `buf`.
10921052

1093-
Example: Create two `Buffer` instances, `buf1` and `buf2`, and copy `buf1` from
1094-
byte 16 through byte 19 into `buf2`, starting at the 8th byte in `buf2`
1095-
10961053
```js
1054+
// Create two `Buffer` instances.
10971055
const buf1 = Buffer.allocUnsafe(26);
10981056
const buf2 = Buffer.allocUnsafe(26).fill('!');
10991057

@@ -1102,16 +1060,17 @@ for (let i = 0; i < 26; i++) {
11021060
buf1[i] = i + 97;
11031061
}
11041062

1063+
// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`
11051064
buf1.copy(buf2, 8, 16, 20);
11061065

11071066
console.log(buf2.toString('ascii', 0, 25));
11081067
// Prints: !!!!!!!!qrst!!!!!!!!!!!!!
11091068
```
11101069

1111-
Example: Create a single `Buffer` and copy data from one region to an
1112-
overlapping region within the same `Buffer`
1113-
11141070
```js
1071+
// Create a `Buffer` and copy data from one region to an overlapping region
1072+
// within the same `Buffer`.
1073+
11151074
const buf = Buffer.allocUnsafe(26);
11161075

11171076
for (let i = 0; i < 26; i++) {
@@ -1135,9 +1094,9 @@ added: v1.1.0
11351094
Creates and returns an [iterator] of `[index, byte]` pairs from the contents of
11361095
`buf`.
11371096

1138-
Example: Log the entire contents of a `Buffer`
1139-
11401097
```js
1098+
// Log the entire contents of a `Buffer`.
1099+
11411100
const buf = Buffer.from('buffer');
11421101

11431102
for (const pair of buf.entries()) {
@@ -1198,9 +1157,9 @@ Fills `buf` with the specified `value`. If the `offset` and `end` are not given,
11981157
the entire `buf` will be filled. This is meant to be a small simplification to
11991158
allow the creation and filling of a `Buffer` to be done on a single line.
12001159

1201-
Example: Fill a `Buffer` with the ASCII character `'h'`
1202-
12031160
```js
1161+
// Fill a `Buffer` with the ASCII character 'h'.
1162+
12041163
const b = Buffer.allocUnsafe(50).fill('h');
12051164

12061165
console.log(b.toString());
@@ -1212,9 +1171,9 @@ console.log(b.toString());
12121171
If the final write of a `fill()` operation falls on a multi-byte character,
12131172
then only the first bytes of that character that fit into `buf` are written.
12141173

1215-
Example: Fill a `Buffer` with a two-byte character
1216-
12171174
```js
1175+
// Fill a `Buffer` with a two-byte character.
1176+
12181177
console.log(Buffer.allocUnsafe(3).fill('\u0222'));
12191178
// Prints: <Buffer c8 a2 c8>
12201179
```
@@ -1355,8 +1314,6 @@ added: v1.1.0
13551314

13561315
Creates and returns an [iterator] of `buf` keys (indices).
13571316

1358-
Example:
1359-
13601317
```js
13611318
const buf = Buffer.from('buffer');
13621319

@@ -1457,9 +1414,9 @@ added: v0.1.90
14571414
Returns the amount of memory allocated for `buf` in bytes. Note that this
14581415
does not necessarily reflect the amount of "usable" data within `buf`.
14591416

1460-
Example: Create a `Buffer` and write a shorter ASCII string to it
1461-
14621417
```js
1418+
// Create a `Buffer` and write a shorter ASCII string to it.
1419+
14631420
const buf = Buffer.alloc(1234);
14641421

14651422
console.log(buf.length);
@@ -1819,10 +1776,10 @@ that of `end` equal to [`buf.length`].
18191776
Modifying the new `Buffer` slice will modify the memory in the original `Buffer`
18201777
because the allocated memory of the two objects overlap.
18211778

1822-
Example: Create a `Buffer` with the ASCII alphabet, take a slice, and then modify
1823-
one byte from the original `Buffer`
1824-
18251779
```js
1780+
// Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte
1781+
// from the original `Buffer`.
1782+
18261783
const buf1 = Buffer.allocUnsafe(26);
18271784

18281785
for (let i = 0; i < 26; i++) {
@@ -1954,8 +1911,6 @@ added: v0.9.2
19541911
Returns a JSON representation of `buf`. [`JSON.stringify()`] implicitly calls
19551912
this function when stringifying a `Buffer` instance.
19561913

1957-
Example:
1958-
19591914
```js
19601915
const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5]);
19611916
const json = JSON.stringify(buf);
@@ -2065,8 +2020,6 @@ The `length` parameter is the number of bytes to write. If `buf` did not contain
20652020
enough space to fit the entire string, only a partial amount of `string` will
20662021
be written. However, partially encoded characters will not be written.
20672022

2068-
Example:
2069-
20702023
```js
20712024
const buf = Buffer.allocUnsafe(256);
20722025

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

2480-
Example:
2481-
24822433
```js
24832434
// Need to keep around a few small chunks of memory
24842435
const store = [];
@@ -2513,10 +2464,8 @@ Allocates a new `Buffer` of `size` bytes. If the `size` is larger than
25132464
thrown. A zero-length `Buffer` will be created if `size` is 0.
25142465

25152466
The underlying memory for `SlowBuffer` instances is *not initialized*. The
2516-
contents of a newly created `SlowBuffer` are unknown and may contain
2517-
sensitive data. Use [`buf.fill(0)`][`buf.fill()`] to initialize a `SlowBuffer` to zeroes.
2518-
2519-
Example:
2467+
contents of a newly created `SlowBuffer` are unknown and may contain sensitive
2468+
data. Use [`buf.fill(0)`][`buf.fill()`] to initialize a `SlowBuffer` to zeroes.
25202469

25212470
```js
25222471
const { SlowBuffer } = require('buffer');

0 commit comments

Comments
 (0)