import { ByteArray } from "./Arrays/ByteArray" ; import { Bytes } from "./utils/Bytes" ; export class ScaleString extends ByteArray { private _valueStr : string ; constructor ( input = "" ) { super ( [ ] ) ; this . _valueStr = input ; const inputBuffer = String . UTF8 . encode ( input ) ; const u8Input = Uint8Array . wrap ( inputBuffer ) ; for ( let i = 0 ; i < u8Input . length ; i ++ ) { this . values [ i ] = u8Input [ i ] ; } } description param bytes param index populateFromBytes ( bytes , index = 0 ) { this . _valueStr = ScaleString . _computeValueStr ( bytes , index ) ; const inputBuffer = String . UTF8 . encode ( this . _valueStr ) ; const u8Input = Uint8Array . wrap ( inputBuffer ) ; for ( let i = 0 ; i < u8Input . length ; i ++ ) { this . values [ i ] = u8Input [ i ] ; } } description toString ( ) { return this . _valueStr ; } param bytes param index static _computeValueStr ( bytes , index = 0 ) { const len = Bytes . decodeCompactInt ( bytes , index ) ; const bytesLength = i32 ( len . value ) ; if ( bytesLength != 0 ) { const stringStart = i32 ( len . decBytes ) ; assert ( bytes . length - index - len . decBytes >= 1 , "ScaleString: Incorrectly encoded input" ) ; const buff = new Uint8Array ( bytesLength ) ; Bytes . copyToTyped ( bytes , buff , 0 , index + stringStart ) ; return String . UTF8 . decode ( buff . buffer ) ; } else { return "" ; } } description static fromU8a ( input , index = 0 ) { return new ScaleString ( ScaleString . _computeValueStr ( input , index ) ) ; } @ inline @ operator ( "==" ) static eq ( a , b ) { return a . eq ( b ) ; } @ inline @ operator ( "!=" ) static notEq ( a , b ) { return a . notEq ( b ) ; } }