buffer: misc logic simplification
Includes: * No need for `typeof` when checking undefined. * length is coerced to uint so no need to check if < 0. * Stay consistent and always throw `new` errors. * Returning offset + magic number in every write is error prone. Instead return the central write function which returns the correct offset.
This commit is contained in:
parent
d74932e518
commit
8ce02cf0aa
@ -147,7 +147,7 @@ Buffer.concat = function(list, length) {
|
||||
if (!Array.isArray(list))
|
||||
throw new TypeError('Usage: Buffer.concat(list[, length])');
|
||||
|
||||
if (typeof length === 'undefined') {
|
||||
if (length === undefined) {
|
||||
length = 0;
|
||||
for (var i = 0; i < list.length; i++)
|
||||
length += list[i].length;
|
||||
@ -162,9 +162,6 @@ Buffer.concat = function(list, length) {
|
||||
else if (list.length === 1)
|
||||
return list[0];
|
||||
|
||||
if (length < 0)
|
||||
throw new RangeError('length is not a positive number');
|
||||
|
||||
var buffer = new Buffer(length);
|
||||
var pos = 0;
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
@ -187,7 +184,7 @@ Buffer.prototype.toString = function(encoding, start, end) {
|
||||
encoding = !!encoding ? (encoding + '').toLowerCase() : 'utf8';
|
||||
|
||||
start = ~~start;
|
||||
end = typeof end === 'undefined' ? this.length : ~~end;
|
||||
end = (end === undefined) ? this.length : ~~end;
|
||||
|
||||
if (start < 0) start = 0;
|
||||
if (end > this.length) end = this.length;
|
||||
@ -256,10 +253,9 @@ var writeMsg = '.write(string, encoding, offset, length) is deprecated.' +
|
||||
' Use write(string, offset, length, encoding) instead.';
|
||||
Buffer.prototype.write = function(string, offset, length, encoding) {
|
||||
// allow write(string, encoding)
|
||||
if (typeof offset === 'string' && typeof length === 'undefined') {
|
||||
if (typeof offset === 'string' && length === undefined) {
|
||||
encoding = offset;
|
||||
offset = 0;
|
||||
length = undefined;
|
||||
|
||||
// allow write(string, offset[, length], encoding)
|
||||
} else if (isFinite(offset)) {
|
||||
@ -290,7 +286,7 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
|
||||
}
|
||||
|
||||
var remaining = this.length - offset;
|
||||
if (typeof length === 'undefined' || length > remaining)
|
||||
if (length === undefined || length > remaining)
|
||||
length = remaining;
|
||||
|
||||
encoding = !!encoding ? (encoding + '').toLowerCase() : 'utf8';
|
||||
@ -350,7 +346,7 @@ Buffer.prototype.toJSON = function() {
|
||||
Buffer.prototype.slice = function(start, end) {
|
||||
var len = this.length;
|
||||
start = ~~start;
|
||||
end = typeof end === 'undefined' ? len : ~~end;
|
||||
end = (end === undefined) ? len : ~~end;
|
||||
|
||||
if (start < 0) {
|
||||
start += len;
|
||||
@ -563,9 +559,9 @@ Buffer.prototype.readInt32BE = function(offset, noAssert) {
|
||||
|
||||
function checkInt(buffer, value, offset, ext, max, min) {
|
||||
if (value > max || value < min)
|
||||
throw TypeError('value is out of bounds');
|
||||
throw new TypeError('value is out of bounds');
|
||||
if (offset < 0 || offset + ext > buffer.length || buffer.length + offset < 0)
|
||||
throw RangeError('index out of range');
|
||||
throw new RangeError('index out of range');
|
||||
}
|
||||
|
||||
|
||||
@ -587,6 +583,7 @@ function writeUInt16(buffer, value, offset, isBigEndian) {
|
||||
buffer[offset + 1] = (value & 0xff00) >>> 8;
|
||||
buffer[offset] = value & 0x00ff;
|
||||
}
|
||||
return offset + 2;
|
||||
}
|
||||
|
||||
|
||||
@ -595,8 +592,7 @@ Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) {
|
||||
offset = ~~offset;
|
||||
if (!noAssert)
|
||||
checkInt(this, value, offset, 2, 0xffff, 0);
|
||||
writeUInt16(this, value, offset, false);
|
||||
return offset + 2;
|
||||
return writeUInt16(this, value, offset, false);
|
||||
};
|
||||
|
||||
|
||||
@ -605,8 +601,7 @@ Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
|
||||
offset = ~~offset;
|
||||
if (!noAssert)
|
||||
checkInt(this, value, offset, 2, 0xffff, 0);
|
||||
writeUInt16(this, value, offset, true);
|
||||
return offset + 2;
|
||||
return writeUInt16(this, value, offset, true);
|
||||
};
|
||||
|
||||
|
||||
@ -622,6 +617,7 @@ function writeUInt32(buffer, value, offset, isBigEndian) {
|
||||
buffer[offset + 1] = (value >>> 8) & 0xff;
|
||||
buffer[offset] = value & 0xff;
|
||||
}
|
||||
return offset + 4;
|
||||
}
|
||||
|
||||
|
||||
@ -630,8 +626,7 @@ Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
|
||||
offset = ~~offset;
|
||||
if (!noAssert)
|
||||
checkInt(this, value, offset, 4, 0xffffffff, 0);
|
||||
writeUInt32(this, value, offset, false);
|
||||
return offset + 4;
|
||||
return writeUInt32(this, value, offset, false);
|
||||
};
|
||||
|
||||
|
||||
@ -640,8 +635,7 @@ Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
|
||||
offset = ~~offset;
|
||||
if (!noAssert)
|
||||
checkInt(this, value, offset, 4, 0xffffffff, 0);
|
||||
writeUInt32(this, value, offset, true);
|
||||
return offset + 4;
|
||||
return writeUInt32(this, value, offset, true);
|
||||
};
|
||||
|
||||
|
||||
@ -699,8 +693,7 @@ Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
|
||||
if (!noAssert)
|
||||
checkInt(this, value, offset, 2, 0x7fff, -0x8000);
|
||||
if (value < 0) value = 0xffff + value + 1;
|
||||
writeUInt16(this, value, offset, false);
|
||||
return offset + 2;
|
||||
return writeUInt16(this, value, offset, false);
|
||||
};
|
||||
|
||||
|
||||
@ -710,8 +703,7 @@ Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
|
||||
if (!noAssert)
|
||||
checkInt(this, value, offset, 2, 0x7fff, -0x8000);
|
||||
if (value < 0) value = 0xffff + value + 1;
|
||||
writeUInt16(this, value, offset, true);
|
||||
return offset + 2;
|
||||
return writeUInt16(this, value, offset, true);
|
||||
};
|
||||
|
||||
|
||||
@ -721,8 +713,7 @@ Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
|
||||
if (!noAssert)
|
||||
checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
|
||||
if (value < 0) value = 0xffffffff + value + 1;
|
||||
writeUInt32(this, value, offset, false);
|
||||
return offset + 4;
|
||||
return writeUInt32(this, value, offset, false);
|
||||
};
|
||||
|
||||
|
||||
@ -732,6 +723,5 @@ Buffer.prototype.writeInt32BE = function(value, offset, noAssert) {
|
||||
if (!noAssert)
|
||||
checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
|
||||
if (value < 0) value = 0xffffffff + value + 1;
|
||||
writeUInt32(this, value, offset, true);
|
||||
return offset + 4;
|
||||
return writeUInt32(this, value, offset, true);
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user