diff options
author | koichik <koichik@improvement.jp> | 2011-07-21 21:53:11 +0400 |
---|---|---|
committer | koichik <koichik@improvement.jp> | 2011-07-23 21:01:02 +0400 |
commit | 50e147bd03a9e2d722e2d7fc96adf8ccefe49866 (patch) | |
tree | 9f356c8c41877c97bcdcc5444955a6488005f44f /lib | |
parent | fa829b0fd31b4723a6d5ffb77816b06baa838267 (diff) |
Add an optional length argument to Buffer.write()
Fixes #243.
Fixes #1361.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/buffer.js | 98 |
1 files changed, 70 insertions, 28 deletions
diff --git a/lib/buffer.js b/lib/buffer.js index b3b0b5446f3..f31d69e85c6 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -92,15 +92,27 @@ SlowBuffer.prototype.toString = function(encoding, start, end) { }; -SlowBuffer.prototype.hexWrite = function(string, offset) { - var len = string.length; +SlowBuffer.prototype.hexWrite = function(string, offset, length) { offset = +offset || 0; + var remaining = this.length - offset; + if (!length) { + length = remaining; + } else { + length = +length; + if (length > remaining) { + length = remaining; + } + } // must be an even number of digits - if (len % 2) { + var strLen = string.length; + if (strLen % 2) { throw new Error('Invalid hex string'); } - for (var i = 0; i < len / 2; i++) { + if (length > strLen / 2) { + length = strLen / 2; + } + for (var i = 0; i < length; i++) { var byte = parseInt(string.substr(i * 2, 2), 16); if (isNaN(byte)) throw new Error('Invalid hex string'); this[offset + i] = byte; @@ -109,38 +121,53 @@ SlowBuffer.prototype.hexWrite = function(string, offset) { }; -SlowBuffer.prototype.write = function(string, offset, encoding) { - // Support both (string, offset, encoding) - // and the legacy (string, encoding, offset) - if (!isFinite(offset)) { +SlowBuffer.prototype.write = function(string, offset, length, encoding) { + // Support both (string, offset, length, encoding) + // and the legacy (string, encoding, offset, length) + if (isFinite(offset)) { + if (!isFinite(length)) { + encoding = length; + length = undefined; + } + } else { // legacy var swap = encoding; encoding = offset; - offset = swap; + offset = length; + length = swap; } offset = +offset || 0; + var remaining = this.length - offset; + if (!length) { + length = remaining; + } else { + length = +length; + if (length > remaining) { + length = remaining; + } + } encoding = String(encoding || 'utf8').toLowerCase(); switch (encoding) { case 'hex': - return this.hexWrite(string, offset); + return this.hexWrite(string, offset, length); case 'utf8': case 'utf-8': - return this.utf8Write(string, offset); + return this.utf8Write(string, offset, length); case 'ascii': - return this.asciiWrite(string, offset); + return this.asciiWrite(string, offset, length); case 'binary': - return this.binaryWrite(string, offset); + return this.binaryWrite(string, offset, length); case 'base64': - return this.base64Write(string, offset); + return this.base64Write(string, offset, length); case 'ucs2': case 'ucs-2': - return this.ucs2Write(string, offset); + return this.ucs2Write(string, offset, length); default: throw new Error('Unknown encoding'); @@ -271,47 +298,61 @@ Buffer.prototype.set = function set(i, v) { }; -// write(string, offset = 0, encoding = 'utf8') -Buffer.prototype.write = function(string, offset, encoding) { - if (!isFinite(offset)) { +// write(string, offset = 0, length = buffer.length-offset, encoding = 'utf8') +Buffer.prototype.write = function(string, offset, length, encoding) { + // Support both (string, offset, length, encoding) + // and the legacy (string, encoding, offset, length) + if (isFinite(offset)) { + if (!isFinite(length)) { + encoding = length; + length = undefined; + } + } else { // legacy var swap = encoding; encoding = offset; - offset = swap; + offset = length; + length = swap; } offset = +offset || 0; + var remaining = this.length - offset; + if (!length) { + length = remaining; + } else { + length = +length; + if (length > remaining) { + length = remaining; + } + } encoding = String(encoding || 'utf8').toLowerCase(); - // Make sure we are not going to overflow - var maxLength = this.length - offset; - var ret; switch (encoding) { case 'hex': - ret = this.parent.hexWrite(string, this.offset + offset, maxLength); + ret = this.parent.hexWrite(string, this.offset + offset, length); break; case 'utf8': case 'utf-8': - ret = this.parent.utf8Write(string, this.offset + offset, maxLength); + ret = this.parent.utf8Write(string, this.offset + offset, length); break; case 'ascii': - ret = this.parent.asciiWrite(string, this.offset + offset, maxLength); + ret = this.parent.asciiWrite(string, this.offset + offset, length); break; case 'binary': - ret = this.parent.binaryWrite(string, this.offset + offset, maxLength); + ret = this.parent.binaryWrite(string, this.offset + offset, length); break; case 'base64': // Warning: maxLength not taken into account in base64Write - ret = this.parent.base64Write(string, this.offset + offset, maxLength); + ret = this.parent.base64Write(string, this.offset + offset, length); break; case 'ucs2': case 'ucs-2': - ret = this.parent.ucs2Write(string, this.offset + offset, maxLength); + ret = this.parent.ucs2Write(string, this.offset + offset, length); break; default: @@ -1019,3 +1060,4 @@ Buffer.prototype.writeDouble = function(value, offset, endian) { verifIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308); IEEE754.writeIEEE754(buffer, value, offset, endian, 52, 8); }; + |