Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Add an optional length argument to Buffer.write()
Browse files Browse the repository at this point in the history
Fixes #243.
Fixes #1361.
  • Loading branch information
koichik authored and koichik committed Jul 22, 2011
1 parent f2ee5aa commit bc42d04
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 54 deletions.
10 changes: 5 additions & 5 deletions doc/api/buffers.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ Allocates a new buffer using an `array` of octets.

Allocates a new buffer containing the given `str`.

### buffer.write(string, offset=0, encoding='utf8')
### buffer.write(string, offset=0, length=buffer.length-offset, encoding='utf8')

Writes `string` to the buffer at `offset` using the given encoding. Returns
number of octets written. If `buffer` did not contain enough space to fit
the entire string, it will write a partial amount of the string.
The method will not write partial characters.
Writes `string` to the buffer at `offset` using the given encoding. `length` is
the number of bytes to write. Returns number of octets written. If `buffer` did
not contain enough space to fit the entire string, it will write a partial
amount of the string. The method will not write partial characters.

Example: write a utf8 string into a buffer, then print it

Expand Down
118 changes: 69 additions & 49 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,27 @@ SlowBuffer.prototype.toString = function(encoding, start, end) {
};


SlowBuffer.prototype.hexWrite = function(string, offset, maxLength) {
var len = string.length;
SlowBuffer.prototype.hexWrite = function(string, offset, length) {
offset = +offset || 0;
maxLength = Math.min(+maxLength || Number.MAX_VALUE, this.length - offset);
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');
}
maxLength = Math.min(maxLength, len / 2);
for (var i = 0; i < maxLength; 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;
Expand All @@ -111,48 +121,53 @@ SlowBuffer.prototype.hexWrite = function(string, offset, maxLength) {
};


SlowBuffer.prototype.write = function(string) {
// Support both (string, offset, maxLength, encoding)
// and the legacy (string, encoding, offset, maxLength)
var offset, maxLength, encoding;
if (isFinite(arguments[1])) {
offset = arguments[1];
if (isFinite(arguments[2])) {
maxLength = arguments[2];
encoding = arguments[3];
} else {
encoding = arguments[2];
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
encoding = arguments[1];
offset = arguments[2];
maxLength = arguments[3];
var swap = encoding;
encoding = offset;
offset = length;
length = swap;
}

offset = +offset || 0;
maxLength = Math.min(+maxLength || Number.MAX_VALUE, this.length - offset);
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, maxLength);
return this.hexWrite(string, offset, length);

case 'utf8':
case 'utf-8':
return this.utf8Write(string, offset, maxLength);
return this.utf8Write(string, offset, length);

case 'ascii':
return this.asciiWrite(string, offset, maxLength);
return this.asciiWrite(string, offset, length);

case 'binary':
return this.binaryWrite(string, offset, maxLength);
return this.binaryWrite(string, offset, length);

case 'base64':
return this.base64Write(string, offset, maxLength);
return this.base64Write(string, offset, length);

case 'ucs2':
case 'ucs-2':
return this.ucs2Write(string, offset, maxLength);
return this.ucs2Write(string, offset, length);

default:
throw new Error('Unknown encoding');
Expand Down Expand Up @@ -283,56 +298,61 @@ Buffer.prototype.set = function set(i, v) {
};


// write(string, offset = 0, maxLength = this.length-offset, encoding = 'utf8')
Buffer.prototype.write = function(string) {
// Support both (string, offset, maxLength, encoding)
// and the legacy (string, encoding, offset, maxLength)
var offset, maxLength, encoding;
if (isFinite(arguments[1])) {
offset = arguments[1];
if (isFinite(arguments[2])) {
maxLength = arguments[2];
encoding = arguments[3];
} else {
encoding = arguments[2];
// 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
encoding = arguments[1];
offset = arguments[2];
maxLength = arguments[3];
var swap = encoding;
encoding = offset;
offset = length;
length = swap;
}

offset = +offset || 0;
maxLength = Math.min(+maxLength || Number.MAX_VALUE, this.length - offset);
var remaining = this.length - offset;
if (!length) {
length = remaining;
} else {
length = +length;
if (length > remaining) {
length = remaining;
}
}
encoding = String(encoding || 'utf8').toLowerCase();

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:
Expand Down

0 comments on commit bc42d04

Please sign in to comment.