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