import { int32 } from './int32' ; import { resize } from './resize' ; param { Uint8Array } buf returns { Uint8Array } export function trim_zeroes_right ( buf ) { let foundNonZero = int32 . zero ( ) ; let i ; let index = - 1 ; let isNonZero = int32 . zero ( ) ; const m = int32 . fromInt ( 0xff ) ; for ( i = buf . length - 1 ; i >= 0 ; i -- ) { isNonZero = int32 . fromInt ( buf [ i ] ) . sub ( 1 ) . rshift ( 8 ) . and ( m ) ; index = ( foundNonZero . not ( ) . and ( i ) ) . xor ( foundNonZero . and ( index ) ) . toNumber ( ) ; foundNonZero = foundNonZero . or ( isNonZero . not ( ) ) . and ( m ) ; } foundNonZero . wipe ( ) ; isNonZero . wipe ( ) ; return resize ( buf , index + 1 ) ; } param { Uint8Array } buf returns { Uint8Array } export function trim_zeroes_left ( buf ) { const ret = buf . slice ( ) ; ret . reverse ( ) ; return trim_zeroes_right ( ret ) . reverse ( ) ; }