Skip to content

Commit

Permalink
simpliy _copyActual, add clarifying comments
Browse files Browse the repository at this point in the history
  • Loading branch information
duncpro committed Oct 23, 2024
1 parent 0bc0b81 commit 02e8c57
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
throw new ERR_INVALID_ARG_TYPE('source', ['Buffer', 'Uint8Array'], source);
if (!ArrayBufferIsView(target))
throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
// Guaranteed real buffers

if (targetStart === undefined) {
targetStart = 0;
Expand All @@ -218,6 +219,7 @@ function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
if (targetStart < 0)
throw new ERR_OUT_OF_RANGE('targetStart', '>= 0', targetStart);
}
// Guaranteed targetStart >= 0

if (sourceStart === undefined) {
sourceStart = 0;
Expand All @@ -226,6 +228,7 @@ function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
if (sourceStart < 0 || sourceStart > source.byteLength)
throw new ERR_OUT_OF_RANGE('sourceStart', `>= 0 && <= ${source.byteLength}`, sourceStart);
}
// Guaranteed 0 <= sourceStart <= source.byteLength

if (sourceEnd === undefined) {
sourceEnd = source.byteLength;
Expand All @@ -234,34 +237,29 @@ function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
if (sourceEnd < 0)
throw new ERR_OUT_OF_RANGE('sourceEnd', '>= 0', sourceEnd);
}
// Guaranteed 0 <= sourceEnd

if (targetStart >= target.byteLength || sourceStart >= sourceEnd)
return 0;

// Guaranteed 0 <= sourceStart < sourceEnd (NO GUARANTEE <= source.byteLnegth)
// Guaranteed 0 <= targetStart < target.byteLength here

return _copyActual(source, target, targetStart, sourceStart, sourceEnd);
}

// Assumes `source`, `target` are real buffers.
// Assumes 0 <= sourceStart <= sourceEnd
// Assumes 0 <= targetStart <= target.byteLength
//
// This function clamps sourceEnd such that the source subarray will not exceed the length
// of the target subarray.
function _copyActual(source, target, targetStart, sourceStart, sourceEnd) {
// Enforce: 0 <= sourceStart <= sourceEnd
if (!(0 <= sourceStart && sourceStart <= sourceEnd)) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('expected 0 <= sourceStart <= sourceEnd' +
' but got 0 <= ' + sourceStart + ' <= ' + sourceEnd);
}

sourceEnd = MathMin(sourceEnd, source.byteLength);

// Enforce: 0 <= targetStart <= target.byteLength;
if (!(0 <= targetStart && targetStart <= target.byteLength)) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('expected 0 <= targetStart <= target.byteLength' +
' but got 0 <= ' + targetStart + ' <= ' + target.byteLength);
}

const sourceLength = sourceEnd - sourceStart;
const targetLength = target.byteLength - targetStart;
const copyLength = MathMin(sourceLength, targetLength);

_copy(source, target, targetStart, sourceStart, copyLength);

return copyLength;
}

Expand Down Expand Up @@ -609,6 +607,7 @@ Buffer.concat = function concat(list, length) {
throw new ERR_INVALID_ARG_TYPE(
`list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);
}
// (src, target, t_beg, s_beg, s_end)
pos += _copyActual(buf, buffer, pos, 0, buf.length);
}

Expand Down

0 comments on commit 02e8c57

Please sign in to comment.