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: #24815
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
cjihrig authored and BethGriggs committed Feb 12, 2019
1 parent 2b03878 commit bd08ede
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 @@ -25,7 +25,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 @@ -37,13 +37,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 @@ -74,7 +70,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 @@ -88,7 +84,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 @@ -102,7 +98,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 @@ -115,7 +111,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 @@ -125,7 +121,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 @@ -135,7 +131,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 All @@ -161,7 +157,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 @@ -175,7 +171,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 @@ -189,7 +185,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 @@ -202,7 +198,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 @@ -212,7 +208,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 All @@ -239,7 +235,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 @@ -254,7 +250,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 @@ -268,7 +264,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 @@ -281,7 +277,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 @@ -292,7 +288,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 @@ -303,7 +299,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 All @@ -329,7 +325,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 @@ -344,7 +340,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 @@ -358,7 +354,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 @@ -371,7 +367,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 @@ -382,7 +378,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 @@ -394,7 +390,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 @@ -408,7 +404,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 @@ -422,7 +418,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 @@ -440,7 +436,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 @@ -554,7 +550,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 bd08ede

Please sign in to comment.