2 \ ** ficl/softwords/string.fr
3 \ A useful dynamic string class
4 \ John Sadler 14 Sep 1998
7 \ counted string, buffer sized dynamically
10 \ s" arf arf!!" str --> set
11 \ s" woof woof woof " str --> cat
14 \ $FreeBSD: src/sys/boot/ficl/softwords/string.fr,v 1.2 2001/04/29 02:36:36 dcs Exp $
15 \ $DragonFly: src/sys/boot/ficl/softwords/string.fr,v 1.3 2003/11/10 06:08:34 dillon Exp $
19 object subclass c-string
25 : get-count ( 2:this -- count ) my=[ .count get ] ;
26 : set-count ( count 2:this -- ) my=[ .count set ] ;
28 : ?empty ( 2:this -- flag ) --> get-count 0= ;
30 : get-buflen ( 2:this -- len ) my=[ .buflen get ] ;
31 : set-buflen ( len 2:this -- ) my=[ .buflen set ] ;
33 : get-buf ( 2:this -- ptr ) my=[ .buf get-ptr ] ;
34 : set-buf { ptr len 2:this -- }
35 ptr this my=[ .buf set-ptr ]
36 len this my=> set-buflen
39 \ set buffer to null and buflen to zero
40 : clr-buf ( 2:this -- )
41 0 0 2over my=> set-buf
45 \ free the buffer if there is one, set buf pointer to null
46 : free-buf { 2:this -- }
50 abort" c-string free failed"
55 \ guarantee buffer is large enough to hold size chars
56 : size-buf { size 2:this -- }
57 size 0< abort" need positive size for size-buf"
59 this --> free-buf exit
62 \ force buflen to be a positive multiple of min-buf chars
63 my=> min-buf size over / 1+ * chars to size
65 \ if buffer is null, allocate one, else resize it
66 this --> get-buflen 0=
71 size this --> set-buflen
75 size this --> get-buflen > if
76 this --> get-buf size resize
82 : set { c-addr u 2:this -- }
85 c-addr this --> get-buf u move
88 : get { 2:this -- c-addr u }
93 \ append string to existing one
94 : cat { c-addr u 2:this -- }
95 this --> get-count u + dup >r
97 c-addr this --> get-buf this --> get-count + u move
102 this --> ?empty if ." (empty) " exit endif
103 this --> .buf --> get-ptr
104 this --> .count --> get
108 : compare ( 2string 2:this -- n )
115 : hashcode ( 2:this -- hashcode )
119 \ destructor method (overrides object --> free)
120 : free ( 2:this -- ) 2dup --> free-buf object => free ;
124 c-string subclass c-hashstring
125 c-2byte obj: .hashcode
127 : set-hashcode { 2:this -- }
128 this --> super --> hashcode
129 this --> .hashcode --> set
132 : get-hashcode ( 2:this -- hashcode )
133 --> .hashcode --> get
136 : set ( c-addr u 2:this -- )
137 2swap 2over --> super --> set
141 : cat ( c-addr u 2:this -- )
142 2swap 2over --> super --> cat