diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2018-04-02 14:44:26 +0300 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-04-13 20:58:26 +0300 |
commit | d5495e859c3aceab7f8a823e27950e715b746a35 (patch) | |
tree | 8c47b93f2da7476a8a0310c78ff13d408e9d82e5 /lib/internal/buffer.js | |
parent | a25f56730e815794ff645a2a050da22343fa7d89 (diff) |
buffer: use a default offset
If none is provided, use zero as a default offset for all read/write
operations on the buffer.
PR-URL: https://github.com/nodejs/node/pull/19749
Refs: https://github.com/nodejs/node/pull/18395
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'lib/internal/buffer.js')
-rw-r--r-- | lib/internal/buffer.js | 108 |
1 files changed, 55 insertions, 53 deletions
diff --git a/lib/internal/buffer.js b/lib/internal/buffer.js index 2c65194bfa0..59ee776250c 100644 --- a/lib/internal/buffer.js +++ b/lib/internal/buffer.js @@ -19,7 +19,9 @@ const float64Array = new Float64Array(1); const uInt8Float64Array = new Uint8Array(float64Array.buffer); // Check endianness. -float32Array[0] = -1; +float32Array[0] = -1; // 0xBF800000 +// Either it is [0, 0, 128, 191] or [191, 128, 0, 0]. It is not possible to +// check this with `os.endianness()` because that is determined at compile time. const bigEndian = uInt8Float32Array[3] === 0; function checkBounds(buf, offset, byteLength) { @@ -67,13 +69,13 @@ function readUIntLE(offset, byteLength) { return this.readUInt32LE(offset); if (byteLength === 2) return this.readUInt16LE(offset); - if (byteLength === 1) + if (byteLength === 1 || byteLength === undefined) return this.readUInt8(offset); boundsError(byteLength, 6, 'byteLength'); } -function readUInt48LE(buf, offset) { +function readUInt48LE(buf, offset = 0) { checkNumberType(offset); const first = buf[offset]; const last = buf[offset + 5]; @@ -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]; @@ -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]; @@ -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]; @@ -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]; @@ -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) @@ -154,13 +156,13 @@ function readUIntBE(offset, byteLength) { return this.readUInt32BE(offset); if (byteLength === 2) return this.readUInt16BE(offset); - if (byteLength === 1) + if (byteLength === 1 || byteLength === undefined) return this.readUInt8(offset); boundsError(byteLength, 6, 'byteLength'); } -function readUInt48BE(buf, offset) { +function readUInt48BE(buf, offset = 0) { checkNumberType(offset); const first = buf[offset]; const last = buf[offset + 5]; @@ -174,7 +176,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]; @@ -188,7 +190,7 @@ function readUInt40BE(buf, offset) { last; } -function readUInt32BE(offset) { +function readUInt32BE(offset = 0) { checkNumberType(offset); const first = this[offset]; const last = this[offset + 3]; @@ -201,7 +203,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]; @@ -211,7 +213,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]; @@ -232,13 +234,13 @@ function readIntLE(offset, byteLength) { return this.readInt32LE(offset); if (byteLength === 2) return this.readInt16LE(offset); - if (byteLength === 1) + if (byteLength === 1 || byteLength === undefined) return this.readInt8(offset); boundsError(byteLength, 6, 'byteLength'); } -function readInt48LE(buf, offset) { +function readInt48LE(buf, offset = 0) { checkNumberType(offset); const first = buf[offset]; const last = buf[offset + 5]; @@ -253,7 +255,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]; @@ -267,7 +269,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]; @@ -280,7 +282,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]; @@ -291,7 +293,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]; @@ -302,7 +304,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) @@ -322,13 +324,13 @@ function readIntBE(offset, byteLength) { return this.readInt32BE(offset); if (byteLength === 2) return this.readInt16BE(offset); - if (byteLength === 1) + if (byteLength === 1 || byteLength === undefined) return this.readInt8(offset); boundsError(byteLength, 6, 'byteLength'); } -function readInt48BE(buf, offset) { +function readInt48BE(buf, offset = 0) { checkNumberType(offset); const first = buf[offset]; const last = buf[offset + 5]; @@ -343,7 +345,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]; @@ -357,7 +359,7 @@ function readInt40BE(buf, offset) { last; } -function readInt32BE(offset) { +function readInt32BE(offset = 0) { checkNumberType(offset); const first = this[offset]; const last = this[offset + 3]; @@ -370,7 +372,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]; @@ -381,7 +383,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]; @@ -393,7 +395,7 @@ function readInt16BE(offset) { } // Read floats -function readFloatBackwards(offset) { +function readFloatBackwards(offset = 0) { checkNumberType(offset); const first = this[offset]; const last = this[offset + 3]; @@ -407,7 +409,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]; @@ -421,7 +423,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]; @@ -439,7 +441,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]; @@ -458,7 +460,7 @@ function readDoubleForwards(offset) { } // Write integers. -function writeUIntLE(value, offset, byteLength) { +function writeUIntLE(value, offset = 0, byteLength) { if (byteLength === 6) return writeU_Int48LE(this, value, offset, 0, 0xffffffffffff); if (byteLength === 5) @@ -469,7 +471,7 @@ function writeUIntLE(value, offset, byteLength) { return writeU_Int32LE(this, value, offset, 0, 0xffffffff); if (byteLength === 2) return writeU_Int16LE(this, value, offset, 0, 0xffff); - if (byteLength === 1) + if (byteLength === 1 || byteLength === undefined) return writeU_Int8(this, value, offset, 0, 0xff); boundsError(byteLength, 6, 'byteLength'); @@ -522,7 +524,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); } @@ -547,7 +549,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); } @@ -565,11 +567,11 @@ 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); } -function writeUIntBE(value, offset, byteLength) { +function writeUIntBE(value, offset = 0, byteLength) { if (byteLength === 6) return writeU_Int48BE(this, value, offset, 0, 0xffffffffffffff); if (byteLength === 5) @@ -580,7 +582,7 @@ function writeUIntBE(value, offset, byteLength) { return writeU_Int32BE(this, value, offset, 0, 0xffffffff); if (byteLength === 2) return writeU_Int16BE(this, value, offset, 0, 0xffff); - if (byteLength === 1) + if (byteLength === 1 || byteLength === undefined) return writeU_Int8(this, value, offset, 0, 0xff); boundsError(byteLength, 6, 'byteLength'); @@ -632,7 +634,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); } @@ -657,11 +659,11 @@ 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); } -function writeIntLE(value, offset, byteLength) { +function writeIntLE(value, offset = 0, byteLength) { if (byteLength === 6) return writeU_Int48LE(this, value, offset, -0x800000000000, 0x7fffffffffff); if (byteLength === 5) @@ -672,25 +674,25 @@ function writeIntLE(value, offset, byteLength) { return writeU_Int32LE(this, value, offset, -0x80000000, 0x7fffffff); if (byteLength === 2) return writeU_Int16LE(this, value, offset, -0x8000, 0x7fff); - if (byteLength === 1) + if (byteLength === 1 || byteLength === undefined) return writeU_Int8(this, value, offset, -0x80, 0x7f); 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); } -function writeIntBE(value, offset, byteLength) { +function writeIntBE(value, offset = 0, byteLength) { if (byteLength === 6) return writeU_Int48BE(this, value, offset, -0x800000000000, 0x7fffffffffff); if (byteLength === 5) @@ -701,22 +703,22 @@ function writeIntBE(value, offset, byteLength) { return writeU_Int32BE(this, value, offset, -0x80000000, 0x7fffffff); if (byteLength === 2) return writeU_Int16BE(this, value, offset, -0x8000, 0x7fff); - if (byteLength === 1) + if (byteLength === 1 || byteLength === undefined) return writeU_Int8(this, value, offset, -0x80, 0x7f); 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); @@ -732,7 +734,7 @@ function writeDoubleForwards(val, offset) { return offset; } -function writeDoubleBackwards(val, offset) { +function writeDoubleBackwards(val, offset = 0) { val = +val; checkBounds(this, offset, 7); @@ -748,7 +750,7 @@ function writeDoubleBackwards(val, offset) { return offset; } -function writeFloatForwards(val, offset) { +function writeFloatForwards(val, offset = 0) { val = +val; checkBounds(this, offset, 3); @@ -760,7 +762,7 @@ function writeFloatForwards(val, offset) { return offset; } -function writeFloatBackwards(val, offset) { +function writeFloatBackwards(val, offset = 0) { val = +val; checkBounds(this, offset, 3); |