compact-encoding
Advanced tools
+3
-10
@@ -84,12 +84,7 @@ const LE = (new Uint8Array(new Uint16Array([255]).buffer))[0] === 0xff | ||
| preencode (state, b) { | ||
| uint.preencode(state, b.length << 2) | ||
| state.end += ((4 - (state.end & 3)) & 3) | ||
| uint.preencode(state, b.length) | ||
| state.end += b.byteLength | ||
| }, | ||
| encode (state, b) { | ||
| const s = state.start | ||
| uint.encode(state, b.length << 2) | ||
| const padding = (4 - (state.start & 3)) & 3 | ||
| state.buffer[s + (state.buffer[s] <= 0xfc ? 0 : 1)] |= padding | ||
| for (let i = 0; i < padding; i++) state.buffer[state.start++] = 0 | ||
| uint.encode(state, b.length) | ||
| const view = new Uint8Array(b.buffer, b.byteOffset, b.byteLength) | ||
@@ -101,6 +96,4 @@ if (BE) hostToLE32(view, b.length) | ||
| decode (state) { | ||
| const l = uint.decode(state) | ||
| const len = l >>> 2 | ||
| const len = uint.decode(state) | ||
| state.start += (l & 3) | ||
| const byteOffset = state.buffer.byteOffset + state.start | ||
@@ -107,0 +100,0 @@ const s = state.start |
+1
-1
| { | ||
| "name": "compact-encoding", | ||
| "version": "1.1.0", | ||
| "version": "2.0.0", | ||
| "description": "A series of compact encoding schemes for building small and fast parsers and serializers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+1
-1
@@ -67,3 +67,3 @@ # compact-encoding | ||
| * `cenc.raw` - Pass through encodes a buffer - ie a basic copy. | ||
| * `cenc.uint32array` - Encodes a uint32array with it's length uint32 prefixed along with a 2 bit padding for alignment. | ||
| * `cenc.uint32array` - Encodes a uint32array with it's element length uint32 prefixed. | ||
| * `cenc.bool` - Encodes a boolean as 1 or 0. | ||
@@ -70,0 +70,0 @@ * `cenc.string` - Encodes a utf-8 string, similar to buffer. |
+4
-21
@@ -110,11 +110,11 @@ const enc = require('./') | ||
| enc.uint32array.preencode(state, new Uint32Array([1])) | ||
| t.same(state, { start: 0, end: 8, buffer: null }) | ||
| t.same(state, { start: 0, end: 5, buffer: null }) | ||
| enc.uint32array.preencode(state, new Uint32Array([42, 43])) | ||
| t.same(state, { start: 0, end: 20, buffer: null }) | ||
| t.same(state, { start: 0, end: 14, buffer: null }) | ||
| state.buffer = Buffer.alloc(state.end) | ||
| enc.uint32array.encode(state, new Uint32Array([1])) | ||
| t.same(state, { start: 8, end: 20, buffer: Buffer.from([7, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }) | ||
| t.same(state, { start: 5, end: 14, buffer: Buffer.from([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }) | ||
| enc.uint32array.encode(state, new Uint32Array([42, 43])) | ||
| t.same(state, { start: 20, end: 20, buffer: Buffer.from([7, 0, 0, 0, 1, 0, 0, 0, 11, 0, 0, 0, 42, 0, 0, 0, 43, 0, 0, 0]) }) | ||
| t.same(state, { start: 14, end: 14, buffer: Buffer.from([1, 1, 0, 0, 0, 2, 42, 0, 0, 0, 43, 0, 0, 0]) }) | ||
@@ -128,19 +128,2 @@ state.start = 0 | ||
| // aligned | ||
| state.start = 0 | ||
| const u = enc.uint32array.decode(state) | ||
| t.ok(u.buffer === state.buffer.buffer) | ||
| // unaligned | ||
| state.buffer = Buffer.concat([Buffer.from('.'), state.buffer]) | ||
| state.start = 1 | ||
| const a = enc.uint32array.decode(state) | ||
| const b = enc.uint32array.decode(state) | ||
| t.same(a, new Uint32Array([1])) | ||
| t.same(b, new Uint32Array([42, 43])) | ||
| t.ok(a.buffer !== state.buffer.buffer) | ||
| t.ok(b.buffer !== state.buffer.buffer) | ||
| t.end() | ||
@@ -147,0 +130,0 @@ }) |
21666
-3.73%492
-3.91%