Skip to content

Commit

Permalink
$mol_buffer - extended DataView
Browse files Browse the repository at this point in the history
  • Loading branch information
jin committed Oct 29, 2023
1 parent 91f79cb commit e72d372
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions buffer/buffer.ts
Original file line number Diff line number Diff line change
@@ -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 )
}

}
}

0 comments on commit e72d372

Please sign in to comment.