import ArrayBufferSlice from './ArrayBufferSlice' ; export function assert ( b , message = "" ) { if ( ! b ) { console . error ( new Error ( ) . stack ) ; throw new Error ( `Assert fail: ${ message }` ) ; } } export function assertExists < T > ( v , name = '' ) { if ( v !== undefined && v !== null ) return v ; else throw new Error ( `Missing object ${ name }` ) ; } export function nullify < T > ( v ) { return v === undefined ? null : v ; } export function readString ( buffer , offs , length = - 1 , nulTerminated = true , encoding = null ) { const buf = buffer . createTypedArray ( Uint8Array , offs ) ; let byteLength = 0 ; while ( true ) { if ( length >= 0 && byteLength >= length ) break ; if ( nulTerminated && buf [ byteLength ] === 0 ) break ; byteLength ++ ; } if ( byteLength === 0 ) return "" ; if ( encoding !== null ) { return decodeString ( buffer , offs , byteLength , encoding ) ; } else { return copyBufferToString ( buffer , offs , byteLength ) ; } } function copyBufferToString ( buffer , offs , byteLength ) { const buf = buffer . createTypedArray ( Uint8Array , offs ) ; let S = '' ; for ( let i = 0 ; i < byteLength ; i ++ ) S += String . fromCharCode ( buf [ i ] ) ; return S ; } export function decodeString ( buffer , offs = undefined , byteLength = undefined , encoding = 'utf8' ) { if ( typeof TextDecoder !== 'undefined' ) { return new TextDecoder ( encoding ) ! . decode ( buffer . copyToBuffer ( offs , byteLength ) ) ; } else if ( typeof require !== 'undefined' ) { const iconv = require ( 'iconv-lite' ) ; return iconv . decode ( Buffer . from ( buffer . copyToBuffer ( offs , byteLength ) ) , encoding ) ; } else { throw "whoops" ; } } export function align ( n , multiple ) { const mask = ( multiple - 1 ) ; return ( n + mask ) & ~ mask ; } export function alignNonPowerOfTwo ( n , multiple ) { return ( ( ( n + multiple - 1 ) / multiple ) | 0 ) * multiple ; } export function nArray < T > ( n , c ) { const d = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) d [ i ] = c ( i ) ; return d ; } export function leftPad ( S , spaces , ch = '0' ) { while ( S . length < spaces ) S = `${ ch }${ S }` ; return S ; } export function hexzero ( n , spaces ) { let S = ( n >>> 0 ) . toString ( 16 ) ; return leftPad ( S , spaces ) ; } export function hexzero0x ( n , spaces = 8 ) { if ( n < 0 ) return `-0x${ hexzero ( - n , spaces ) }` ; else return `0x${ hexzero ( n , spaces ) }` ; } export function flatten < T > ( L ) { const R = [ ] ; for ( let i = 0 ; i < L . length ; i ++ ) R . push ( ... L [ i ] ) ; return R ; } export function fallback < T > ( v , fallback ) { return v !== null ? v : fallback ; } export function fallbackUndefined < T > ( v , fallback ) { return ( v !== null && v !== undefined ) ? v : fallback ; } export function arrayRemove < T > ( L , n ) { const idx = L . indexOf ( n ) ; assert ( idx >= 0 ) ; L . splice ( idx , 1 ) ; return idx ; } export function arrayRemoveIfExist < T > ( L , n ) { const idx = L . indexOf ( n ) ; if ( idx >= 0 ) L . splice ( idx , 1 ) ; return idx ; } export function bisectRight < T > ( L , e , compare ) { let lo = 0 , hi = L . length ; while ( lo < hi ) { const mid = lo + ( ( hi - lo ) >>> 1 ) ; const cmp = compare ( e , L [ mid ] ) ; if ( cmp < 0 ) hi = mid ; else lo = mid + 1 ; } return lo ; } export function spliceBisectRight < T > ( L , e , compare ) { const idx = bisectRight ( L , e , compare ) ; L . splice ( idx , 0 , e ) ; } export function setBitFlagEnabled ( v , mask , enabled ) { if ( enabled ) v |= mask ; else v &= ~ mask ; return v ; } export function mod ( a , b ) { return ( a + b ) % b ; }