Skip to content

Commit

Permalink
buffer: use a default offset
Browse files Browse the repository at this point in the history
If none is provided, use zero as a default offset for all read/write
operations on the buffer.
  • Loading branch information
BridgeAR committed Apr 10, 2018
1 parent d1156da commit 95449ef
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 55 deletions.
92 changes: 50 additions & 42 deletions lib/internal/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
ERR_OUT_OF_RANGE
} = require('internal/errors').codes;
const { setupBufferJS } = binding;
const { endianness } = require('os');

// Remove from the binding so that function is only available as exported here.
// (That is, for internal use only.)
Expand All @@ -19,8 +20,7 @@ const float64Array = new Float64Array(1);
const uInt8Float64Array = new Uint8Array(float64Array.buffer);

// Check endianness.
float32Array[0] = -1;
const bigEndian = uInt8Float32Array[3] === 0;
const bigEndian = endianness === 'BE';

function checkBounds(buf, offset, byteLength) {
checkNumberType(offset);
Expand Down Expand Up @@ -57,6 +57,8 @@ function boundsError(value, length, type) {

// Read integers.
function readUIntLE(offset, byteLength) {
if (offset === undefined)
throw new ERR_INVALID_ARG_TYPE('offset', 'number', undefined);
if (byteLength === 6)
return readUInt48LE(this, offset);
if (byteLength === 5)
Expand All @@ -73,7 +75,7 @@ function readUIntLE(offset, byteLength) {
boundsError(byteLength, 6, 'byteLength');
}

function readUInt48LE(buf, offset) {
function readUInt48LE(buf, offset = 0) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 5];
Expand All @@ -87,7 +89,7 @@ function readUInt48LE(buf, offset) {
(buf[++offset] + last * 2 ** 8) * 2 ** 32;
}

function readUInt40LE(buf, offset) {
function readUInt40LE(buf, offset = 0) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 4];
Expand All @@ -101,7 +103,7 @@ function readUInt40LE(buf, offset) {
last * 2 ** 32;
}

function readUInt32LE(offset) {
function readUInt32LE(offset = 0) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 3];
Expand All @@ -114,7 +116,7 @@ function readUInt32LE(offset) {
last * 2 ** 24;
}

function readUInt24LE(buf, offset) {
function readUInt24LE(buf, offset = 0) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 2];
Expand All @@ -124,7 +126,7 @@ function readUInt24LE(buf, offset) {
return first + buf[++offset] * 2 ** 8 + last * 2 ** 16;
}

function readUInt16LE(offset) {
function readUInt16LE(offset = 0) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 1];
Expand All @@ -134,7 +136,7 @@ function readUInt16LE(offset) {
return first + last * 2 ** 8;
}

function readUInt8(offset) {
function readUInt8(offset = 0) {
checkNumberType(offset);
const val = this[offset];
if (val === undefined)
Expand All @@ -144,6 +146,8 @@ function readUInt8(offset) {
}

function readUIntBE(offset, byteLength) {
if (offset === undefined)
throw new ERR_INVALID_ARG_TYPE('offset', 'number', undefined);
if (byteLength === 6)
return readUInt48BE(this, offset);
if (byteLength === 5)
Expand All @@ -160,7 +164,7 @@ function readUIntBE(offset, byteLength) {
boundsError(byteLength, 6, 'byteLength');
}

function readUInt48BE(buf, offset) {
function readUInt48BE(buf, offset = 0) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 5];
Expand All @@ -174,7 +178,7 @@ function readUInt48BE(buf, offset) {
last;
}

function readUInt40BE(buf, offset) {
function readUInt40BE(buf, offset = 0) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 4];
Expand All @@ -188,7 +192,7 @@ function readUInt40BE(buf, offset) {
last;
}

function readUInt32BE(offset) {
function readUInt32BE(offset = 0) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 3];
Expand All @@ -201,7 +205,7 @@ function readUInt32BE(offset) {
last;
}

function readUInt24BE(buf, offset) {
function readUInt24BE(buf, offset = 0) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 2];
Expand All @@ -211,7 +215,7 @@ function readUInt24BE(buf, offset) {
return first * 2 ** 16 + buf[++offset] * 2 ** 8 + last;
}

function readUInt16BE(offset) {
function readUInt16BE(offset = 0) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 1];
Expand All @@ -222,6 +226,8 @@ function readUInt16BE(offset) {
}

function readIntLE(offset, byteLength) {
if (offset === undefined)
throw new ERR_INVALID_ARG_TYPE('offset', 'number', undefined);
if (byteLength === 6)
return readInt48LE(this, offset);
if (byteLength === 5)
Expand All @@ -238,7 +244,7 @@ function readIntLE(offset, byteLength) {
boundsError(byteLength, 6, 'byteLength');
}

function readInt48LE(buf, offset) {
function readInt48LE(buf, offset = 0) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 5];
Expand All @@ -253,7 +259,7 @@ function readInt48LE(buf, offset) {
buf[++offset] * 2 ** 24;
}

function readInt40LE(buf, offset) {
function readInt40LE(buf, offset = 0) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 4];
Expand All @@ -267,7 +273,7 @@ function readInt40LE(buf, offset) {
buf[++offset] * 2 ** 24;
}

function readInt32LE(offset) {
function readInt32LE(offset = 0) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 3];
Expand All @@ -280,7 +286,7 @@ function readInt32LE(offset) {
(last << 24); // Overflow
}

function readInt24LE(buf, offset) {
function readInt24LE(buf, offset = 0) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 2];
Expand All @@ -291,7 +297,7 @@ function readInt24LE(buf, offset) {
return val | (val & 2 ** 23) * 0x1fe;
}

function readInt16LE(offset) {
function readInt16LE(offset = 0) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 1];
Expand All @@ -302,7 +308,7 @@ function readInt16LE(offset) {
return val | (val & 2 ** 15) * 0x1fffe;
}

function readInt8(offset) {
function readInt8(offset = 0) {
checkNumberType(offset);
const val = this[offset];
if (val === undefined)
Expand All @@ -312,6 +318,8 @@ function readInt8(offset) {
}

function readIntBE(offset, byteLength) {
if (offset === undefined)
throw new ERR_INVALID_ARG_TYPE('offset', 'number', undefined);
if (byteLength === 6)
return readInt48BE(this, offset);
if (byteLength === 5)
Expand All @@ -328,7 +336,7 @@ function readIntBE(offset, byteLength) {
boundsError(byteLength, 6, 'byteLength');
}

function readInt48BE(buf, offset) {
function readInt48BE(buf, offset = 0) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 5];
Expand All @@ -343,7 +351,7 @@ function readInt48BE(buf, offset) {
last;
}

function readInt40BE(buf, offset) {
function readInt40BE(buf, offset = 0) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 4];
Expand All @@ -357,7 +365,7 @@ function readInt40BE(buf, offset) {
last;
}

function readInt32BE(offset) {
function readInt32BE(offset = 0) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 3];
Expand All @@ -370,7 +378,7 @@ function readInt32BE(offset) {
last;
}

function readInt24BE(buf, offset) {
function readInt24BE(buf, offset = 0) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 2];
Expand All @@ -381,7 +389,7 @@ function readInt24BE(buf, offset) {
return val | (val & 2 ** 23) * 0x1fe;
}

function readInt16BE(offset) {
function readInt16BE(offset = 0) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 1];
Expand All @@ -393,7 +401,7 @@ function readInt16BE(offset) {
}

// Read floats
function readFloatBackwards(offset) {
function readFloatBackwards(offset = 0) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 3];
Expand All @@ -407,7 +415,7 @@ function readFloatBackwards(offset) {
return float32Array[0];
}

function readFloatForwards(offset) {
function readFloatForwards(offset = 0) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 3];
Expand All @@ -421,7 +429,7 @@ function readFloatForwards(offset) {
return float32Array[0];
}

function readDoubleBackwards(offset) {
function readDoubleBackwards(offset = 0) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 7];
Expand All @@ -439,7 +447,7 @@ function readDoubleBackwards(offset) {
return float64Array[0];
}

function readDoubleForwards(offset) {
function readDoubleForwards(offset = 0) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 7];
Expand Down Expand Up @@ -522,7 +530,7 @@ function writeU_Int32LE(buf, value, offset, min, max) {
return offset;
}

function writeUInt32LE(value, offset) {
function writeUInt32LE(value, offset = 0) {
return writeU_Int32LE(this, value, offset, 0, 0xffffffff);
}

Expand All @@ -547,7 +555,7 @@ function writeU_Int16LE(buf, value, offset, min, max) {
return offset;
}

function writeUInt16LE(value, offset) {
function writeUInt16LE(value, offset = 0) {
return writeU_Int16LE(this, value, offset, 0, 0xffff);
}

Expand All @@ -565,7 +573,7 @@ function writeU_Int8(buf, value, offset, min, max) {
return offset + 1;
}

function writeUInt8(value, offset) {
function writeUInt8(value, offset = 0) {
return writeU_Int8(this, value, offset, 0, 0xff);
}

Expand Down Expand Up @@ -632,7 +640,7 @@ function writeU_Int32BE(buf, value, offset, min, max) {
return offset + 4;
}

function writeUInt32BE(value, offset) {
function writeUInt32BE(value, offset = 0) {
return writeU_Int32BE(this, value, offset, 0, 0xffffffff);
}

Expand All @@ -657,7 +665,7 @@ function writeU_Int16BE(buf, value, offset, min, max) {
return offset;
}

function writeUInt16BE(value, offset) {
function writeUInt16BE(value, offset = 0) {
return writeU_Int16BE(this, value, offset, 0, 0xffffffff);
}

Expand All @@ -678,15 +686,15 @@ function writeIntLE(value, offset, byteLength) {
boundsError(byteLength, 6, 'byteLength');
}

function writeInt32LE(value, offset) {
function writeInt32LE(value, offset = 0) {
return writeU_Int32LE(this, value, offset, -0x80000000, 0x7fffffff);
}

function writeInt16LE(value, offset) {
function writeInt16LE(value, offset = 0) {
return writeU_Int16LE(this, value, offset, -0x8000, 0x7fff);
}

function writeInt8(value, offset) {
function writeInt8(value, offset = 0) {
return writeU_Int8(this, value, offset, -0x80, 0x7f);
}

Expand All @@ -707,16 +715,16 @@ function writeIntBE(value, offset, byteLength) {
boundsError(byteLength, 6, 'byteLength');
}

function writeInt32BE(value, offset) {
function writeInt32BE(value, offset = 0) {
return writeU_Int32BE(this, value, offset, -0x80000000, 0x7fffffff);
}

function writeInt16BE(value, offset) {
function writeInt16BE(value, offset = 0) {
return writeU_Int16BE(this, value, offset, -0x8000, 0x7fff);
}

// Write floats.
function writeDoubleForwards(val, offset) {
function writeDoubleForwards(val, offset = 0) {
val = +val;
checkBounds(this, offset, 7);

Expand All @@ -732,7 +740,7 @@ function writeDoubleForwards(val, offset) {
return offset;
}

function writeDoubleBackwards(val, offset) {
function writeDoubleBackwards(val, offset = 0) {
val = +val;
checkBounds(this, offset, 7);

Expand All @@ -748,7 +756,7 @@ function writeDoubleBackwards(val, offset) {
return offset;
}

function writeFloatForwards(val, offset) {
function writeFloatForwards(val, offset = 0) {
val = +val;
checkBounds(this, offset, 3);

Expand All @@ -760,7 +768,7 @@ function writeFloatForwards(val, offset) {
return offset;
}

function writeFloatBackwards(val, offset) {
function writeFloatBackwards(val, offset = 0) {
val = +val;
checkBounds(this, offset, 3);

Expand Down
Loading

0 comments on commit 95449ef

Please sign in to comment.