Browse Source

Some silly fixes to buffer.js

v0.7.4-release
Tim-Smart 15 years ago
committed by Ryan Dahl
parent
commit
2b07c9fcae
  1. 55
      lib/buffer.js

55
lib/buffer.js

@ -1,24 +1,24 @@
var Buffer = process.binding('buffer').Buffer; var SlowBuffer = process.binding('buffer').Buffer;
function toHex (n) { function toHex (n) {
if (n < 16) return "0" + n.toString(16); if (n < 16) return "0" + n.toString(16);
return n.toString(16); return n.toString(16);
} }
Buffer.isBuffer = function (b) { SlowBuffer.isSlowBuffer = function (b) {
return b instanceof Buffer; return b instanceof SlowBuffer;
}; };
Buffer.prototype.inspect = function () { SlowBuffer.prototype.inspect = function () {
var out = [], var out = [],
len = this.length; len = this.length;
for (var i = 0; i < len; i++) { for (var i = 0; i < len; i++) {
out[i] = toHex(this[i]); out[i] = toHex(this[i]);
} }
return "<Buffer " + out.join(" ") + ">"; return "<SlowBuffer " + out.join(" ") + ">";
}; };
Buffer.prototype.toString = function (encoding, start, stop) { SlowBuffer.prototype.toString = function (encoding, start, stop) {
encoding = String(encoding || 'utf8').toLowerCase(); encoding = String(encoding || 'utf8').toLowerCase();
start = +start || 0; start = +start || 0;
if (typeof stop == "undefined") stop = this.length; if (typeof stop == "undefined") stop = this.length;
@ -47,7 +47,7 @@ Buffer.prototype.toString = function (encoding, start, stop) {
} }
}; };
Buffer.prototype.write = function (string, offset, encoding) { SlowBuffer.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)
if (!isFinite(offset)) { if (!isFinite(offset)) {
@ -78,24 +78,24 @@ Buffer.prototype.write = function (string, offset, encoding) {
} }
}; };
Buffer.prototype.get = function (index) { SlowBuffer.prototype.get = function (index) {
return this[index]; return this[index];
}; };
Buffer.prototype.set = function (index, value) { SlowBuffer.prototype.set = function (index, value) {
return this[index] = value; return this[index] = value;
}; };
// FastBuffer // Buffer
var POOLSIZE = 8*1024; var POOLSIZE = 8*1024;
var pool; var pool;
function allocPool () { function allocPool () {
pool = new Buffer(POOLSIZE); pool = new SlowBuffer(POOLSIZE);
pool.used = 0; pool.used = 0;
} }
function FastBuffer (subject, encoding, legacy, slice_legacy) { function Buffer (subject, encoding, legacy, slice_legacy) {
var length, type; var length, type;
// Are we slicing? // Are we slicing?
@ -124,7 +124,7 @@ function FastBuffer (subject, encoding, legacy, slice_legacy) {
if (length > POOLSIZE) { if (length > POOLSIZE) {
// Big buffer, just alloc one. // Big buffer, just alloc one.
this.parent = new Buffer(subject, encoding); this.parent = new SlowBuffer(subject, encoding);
this.offset = 0; this.offset = 0;
} else { } else {
// Small buffer. // Small buffer.
@ -151,20 +151,19 @@ function FastBuffer (subject, encoding, legacy, slice_legacy) {
// Make sure the api is equivilent to old buffers, unless user doesn't // Make sure the api is equivilent to old buffers, unless user doesn't
// want overhead // want overhead
if (legacy !== false) { if (legacy !== false) {
Buffer.makeFastBuffer(this.parent, this, this.offset, this.length); SlowBuffer.makeFastBuffer(this.parent, this, this.offset, this.length);
} }
} }
exports.FastBuffer = FastBuffer; exports.Buffer = Buffer;
exports.Buffer = FastBuffer;
// Static methods // Static methods
FastBuffer.isBuffer = function isBuffer(b) { Buffer.isBuffer = function isBuffer(b) {
return b instanceof FastBuffer; return b instanceof Buffer;
}; };
// Inspect // Inspect
FastBuffer.prototype.inspect = function inspect() { Buffer.prototype.inspect = function inspect() {
var out = [], var out = [],
len = this.length; len = this.length;
for (var i = 0; i < len; i++) { for (var i = 0; i < len; i++) {
@ -173,12 +172,12 @@ FastBuffer.prototype.inspect = function inspect() {
return "<Buffer " + out.join(" ") + ">"; return "<Buffer " + out.join(" ") + ">";
}; };
FastBuffer.prototype.get = function (i) { Buffer.prototype.get = function get (i) {
if (i < 0 || i >= this.length) throw new Error("oob"); if (i < 0 || i >= this.length) throw new Error("oob");
return this.parent[this.offset + i]; return this.parent[this.offset + i];
}; };
FastBuffer.prototype.set = function (i, v) { Buffer.prototype.set = function set (i, v) {
if (i < 0 || i >= this.length) throw new Error("oob"); if (i < 0 || i >= this.length) throw new Error("oob");
return this.parent[this.offset + i] = v; return this.parent[this.offset + i] = v;
}; };
@ -187,7 +186,7 @@ FastBuffer.prototype.set = function (i, v) {
// slice should not use c++ // slice should not use c++
// write(string, offset = 0, encoding = 'uft8') // write(string, offset = 0, encoding = 'uft8')
FastBuffer.prototype.write = function write (string, offset, encoding) { Buffer.prototype.write = function write (string, offset, encoding) {
if (!isFinite(offset)) { if (!isFinite(offset)) {
var swap = encoding; var swap = encoding;
encoding = offset; encoding = offset;
@ -208,7 +207,7 @@ FastBuffer.prototype.write = function write (string, offset, encoding) {
} }
// toString(encoding, start=0, end=buffer.length) // toString(encoding, start=0, end=buffer.length)
FastBuffer.prototype.toString = function toSting (encoding, start, end) { Buffer.prototype.toString = function toString (encoding, start, end) {
encoding || (encoding = 'utf8'); encoding || (encoding = 'utf8');
start || (start = 0); start || (start = 0);
end || (end = this.length); end || (end = this.length);
@ -222,10 +221,10 @@ FastBuffer.prototype.toString = function toSting (encoding, start, end) {
}; };
// byteLength // byteLength
FastBuffer.byteLength = Buffer.byteLength; Buffer.byteLength = SlowBuffer.byteLength;
// copy(targetBuffer, targetStart, sourceStart, sourceEnd=buffer.length) // copy(targetBuffer, targetStart, sourceStart, sourceEnd=buffer.length)
FastBuffer.prototype.copy = function copy (target_buffer, target_start, start, end) { Buffer.prototype.copy = function copy (target, target_start, start, end) {
start || (start = 0); start || (start = 0);
end || (end = this.length); end || (end = this.length);
@ -234,11 +233,11 @@ FastBuffer.prototype.copy = function copy (target_buffer, target_start, start, e
end = this.length; end = this.length;
} }
return this.parent.copy(target_buffer, target_start, start + this.offset, end + this.offset); return this.parent.copy(target.parent, target_start + target.offset, start + this.offset, end + this.offset);
}; };
// slice(start, end) // slice(start, end)
FastBuffer.prototype.slice = function slice (start, end, legacy) { Buffer.prototype.slice = function slice (start, end, legacy) {
if (end > this.length) { if (end > this.length) {
throw new Error("oob"); throw new Error("oob");
} }
@ -246,5 +245,5 @@ FastBuffer.prototype.slice = function slice (start, end, legacy) {
throw new Error("oob"); throw new Error("oob");
} }
return new FastBuffer(this.parent, end - start, +start + this.offset, legacy); return new Buffer(this.parent, end - start, +start + this.offset, legacy);
}; };

Loading…
Cancel
Save