author jdiaz5513
  import initTrace from "debug" ; import { decodeUtf8 , encodeUtf8 } from "../../util" ; import { ListElementSize } from "../list-element-size" ; import { List , initList } from "./list" ; import { Pointer , validate , isNull , getContent , erase } from "./pointer" ; import { PointerType } from "./pointer-type" ; const trace = initTrace ( "capnp:text" ) ; trace ( "load" ) ; export class Text extends List < string > { static fromPointer ( pointer ) { validate ( PointerType . LIST , pointer , ListElementSize . BYTE ) ; return textFromPointerUnchecked ( pointer ) ; } param { number } index returns { string } get ( index = 0 ) { if ( index !== 0 ) { trace ( "Called get() on %s with a strange index (%d)." , this , index ) ; } if ( isNull ( this ) ) return "" ; const c = getContent ( this ) ; return decodeUtf8 ( new Uint8Array ( c . segment . buffer , c . byteOffset + index , this . getLength ( ) - index ) ) ; } returns { number } getLength ( ) { return super . getLength ( ) - 1 ; } param { number } index param { string } value returns { void } set ( index , value ) { if ( index !== 0 ) { trace ( "Called set() on %s with a strange index (%d)." , this , index ) ; } const src = encodeUtf8 ( value ) ; const dstLength = src . byteLength + index ; let c ; let original ; if ( ! isNull ( this ) ) { c = getContent ( this ) ; let originalLength = this . getLength ( ) ; if ( originalLength >= index ) { originalLength = index ; } else { trace ( "%d byte gap exists between original text and new text in %s." , index - originalLength , this ) ; } original = new Uint8Array ( c . segment . buffer . slice ( c . byteOffset , c . byteOffset + Math . min ( originalLength , index ) ) ) ; erase ( this ) ; } initList ( ListElementSize . BYTE , dstLength + 1 , this ) ; c = getContent ( this ) ; const dst = new Uint8Array ( c . segment . buffer , c . byteOffset , dstLength ) ; if ( original ) dst . set ( original ) ; dst . set ( src , index ) ; } toString ( ) { return `Text_${ super . toString ( ) }` ; } } function textFromPointerUnchecked ( pointer ) { return new Text ( pointer . segment , pointer . byteOffset , pointer . _capnp . depthLimit ) ; }