From: Janus Date: Thu, 20 Sep 2012 22:21:38 +0000 (+0200) Subject: Fix read too, add test for read. Also add buffer.copy X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=6afa1a7bca057337347edd4a553747ca915536af;p=buffer.git Fix read too, add test for read. Also add buffer.copy --- diff --git a/index.js b/index.js index f63a415..b13ca2d 100644 --- a/index.js +++ b/index.js @@ -288,6 +288,17 @@ SlowBuffer.prototype.slice = function(start, end) { return new Buffer(this, end - start, +start); }; +SlowBuffer.prototype.copy = function(target, targetstart, sourcestart, sourceend) { + var temp = []; + for (var i=sourcestart; i>> 0); + val = buffer.parent[buffer.offset + offset + 1] << 16; + val |= buffer.parent[buffer.offset + offset + 2] << 8; + val |= buffer.parent[buffer.offset + offset + 3]; + val = val + (buffer.parent[buffer.offset + offset] << 24 >>> 0); } else { - val = buffer[offset + 2] << 16; - val |= buffer[offset + 1] << 8; - val |= buffer[offset]; - val = val + (buffer[offset + 3] << 24 >>> 0); + val = buffer.parent[buffer.offset + offset + 2] << 16; + val |= buffer.parent[buffer.offset + offset + 1] << 8; + val |= buffer.parent[buffer.offset + offset]; + val = val + (buffer.parent[buffer.offset + offset + 3] << 24 >>> 0); } return val; @@ -781,12 +792,12 @@ Buffer.prototype.readInt8 = function(offset, noAssert) { 'Trying to read beyond buffer length'); } - neg = buffer[offset] & 0x80; + neg = buffer.parent[buffer.offset + offset] & 0x80; if (!neg) { - return (buffer[offset]); + return (buffer.parent[buffer.offset + offset]); } - return ((0xff - buffer[offset] + 1) * -1); + return ((0xff - buffer.parent[buffer.offset + offset] + 1) * -1); }; function readInt16(buffer, offset, isBigEndian, noAssert) { diff --git a/test/buffer.js b/test/buffer.js index f5da95b..4486602 100644 --- a/test/buffer.js +++ b/test/buffer.js @@ -74,21 +74,26 @@ test('hex buffer to ascii', function (t) { }); test("hex of write{Uint,Int}{8,16,32}{LE,BE}", function(t) { - t.plan(2*2*2+2); + t.plan(2*(2*2*2+2)); ["UInt","Int"].forEach(function(x){ [8,16,32].forEach(function(y){ var endianesses = (y === 8) ? [""] : ["LE","BE"]; endianesses.forEach(function(z){ var v1 = new buffer.Buffer(y / 8); var v2 = new Buffer(y / 8); - var fn = "write" + x + y + z; + var writefn = "write" + x + y + z; var val = (x === "Int") ? -3 : 3; - v1[fn](val, 0); - v2[fn](val, 0); + v1[writefn](val, 0); + v2[writefn](val, 0); t.equal( v1.toString("hex"), v2.toString("hex") ); + var readfn = "read" + x + y + z; + t.equal( + v1[readfn](0), + v2[readfn](0) + ); }); }); });