Skip to content

Commit

Permalink
buffer: remove checkNumberType()
Browse files Browse the repository at this point in the history
checkNumberType() was a very thin wrapper around validateNumber().
This commit removes checkNumberType() and used validateNumber()
directly instead.

PR-URL: nodejs#24815
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
cjihrig authored and refack committed Jan 10, 2019
1 parent 35401d7 commit 6877892
Showing 1 changed file with 29 additions and 33 deletions.
62 changes: 29 additions & 33 deletions lib/internal/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ float32Array[0] = -1; // 0xBF800000
const bigEndian = uInt8Float32Array[3] === 0;

function checkBounds(buf, offset, byteLength) {
checkNumberType(offset);
validateNumber(offset, 'offset');
if (buf[offset] === undefined || buf[offset + byteLength] === undefined)
boundsError(offset, buf.length - (byteLength + 1));
}
Expand All @@ -38,13 +38,9 @@ function checkInt(value, min, max, buf, offset, byteLength) {
checkBounds(buf, offset, byteLength);
}

function checkNumberType(value, type) {
validateNumber(value, type || 'offset');
}

function boundsError(value, length, type) {
if (Math.floor(value) !== value) {
checkNumberType(value, type);
validateNumber(value, type);
throw new ERR_OUT_OF_RANGE(type || 'offset', 'an integer', value);
}

Expand Down Expand Up @@ -77,7 +73,7 @@ function readUIntLE(offset, byteLength) {
}

function readUInt48LE(buf, offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = buf[offset];
const last = buf[offset + 5];
if (first === undefined || last === undefined)
Expand All @@ -91,7 +87,7 @@ function readUInt48LE(buf, offset = 0) {
}

function readUInt40LE(buf, offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = buf[offset];
const last = buf[offset + 4];
if (first === undefined || last === undefined)
Expand All @@ -105,7 +101,7 @@ function readUInt40LE(buf, offset = 0) {
}

function readUInt32LE(offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = this[offset];
const last = this[offset + 3];
if (first === undefined || last === undefined)
Expand All @@ -118,7 +114,7 @@ function readUInt32LE(offset = 0) {
}

function readUInt24LE(buf, offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = buf[offset];
const last = buf[offset + 2];
if (first === undefined || last === undefined)
Expand All @@ -128,7 +124,7 @@ function readUInt24LE(buf, offset = 0) {
}

function readUInt16LE(offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = this[offset];
const last = this[offset + 1];
if (first === undefined || last === undefined)
Expand All @@ -138,7 +134,7 @@ function readUInt16LE(offset = 0) {
}

function readUInt8(offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const val = this[offset];
if (val === undefined)
boundsError(offset, this.length - 1);
Expand Down Expand Up @@ -166,7 +162,7 @@ function readUIntBE(offset, byteLength) {
}

function readUInt48BE(buf, offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = buf[offset];
const last = buf[offset + 5];
if (first === undefined || last === undefined)
Expand All @@ -180,7 +176,7 @@ function readUInt48BE(buf, offset = 0) {
}

function readUInt40BE(buf, offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = buf[offset];
const last = buf[offset + 4];
if (first === undefined || last === undefined)
Expand All @@ -194,7 +190,7 @@ function readUInt40BE(buf, offset = 0) {
}

function readUInt32BE(offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = this[offset];
const last = this[offset + 3];
if (first === undefined || last === undefined)
Expand All @@ -207,7 +203,7 @@ function readUInt32BE(offset = 0) {
}

function readUInt24BE(buf, offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = buf[offset];
const last = buf[offset + 2];
if (first === undefined || last === undefined)
Expand All @@ -217,7 +213,7 @@ function readUInt24BE(buf, offset = 0) {
}

function readUInt16BE(offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = this[offset];
const last = this[offset + 1];
if (first === undefined || last === undefined)
Expand Down Expand Up @@ -246,7 +242,7 @@ function readIntLE(offset, byteLength) {
}

function readInt48LE(buf, offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = buf[offset];
const last = buf[offset + 5];
if (first === undefined || last === undefined)
Expand All @@ -261,7 +257,7 @@ function readInt48LE(buf, offset = 0) {
}

function readInt40LE(buf, offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = buf[offset];
const last = buf[offset + 4];
if (first === undefined || last === undefined)
Expand All @@ -275,7 +271,7 @@ function readInt40LE(buf, offset = 0) {
}

function readInt32LE(offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = this[offset];
const last = this[offset + 3];
if (first === undefined || last === undefined)
Expand All @@ -288,7 +284,7 @@ function readInt32LE(offset = 0) {
}

function readInt24LE(buf, offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = buf[offset];
const last = buf[offset + 2];
if (first === undefined || last === undefined)
Expand All @@ -299,7 +295,7 @@ function readInt24LE(buf, offset = 0) {
}

function readInt16LE(offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = this[offset];
const last = this[offset + 1];
if (first === undefined || last === undefined)
Expand All @@ -310,7 +306,7 @@ function readInt16LE(offset = 0) {
}

function readInt8(offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const val = this[offset];
if (val === undefined)
boundsError(offset, this.length - 1);
Expand Down Expand Up @@ -338,7 +334,7 @@ function readIntBE(offset, byteLength) {
}

function readInt48BE(buf, offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = buf[offset];
const last = buf[offset + 5];
if (first === undefined || last === undefined)
Expand All @@ -353,7 +349,7 @@ function readInt48BE(buf, offset = 0) {
}

function readInt40BE(buf, offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = buf[offset];
const last = buf[offset + 4];
if (first === undefined || last === undefined)
Expand All @@ -367,7 +363,7 @@ function readInt40BE(buf, offset = 0) {
}

function readInt32BE(offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = this[offset];
const last = this[offset + 3];
if (first === undefined || last === undefined)
Expand All @@ -380,7 +376,7 @@ function readInt32BE(offset = 0) {
}

function readInt24BE(buf, offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = buf[offset];
const last = buf[offset + 2];
if (first === undefined || last === undefined)
Expand All @@ -391,7 +387,7 @@ function readInt24BE(buf, offset = 0) {
}

function readInt16BE(offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = this[offset];
const last = this[offset + 1];
if (first === undefined || last === undefined)
Expand All @@ -403,7 +399,7 @@ function readInt16BE(offset = 0) {

// Read floats
function readFloatBackwards(offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = this[offset];
const last = this[offset + 3];
if (first === undefined || last === undefined)
Expand All @@ -417,7 +413,7 @@ function readFloatBackwards(offset = 0) {
}

function readFloatForwards(offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = this[offset];
const last = this[offset + 3];
if (first === undefined || last === undefined)
Expand All @@ -431,7 +427,7 @@ function readFloatForwards(offset = 0) {
}

function readDoubleBackwards(offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = this[offset];
const last = this[offset + 7];
if (first === undefined || last === undefined)
Expand All @@ -449,7 +445,7 @@ function readDoubleBackwards(offset = 0) {
}

function readDoubleForwards(offset = 0) {
checkNumberType(offset);
validateNumber(offset, 'offset');
const first = this[offset];
const last = this[offset + 7];
if (first === undefined || last === undefined)
Expand Down Expand Up @@ -563,7 +559,7 @@ function writeUInt16LE(value, offset = 0) {
function writeU_Int8(buf, value, offset, min, max) {
value = +value;
// `checkInt()` can not be used here because it checks two entries.
checkNumberType(offset);
validateNumber(offset, 'offset');
if (value > max || value < min) {
throw new ERR_OUT_OF_RANGE('value', `>= ${min} and <= ${max}`, value);
}
Expand Down

0 comments on commit 6877892

Please sign in to comment.