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

Commit

Permalink
Added ability to pass offset to buffer write and toString methods as …
Browse files Browse the repository at this point in the history
…a string, i.e. '2' and encoding as anything
  • Loading branch information
DmitryBaranovskiy authored and ry committed Jul 26, 2010
1 parent a585c5b commit 8ab3c3e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
45 changes: 17 additions & 28 deletions lib/buffer.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@ Buffer.isBuffer = function (b) {
}; };


Buffer.prototype.inspect = function () { Buffer.prototype.inspect = function () {
var s = "<Buffer "; var out = [],
for (var i = 0; i < this.length; i++) { len = this.length;
s += toHex(this[i]); for (var i = 0; i < len; i++) {
if (i != this.length - 1) s += ' '; out[i] = toHex(this[i]);
} }
s += ">"; return "<Buffer " + out.join(" ") + ">";
return s;
}; };


Buffer.prototype.toString = function (encoding, start, stop) { Buffer.prototype.toString = function (encoding, start, stop) {
encoding = (encoding || 'utf8').toLowerCase(); encoding = String(encoding || 'utf8').toLowerCase();
if (!start) start = 0; start = +start || 0;
if (stop === undefined) stop = this.length; if (typeof stop == "undefined") stop = this.length;


// Fastpath empty strings // Fastpath empty strings
if (stop === start) { if (+stop == start) {
return ''; return '';
} }


Expand All @@ -50,21 +49,17 @@ Buffer.prototype.toString = function (encoding, start, stop) {
} }
}; };


Buffer.prototype.write = function (string) { Buffer.prototype.write = function (string, offset, encoding) {
// Support both (string, offset, encoding) // Support both (string, offset, encoding)
// and the legacy (string, encoding, offset) // and the legacy (string, encoding, offset)
var offset, encoding; if (!isFinite(offset)) {

var swap = encoding;
if (typeof arguments[1] == 'string') { encoding = offset;
encoding = arguments[1]; offset = swap;
offset = arguments[2];
} else {
offset = arguments[1];
encoding = arguments[2];
} }


offset = offset || 0; offset = +offset || 0;
encoding = encoding || 'utf8'; encoding = String(encoding || 'utf8').toLowerCase();


switch (encoding) { switch (encoding) {
case 'utf8': case 'utf8':
Expand All @@ -83,10 +78,4 @@ Buffer.prototype.write = function (string) {
default: default:
throw new Error('Unknown encoding'); throw new Error('Unknown encoding');
} }
}; };






12 changes: 12 additions & 0 deletions test/simple/test-buffer.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ assert.equal(new Buffer('abc').toString('ascii', 0, 0), '');
assert.equal(new Buffer('abc').toString('ascii', -100, -100), ''); assert.equal(new Buffer('abc').toString('ascii', -100, -100), '');
assert.equal(new Buffer('abc').toString('ascii', 100, 100), ''); assert.equal(new Buffer('abc').toString('ascii', 100, 100), '');


// try toString() with a object as a encoding
assert.equal(new Buffer('abc').toString({toString: function () {return 'ascii';}}), 'abc');

// testing for smart defaults and ability to pass string values as offset
var writeTest = new Buffer('abcdes');
writeTest.write('n', 'ascii');
writeTest.write('o', 'ascii', '1');
writeTest.write('d', '2', 'ascii');
writeTest.write('e', 3, 'ascii');
writeTest.write('j', 'ascii', 4);
assert.equal(writeTest.toString(), 'nodejs');

var asciiString = "hello world"; var asciiString = "hello world";
var offset = 100; var offset = 100;
for (var j = 0; j < 500; j++) { for (var j = 0; j < 500; j++) {
Expand Down

0 comments on commit 8ab3c3e

Please sign in to comment.