From e72d372308679155295a9d12c103b0cfddf350e8 Mon Sep 17 00:00:00 2001 From: jin Date: Sun, 29 Oct 2023 22:37:44 +0300 Subject: [PATCH] $mol_buffer - extended DataView --- buffer/buffer.ts | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 buffer/buffer.ts diff --git a/buffer/buffer.ts b/buffer/buffer.ts new file mode 100644 index 00000000000..de9d5347adb --- /dev/null +++ b/buffer/buffer.ts @@ -0,0 +1,85 @@ +namespace $ { + export class $mol_buffer extends DataView { + + static toString() { + return $$.$mol_func_name( this ) + } + + getUint48( offset: number, LE = false ) { + if( offset % 4 ) { + return this.getUint16( offset, LE ) + this.getUint32( offset + 2, LE ) * 2**16 + } else { + return this.getUint32( offset, LE ) + this.getUint16( offset + 4, LE ) * 2**32 + } + } + + setUint48( offset: number, value: number, LE = false ) { + if( offset % 4 ) { + this.setUint16( offset, value & ( (1<<16) - 1 ), LE ) + this.setUint32( offset + 2, ( value / 2**16 )|0, LE ) + } else { + this.setUint32( offset, value |0, LE ) + this.setUint16( offset + 4, ( value / 2**32 )|0, LE ) + } + } + + int8( offset: number, next?: number ) { + if( next === undefined ) return this.getInt8( offset ) + if( next >= -(2**7) && next < 2**7 ) return this.setInt8( offset, next ), next + $mol_fail( new Error( `Wrong int8 value ${ next }` ) ) + } + + uint8( offset: number, next?: number ) { + if( next === undefined ) return this.getUint8( offset ) + if( next >= 0 && next < 2**8 ) return this.setUint8( offset, next ), next + $mol_fail( new Error( `Wrong uint8 value ${ next }` ) ) + } + + int16( offset: number, next?: number ) { + if( next === undefined ) return this.getInt16( offset, !!'LE' ) + if( next >= -(2**15) && next < 2**15 ) return this.setInt16( offset, next, !!'LE' ), next + $mol_fail( new Error( `Wrong int16 value ${ next }` ) ) + } + + uint16( offset: number, next?: number ) { + if( next === undefined ) return this.getUint16( offset, !!'LE' ) + if( next >= 0 && next < 2**16 ) return this.setUint16( offset, next, !!'LE' ), next + $mol_fail( new Error( `Wrong uint16 value ${ next }` ) ) + } + + int32( offset: number, next?: number ) { + if( next === undefined ) return this.getInt32( offset, !!'LE' ) + if( next >= -(2**31) && next < 2**31 ) return this.setInt32( offset, next, !!'LE' ), next + $mol_fail( new Error( `Wrong int32 value ${ next }` ) ) + } + + uint32( offset: number, next?: number ) { + if( next === undefined ) return this.getUint32( offset, !!'LE' ) + if( next >= 0 && next < 2**32 ) return this.setUint32( offset, next, !!'LE' ), next + $mol_fail( new Error( `Wrong uint32 value ${ next }` ) ) + } + + uint48( offset: number, next?: number ) { + if( next === undefined ) return this.getUint48( offset, !!'LE' ) + if( next >= 0 && next < 2**48 ) return this.setUint48( offset, next, !!'LE' ), next + $mol_fail( new Error( `Wrong uint48 value ${ next }` ) ) + } + + int64( offset: number, next?: bigint ) { + if( next === undefined ) return this.getBigInt64( offset, !!'LE' ) + if( next >= -(2**63) && next < 2**63 ) return this.setBigInt64( offset, next, !!'LE' ), next + $mol_fail( new Error( `Wrong int64 value ${ next }` ) ) + } + + uint64( offset: number, next?: bigint ) { + if( next === undefined ) return this.getBigUint64( offset, !!'LE' ) + if( next >= 0 && next < 2**64 ) return this.setBigUint64( offset, next, !!'LE' ), next + $mol_fail( new Error( `Wrong uint64 value ${ next }` ) ) + } + + asArray() { + return new Uint8Array( this.buffer, this.byteOffset, this.byteLength ) + } + + } +}