@@ -169,19 +169,11 @@ export default class Decoder {
169
169
// At this point `size` is always 31.
170
170
// If the value is 31, then the size is 65,821 + *the next three bytes after the
171
171
// type specifying bytes as a single unsigned integer*.
172
- return cursor (
173
- 65821 +
174
- utils . concat3 (
175
- this . db [ offset ] ,
176
- this . db [ offset + 1 ] ,
177
- this . db [ offset + 2 ]
178
- ) ,
179
- offset + 3
180
- ) ;
172
+ return cursor ( 65821 + this . db . readUIntBE ( offset , 3 ) , offset + 3 ) ;
181
173
}
182
174
183
175
private decodeBytes ( offset : number , size : number ) : Buffer {
184
- return this . db . slice ( offset , offset + size ) ;
176
+ return this . db . subarray ( offset , offset + size ) ;
185
177
}
186
178
187
179
private decodePointer ( ctrlByte : number , offset : number ) : Cursor {
@@ -201,26 +193,17 @@ export default class Decoder {
201
193
// If the size is 0, the pointer is built by appending the next byte to the last
202
194
// three bits to produce an 11-bit value.
203
195
if ( pointerSize === 0 ) {
204
- packed = utils . concat2 ( ctrlByte & 7 , this . db [ offset ] ) ;
196
+ packed = ( ( ctrlByte & 7 ) << 8 ) | this . db [ offset ] ;
205
197
206
198
// If the size is 1, the pointer is built by appending the next two bytes to the
207
199
// last three bits to produce a 19-bit value + 2048.
208
200
} else if ( pointerSize === 1 ) {
209
- packed = utils . concat3 (
210
- ctrlByte & 7 ,
211
- this . db [ offset ] ,
212
- this . db [ offset + 1 ]
213
- ) ;
201
+ packed = ( ( ctrlByte & 7 ) << 16 ) | this . db . readUInt16BE ( offset ) ;
214
202
215
203
// If the size is 2, the pointer is built by appending the next three bytes to the
216
204
// last three bits to produce a 27-bit value + 526336.
217
205
} else if ( pointerSize === 2 ) {
218
- packed = utils . concat4 (
219
- ctrlByte & 7 ,
220
- this . db [ offset ] ,
221
- this . db [ offset + 1 ] ,
222
- this . db [ offset + 2 ]
223
- ) ;
206
+ packed = ( ( ctrlByte & 7 ) << 24 ) | this . db . readUIntBE ( offset , 3 ) ;
224
207
225
208
// At next point `size` is always 3.
226
209
// Finally, if the size is 3, the pointer's value is contained in the next four
@@ -236,12 +219,12 @@ export default class Decoder {
236
219
237
220
private decodeArray ( size : number , offset : number ) : Cursor {
238
221
let tmp ;
239
- const array = [ ] ;
222
+ const array = new Array ( size ) ;
240
223
241
224
for ( let i = 0 ; i < size ; i ++ ) {
242
225
tmp = this . decode ( offset ) ;
243
226
offset = tmp . offset ;
244
- array . push ( tmp . value ) ;
227
+ array [ i ] = tmp . value ;
245
228
}
246
229
247
230
return cursor ( array , offset ) ;
@@ -280,40 +263,30 @@ export default class Decoder {
280
263
if ( size === 0 ) {
281
264
return 0 ;
282
265
}
266
+ if ( size < 4 ) {
267
+ return this . db . readUIntBE ( offset , size ) ;
268
+ }
283
269
return this . db . readInt32BE ( offset ) ;
284
270
}
285
271
286
272
private decodeUint ( offset : number , size : number ) {
287
- switch ( size ) {
288
- case 0 :
289
- return 0 ;
290
- case 1 :
291
- return this . db [ offset ] ;
292
- case 2 :
293
- return utils . concat2 ( this . db [ offset + 0 ] , this . db [ offset + 1 ] ) ;
294
- case 3 :
295
- return utils . concat3 (
296
- this . db [ offset + 0 ] ,
297
- this . db [ offset + 1 ] ,
298
- this . db [ offset + 2 ]
299
- ) ;
300
- case 4 :
301
- return utils . concat4 (
302
- this . db [ offset + 0 ] ,
303
- this . db [ offset + 1 ] ,
304
- this . db [ offset + 2 ] ,
305
- this . db [ offset + 3 ]
306
- ) ;
307
- case 8 :
308
- return this . decodeBigUint ( offset , size ) ;
309
- case 16 :
310
- return this . decodeBigUint ( offset , size ) ;
273
+ if ( size === 0 ) {
274
+ return 0 ;
275
+ }
276
+ if ( size <= 6 ) {
277
+ return this . db . readUIntBE ( offset , size ) ;
278
+ }
279
+ if ( size == 8 ) {
280
+ return this . db . readBigInt64BE ( offset ) . toString ( ) ;
281
+ }
282
+ if ( size > 16 ) {
283
+ return 0 ;
311
284
}
312
- return 0 ;
285
+ return this . decodeBigUint ( offset , size ) ;
313
286
}
314
287
315
288
private decodeString ( offset : number , size : number ) {
316
- return this . db . slice ( offset , offset + size ) . toString ( ) ;
289
+ return this . db . toString ( 'utf8' , offset , offset + size ) ;
317
290
}
318
291
319
292
private decodeBigUint ( offset : number , size : number ) {
0 commit comments