Skip to content

Commit 59f5d09

Browse files
committed
added comments and updated tests
1 parent f5c875b commit 59f5d09

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

lib/buffer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ Buffer.copyBytesFrom = function copyBytesFrom(view, offset, length) {
394394
let end;
395395
if (length !== undefined) {
396396
validateInteger(length, 'length', 0);
397+
// The old code used TypedArrayPrototypeSlice which clamps internally.
397398
end = MathMin(offset + length, viewLength);
398399
} else {
399400
end = viewLength;
@@ -1096,6 +1097,8 @@ function _fill(buf, value, offset, end, encoding) {
10961097
value = 0;
10971098
} else if (value.length === 1) {
10981099
// Fast path: If `value` fits into a single byte, use that numeric value.
1100+
// ASCII shares this branch with utf8 since code < 128 covers the full
1101+
// ASCII range; anything outside falls through to C++ bindingFill.
10991102
if (normalizedEncoding === 'utf8' || normalizedEncoding === 'ascii') {
11001103
const code = StringPrototypeCharCodeAt(value, 0);
11011104
if (code < 128) {

test/parallel/test-buffer-swap-fast.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,30 @@ const common = require('../common');
55
const assert = require('assert');
66

77
function testFastSwap16() {
8-
const buf = Buffer.alloc(256);
9-
buf.swap16();
8+
const buf = Buffer.from([0x01, 0x02, 0x03, 0x04]);
9+
const expected = Buffer.from([0x02, 0x01, 0x04, 0x03]);
10+
const padded = Buffer.alloc(256);
11+
buf.copy(padded);
12+
padded.swap16();
13+
assert.deepStrictEqual(padded.subarray(0, 4), expected);
1014
}
1115

1216
function testFastSwap32() {
13-
const buf = Buffer.alloc(256);
14-
buf.swap32();
17+
const buf = Buffer.from([0x01, 0x02, 0x03, 0x04]);
18+
const expected = Buffer.from([0x04, 0x03, 0x02, 0x01]);
19+
const padded = Buffer.alloc(256);
20+
buf.copy(padded);
21+
padded.swap32();
22+
assert.deepStrictEqual(padded.subarray(0, 4), expected);
1523
}
1624

1725
function testFastSwap64() {
18-
const buf = Buffer.alloc(256);
19-
buf.swap64();
26+
const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
27+
const expected = Buffer.from([0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01]);
28+
const padded = Buffer.alloc(256);
29+
buf.copy(padded);
30+
padded.swap64();
31+
assert.deepStrictEqual(padded.subarray(0, 8), expected);
2032
}
2133

2234
eval('%PrepareFunctionForOptimization(Buffer.prototype.swap16)');

0 commit comments

Comments
 (0)