Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: small fixes in the buffer.md #9795

Closed
wants to merge 10 commits into from
54 changes: 31 additions & 23 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ const buf = Buffer.from([1, 2, 3]);
// 1
// 2
// 3
for (var b of buf) {
for (const b of buf) {
console.log(b);
}
```
Expand Down Expand Up @@ -404,14 +404,14 @@ to initialize a `Buffer` to zeroes.
Example:

```js
const buf = new Buffer(5);
const buf = new Buffer(10);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is this a fix? why is 10 better than 5?

Copy link
Contributor Author

@vsemozhetbyt vsemozhetbyt Nov 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #9786. This increases the example size (with the predicted outputs in the comments) so the output could not be confusing if the example is testing via a file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scanned #9876, not finding what you mean. And further confused by "testing via a file". There is no file in this example. PR should standalone, can you describe why this is an improvement?

Copy link
Contributor Author

@vsemozhetbyt vsemozhetbyt Nov 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for my incomprehensibility. Consider this case: a user copies this example, pastes it in the empty file test.js, saves it and runs via console or IDE to see the output. He will not see the described output, he will see two identical lines with null bytes all the times. In #9786 I've described the cause. This fix increases the size of possible test.js so the two lines will be different. I'll try to make some GIFs now to explain more clearly what I mean.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the first GIF you can see a test attempt with the old code example. See how both output lines are the same in contravention of the doc prediction.

buffer1

In the second GIF you can see a test attempt with the new code example. See how both output lines are different as the doc predicts.

buffer2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I don't mind the buffer getting a bit bigger, but if you are hoping that means it will always be non-zero.... I don't think that's true. Your own system proves that the initial values can be anything, right? 10 bytes is not special, AFAICT.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thus I think the docs should make the behaviour clear, not the examples. Examples show only one scenario, the docs say how it will be for all scenarios. And frankly, I don't get the point of this example, it doesn't add anything to my understanding. The docs already said the memory wouldn't be initialized. But that's another issue, I guess.

Copy link
Contributor Author

@vsemozhetbyt vsemozhetbyt Nov 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Contents may vary" does mean it can be anything, including zeroed, but in my case, it is always all zeroes. I do not mean it is true for all the systems, but my case shows that for some systems it is. If the output is all zeroes one time and is not all zeroes another time, it would be OK. But persistent zeroes is confusing, it can make a novice think that doc is wrong.

In the #9786 I've linked to stats that show my system not always zero buffers 8 bytes or smaller. It has some correlation between script size and buffer zeroing scheme. Maybe I have a unique situation. But it seems we won't get more stats because I can't make my point clear(

So what should I do? Just not change these fragments?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know. It clearly confused you, and docs should clarify, not confuse. On the other hand, 10 seems as random as 5. I guess leave it, at least anyone wondering why it was changed can find the PR, this conversation, and understand why it changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the patience.

So can I remake the PR as I've described below?


// Prints: (contents may vary): <Buffer 78 e0 82 02 01>
// Prints: (contents may vary): <Buffer 48 21 4b 00 00 00 00 00 30 dd>
console.log(buf);

buf.fill(0);

// Prints: <Buffer 00 00 00 00 00>
// Prints: <Buffer 00 00 00 00 00 00 00 00 00 00>
console.log(buf);
```

Expand Down Expand Up @@ -523,14 +523,14 @@ initialized*. The contents of the newly created `Buffer` are unknown and
Example:

```js
const buf = Buffer.allocUnsafe(5);
const buf = Buffer.allocUnsafe(10);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


// Prints: (contents may vary): <Buffer 78 e0 82 02 01>
// Prints: (contents may vary): <Buffer a0 8b 28 3f 01 00 00 00 50 32>
console.log(buf);

buf.fill(0);

// Prints: <Buffer 00 00 00 00 00>
// Prints: <Buffer 00 00 00 00 00 00 00 00 00 00>
console.log(buf);
```

Expand Down Expand Up @@ -995,7 +995,7 @@ overlapping region within the same `Buffer`
```js
const buf = Buffer.allocUnsafe(26);

for (var i = 0 ; i < 26 ; i++) {
for (let i = 0 ; i < 26 ; i++) {
// 97 is the decimal ASCII value for 'a'
buf[i] = i + 97;
}
Expand Down Expand Up @@ -1028,7 +1028,7 @@ const buf = Buffer.from('buffer');
// [3, 102]
// [4, 101]
// [5, 114]
for (var pair of buf.entries()) {
for (const pair of buf.entries()) {
console.log(pair);
}
```
Expand Down Expand Up @@ -1122,7 +1122,7 @@ Examples:
const buf = Buffer.from('this is a buffer');

// Prints: 0
console.log(buf.indexOf('this')));
console.log(buf.indexOf('this'));

// Prints: 2
console.log(buf.indexOf('is'));
Expand Down Expand Up @@ -1212,7 +1212,7 @@ const buf = Buffer.from('buffer');
// 3
// 4
// 5
for (var key of buf.keys()) {
for (const key of buf.keys()) {
console.log(key);
}
```
Expand All @@ -1223,7 +1223,7 @@ added: v6.0.0
-->

* `value` {String | Buffer | Integer} What to search for
* `byteOffset` {Integer} Where to begin searching in `buf` (not inclusive).
* `byteOffset` {Integer} Where to begin searching in `buf`.
**Default:** [`buf.length`]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default is actually buf.length - 1.

byteOffset = dir ? 0 : (buffer.length - 1);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@silverwind Should I preserve the link? Should it be

[`buf.length - 1`][`buf.length`]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put the - 1 outside the link.

* `encoding` {String} If `value` is a string, this is its encoding.
**Default:** `'utf8'`
Expand Down Expand Up @@ -1264,7 +1264,7 @@ console.log(buf.lastIndexOf('buffer', 4));
const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');

// Prints: 6
console.log(utf16Buffer.lastIndexOf('\u03a3', null, 'ucs2'));
console.log(utf16Buffer.lastIndexOf('\u03a3', undefined, 'ucs2'));
Copy link
Contributor

@silverwind silverwind Dec 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both null and undefined are technically not valid arguments. Maybe make a better example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@silverwind It is a bit complicated here and I hoped it would be fixed fully when #9801 is addressed. Maybe this could be postponed till that big issue fixed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah fine with me.


// Prints: 4
console.log(utf16Buffer.lastIndexOf('\u03a3', -5, 'ucs2'));
Expand Down Expand Up @@ -1302,7 +1302,7 @@ use [`buf.slice()`] to create a new `Buffer`.
Examples:

```js
var buf = Buffer.allocUnsafe(10);
let buf = Buffer.allocUnsafe(10);

buf.write('abcdefghj', 0, 'ascii');

Expand Down Expand Up @@ -1446,7 +1446,7 @@ const buf = Buffer.from([0, 5]);
console.log(buf.readInt16BE());

// Prints: 1280
console.log(buf.readInt16LE(1));
console.log(buf.readInt16LE());

// Throws an exception: RangeError: Index out of range
console.log(buf.readInt16LE(1));
Expand Down Expand Up @@ -1509,10 +1509,10 @@ Examples:
```js
const buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);

// Prints: 1234567890ab
// Prints: -546f87a9cbee
console.log(buf.readIntLE(0, 6).toString(16));

// Prints: -546f87a9cbee
// Prints: 1234567890ab
console.log(buf.readIntBE(0, 6).toString(16));

// Throws an exception: RangeError: Index out of range
Expand Down Expand Up @@ -1673,7 +1673,7 @@ one byte from the original `Buffer`
```js
const buf1 = Buffer.allocUnsafe(26);

for (var i = 0 ; i < 26 ; i++) {
for (let i = 0 ; i < 26 ; i++) {
// 97 is the decimal ASCII value for 'a'
buf1[i] = i + 97;
}
Expand Down Expand Up @@ -1737,7 +1737,7 @@ console.log(buf1);
const buf2 = Buffer.from([0x1, 0x2, 0x3]);

// Throws an exception: RangeError: Buffer size must be a multiple of 16-bits
buf2.swap32();
buf2.swap16();
```

### buf.swap32()
Expand Down Expand Up @@ -1822,7 +1822,7 @@ Examples:
```js
const buf1 = Buffer.allocUnsafe(26);

for (var i = 0 ; i < 26 ; i++) {
for (let i = 0 ; i < 26 ; i++) {
// 97 is the decimal ASCII value for 'a'
buf1[i] = i + 97;
}
Expand Down Expand Up @@ -1897,7 +1897,7 @@ const buf = Buffer.from('buffer');
// 102
// 101
// 114
for (var value of buf.values()) {
for (const value of buf.values()) {
console.log(value);
}

Expand All @@ -1908,7 +1908,7 @@ for (var value of buf.values()) {
// 102
// 101
// 114
for (var value of buf) {
for (const value of buf) {
console.log(value);
}
```
Expand Down Expand Up @@ -2293,7 +2293,7 @@ Returns the maximum number of bytes that will be returned when
`buf.inspect()` is called. This can be overridden by user modules. See
[`util.inspect()`] for more details on `buf.inspect()` behavior.

Note that this is a property on the `buffer` module as returned by
Note that this is a property on the `buffer` module returned by
`require('buffer')`, not on the `Buffer` global or a `Buffer` instance.

## buffer.kMaxLength
Expand All @@ -2306,6 +2306,9 @@ added: v3.0.0
On 32-bit architectures, this value is `(2^30)-1` (~1GB).
On 64-bit architectures, this value is `(2^31)-1` (~2GB).

Note that this is a property on the `buffer` module returned by
`require('buffer')`, not on the `Buffer` global or a `Buffer` instance.

## buffer.transcode(source, fromEnc, toEnc)
<!-- YAML
added: v7.1.0
Expand All @@ -2325,6 +2328,8 @@ The transcoding process will use substitution characters if a given byte
sequence cannot be adequately represented in the target encoding. For instance:

```js
const buffer = require('buffer');

const newBuf = buffer.transcode(Buffer.from('€'), 'utf8', 'ascii');
console.log(newBuf.toString('ascii'));
// Prints: '?'
Expand All @@ -2333,6 +2338,9 @@ console.log(newBuf.toString('ascii'));
Because the Euro (`€`) sign is not representable in US-ASCII, it is replaced
with `?` in the transcoded `Buffer`.

Note that this is a property on the `buffer` module returned by
`require('buffer')`, not on the `Buffer` global or a `Buffer` instance.

## Class: SlowBuffer
<!-- YAML
deprecated: v6.0.0
Expand Down