From: James Halliday Date: Wed, 11 Dec 2013 01:16:45 +0000 (-0800) Subject: use the typedarray DataView and ArrayBuffer X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=143cfd6770bd5d4537ec582237fff53c8e86ca0f;p=buffer.git use the typedarray DataView and ArrayBuffer --- diff --git a/index.js b/index.js index 6a14f49..f1dcaec 100644 --- a/index.js +++ b/index.js @@ -413,7 +413,7 @@ function _readUInt16 (buf, offset, littleEndian, noAssert) { if (offset >= len) { return } else if (offset + 1 === len) { - var dv = new DataView(new ArrayBuffer(2)) + var dv = new xDataView(new xArrayBuffer(2)) dv.setUint8(0, buf[len - 1]) return dv.getUint16(0, littleEndian) } else { @@ -441,7 +441,7 @@ function _readUInt32 (buf, offset, littleEndian, noAssert) { if (offset >= len) { return } else if (offset + 3 >= len) { - var dv = new DataView(new ArrayBuffer(4)) + var dv = new xDataView(new xArrayBuffer(4)) for (var i = 0; i + offset < len; i++) { dv.setUint8(i, buf[i + offset]) } @@ -486,7 +486,7 @@ function _readInt16 (buf, offset, littleEndian, noAssert) { if (offset >= len) { return } else if (offset + 1 === len) { - var dv = new DataView(new ArrayBuffer(2)) + var dv = new xDataView(new xArrayBuffer(2)) dv.setUint8(0, buf[len - 1]) return dv.getInt16(0, littleEndian) } else { @@ -514,7 +514,7 @@ function _readInt32 (buf, offset, littleEndian, noAssert) { if (offset >= len) { return } else if (offset + 3 >= len) { - var dv = new DataView(new ArrayBuffer(4)) + var dv = new xDataView(new xArrayBuffer(4)) for (var i = 0; i + offset < len; i++) { dv.setUint8(i, buf[i + offset]) } @@ -596,7 +596,7 @@ function _writeUInt16 (buf, value, offset, littleEndian, noAssert) { if (offset >= len) { return } else if (offset + 1 === len) { - var dv = new DataView(new ArrayBuffer(2)) + var dv = new xDataView(new xArrayBuffer(2)) dv.setUint16(0, value, littleEndian) buf[offset] = dv.getUint8(0) } else { @@ -626,7 +626,7 @@ function _writeUInt32 (buf, value, offset, littleEndian, noAssert) { if (offset >= len) { return } else if (offset + 3 >= len) { - var dv = new DataView(new ArrayBuffer(4)) + var dv = new xDataView(new xArrayBuffer(4)) dv.setUint32(0, value, littleEndian) for (var i = 0; i + offset < len; i++) { buf[i + offset] = dv.getUint8(i) @@ -672,7 +672,7 @@ function _writeInt16 (buf, value, offset, littleEndian, noAssert) { if (offset >= len) { return } else if (offset + 1 === len) { - var dv = new DataView(new ArrayBuffer(2)) + var dv = new xDataView(new xArrayBuffer(2)) dv.setInt16(0, value, littleEndian) buf[offset] = dv.getUint8(0) } else { @@ -702,7 +702,7 @@ function _writeInt32 (buf, value, offset, littleEndian, noAssert) { if (offset >= len) { return } else if (offset + 3 >= len) { - var dv = new DataView(new ArrayBuffer(4)) + var dv = new xDataView(new xArrayBuffer(4)) dv.setInt32(0, value, littleEndian) for (var i = 0; i + offset < len; i++) { buf[i + offset] = dv.getUint8(i) @@ -734,7 +734,7 @@ function _writeFloat (buf, value, offset, littleEndian, noAssert) { if (offset >= len) { return } else if (offset + 3 >= len) { - var dv = new DataView(new ArrayBuffer(4)) + var dv = new xDataView(new xArrayBuffer(4)) dv.setFloat32(0, value, littleEndian) for (var i = 0; i + offset < len; i++) { buf[i + offset] = dv.getUint8(i) @@ -767,7 +767,7 @@ function _writeDouble (buf, value, offset, littleEndian, noAssert) { if (offset >= len) { return } else if (offset + 7 >= len) { - var dv = new DataView(new ArrayBuffer(8)) + var dv = new xDataView(new xArrayBuffer(8)) dv.setFloat64(0, value, littleEndian) for (var i = 0; i + offset < len; i++) { buf[i + offset] = dv.getUint8(i) @@ -896,7 +896,7 @@ function ProxyBuffer (arr) { this._arr = arr if (arr.byteLength !== 0) - this._dataview = new DataView(arr.buffer, arr.byteOffset, arr.byteLength) + this._dataview = new xDataView(arr.buffer, arr.byteOffset, arr.byteLength) } ProxyBuffer.prototype.write = BufferWrite @@ -1001,7 +1001,7 @@ function augment (arr) { arr._isBuffer = true if (arr.byteLength !== 0) - arr._dataview = new DataView(arr.buffer, arr.byteOffset, arr.byteLength) + arr._dataview = new xDataView(arr.buffer, arr.byteOffset, arr.byteLength) return arr @@ -1128,4 +1128,4 @@ function verifIEEE754(value, max, min) { function assert (test, message) { if (!test) throw new Error(message || 'Failed assertion') -} \ No newline at end of file +}