afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeS" FICL_WANT_OOP" ENVIRONMENT? drop [if]
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome\ ** ficl/softwords/string.fr
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome\ A useful dynamic string class
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome\ John Sadler 14 Sep 1998
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome\
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome\ ** C - S T R I N G
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome\ counted string, buffer sized dynamically
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome\ Creation example:
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome\ c-string --> new str
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome\ s" arf arf!!" str --> set
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome\ s" woof woof woof " str --> cat
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome\ str --> type cr
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome\
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome.( loading ficl string class ) cr
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomealso oop definitions
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomeobject subclass c-string
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome c-cell obj: .count
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome c-cell obj: .buflen
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome c-ptr obj: .buf
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome 32 constant min-buf
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : get-count ( 2:this -- count ) my=[ .count get ] ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : set-count ( count 2:this -- ) my=[ .count set ] ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : ?empty ( 2:this -- flag ) --> get-count 0= ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : get-buflen ( 2:this -- len ) my=[ .buflen get ] ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : set-buflen ( len 2:this -- ) my=[ .buflen set ] ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : get-buf ( 2:this -- ptr ) my=[ .buf get-ptr ] ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : set-buf { ptr len 2:this -- }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ptr this my=[ .buf set-ptr ]
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome len this my=> set-buflen
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome \ set buffer to null and buflen to zero
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : clr-buf ( 2:this -- )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome 0 0 2over my=> set-buf
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome 0 -rot my=> set-count
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome \ free the buffer if there is one, set buf pointer to null
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : free-buf { 2:this -- }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome this my=> get-buf
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ?dup if
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome free
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome abort" c-string free failed"
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome this my=> clr-buf
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome endif
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome \ guarantee buffer is large enough to hold size chars
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : size-buf { size 2:this -- }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome size 0< abort" need positive size for size-buf"
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome size 0= if
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome this --> free-buf exit
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome endif
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome \ force buflen to be a positive multiple of min-buf chars
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome my=> min-buf size over / 1+ * chars to size
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome \ if buffer is null, allocate one, else resize it
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome this --> get-buflen 0=
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome size allocate
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome abort" out of memory"
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome size this --> set-buf
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome size this --> set-buflen
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome exit
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome endif
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome size this --> get-buflen > if
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome this --> get-buf size resize
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome abort" out of memory"
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome size this --> set-buf
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome endif
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : set { c-addr u 2:this -- }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome u this --> size-buf
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome u this --> set-count
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome c-addr this --> get-buf u move
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : get { 2:this -- c-addr u }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome this --> get-buf
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome this --> get-count
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome \ append string to existing one
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : cat { c-addr u 2:this -- }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome this --> get-count u + dup >r
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome this --> size-buf
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome c-addr this --> get-buf this --> get-count + u move
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome r> this --> set-count
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : type { 2:this -- }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome this --> ?empty if ." (empty) " exit endif
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome this --> .buf --> get-ptr
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome this --> .count --> get
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome type
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : compare ( 2string 2:this -- n )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome --> get
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome 2swap
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome --> get
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome 2swap compare
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : hashcode ( 2:this -- hashcode )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome --> get hash
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome \ destructor method (overrides object --> free)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : free ( 2:this -- ) 2dup --> free-buf object => free ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomeend-class
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomec-string subclass c-hashstring
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome c-2byte obj: .hashcode
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : set-hashcode { 2:this -- }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome this --> super --> hashcode
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome this --> .hashcode --> set
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : get-hashcode ( 2:this -- hashcode )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome --> .hashcode --> get
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : set ( c-addr u 2:this -- )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome 2swap 2over --> super --> set
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome --> set-hashcode
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome : cat ( c-addr u 2:this -- )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome 2swap 2over --> super --> cat
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome --> set-hashcode
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomeend-class
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomeprevious definitions
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome[endif]