import { reverseHex } from "./misc" ; param str export class StringStream { public str : string ; public pter : number ; /**
     * Initializes the stream with given string and pointer at position 0.
     */ public constructor ( str = "" ) { this . str = str ; this . pter = 0 ; } example public isEmpty ( ) { return this . pter >= this . str . length ; } example public peek ( bytes = 1 ) { if ( this . isEmpty ( ) ) { return "" ; } return this . str . substr ( this . pter , bytes * 2 ) ; } param bytes example public read ( bytes = 1 ) { if ( this . isEmpty ( ) ) { throw new Error ( "Reached the end of the stream!" ) ; } const out = this . str . substr ( this . pter , bytes * 2 ) ; this . pter += bytes * 2 ; return out ; } /**
     * Reads some bytes off the stream.
     * A variable-length integer is first read off the stream and then bytes equal to the integer is read off and returned.
     */ public readVarBytes ( ) { return this . read ( this . readVarInt ( ) ) ; } /**
     * Reads an integer of variable bytelength. May consume up to 9 bytes.
     * The first byte read indicates if more bytes need to be read off.
     */ public readVarInt ( ) { let len = parseInt ( this . read ( 1 ) , 16 ) ; if ( len === 0xfd ) { len = parseInt ( reverseHex ( this . read ( 2 ) ) , 16 ) ; } else if ( len === 0xfe ) { len = parseInt ( reverseHex ( this . read ( 4 ) ) , 16 ) ; } else if ( len === 0xff ) { len = parseInt ( reverseHex ( this . read ( 8 ) ) , 16 ) ; } return len ; } example public reset ( ) { this . pter = 0 ; } /**
     * Returns a printable string of the characters around the pointer.
     * Used for debugging.
     */ public context ( ) { const before = this . pter > 10 ? this . str . slice ( this . pter - 10 , this . pter ) : this . str . slice ( 0 , this . pter ) ; const current = this . read ( 1 ) ; const after = this . peek ( 5 ) ; this . pter -= 2 ; return `${ before }|${ current }|${ after }` ; } } export default StringStream ;