1 S" FICL_WANT_OOP" ENVIRONMENT? drop [if]
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
15 .( loading ficl string class ) cr
18 object subclass c-string
24 : get-count ( 2:this -- count ) my=[ .count get ] ;
25 : set-count ( count 2:this -- ) my=[ .count set ] ;
27 : ?empty ( 2:this -- flag ) --> get-count 0= ;
29 : get-buflen ( 2:this -- len ) my=[ .buflen get ] ;
30 : set-buflen ( len 2:this -- ) my=[ .buflen set ] ;
32 : get-buf ( 2:this -- ptr ) my=[ .buf get-ptr ] ;
33 : set-buf { ptr len 2:this -- }
34 ptr this my=[ .buf set-ptr ]
35 len this my=> set-buflen
38 \ set buffer to null and buflen to zero
39 : clr-buf ( 2:this -- )
40 0 0 2over my=> set-buf
44 \ free the buffer if there is one, set buf pointer to null
45 : free-buf { 2:this -- }
49 abort" c-string free failed"
54 \ guarantee buffer is large enough to hold size chars
55 : size-buf { size 2:this -- }
56 size 0< abort" need positive size for size-buf"
58 this --> free-buf exit
61 \ force buflen to be a positive multiple of min-buf chars
62 my=> min-buf size over / 1+ * chars to size
64 \ if buffer is null, allocate one, else resize it
65 this --> get-buflen 0=
70 size this --> set-buflen
74 size this --> get-buflen > if
75 this --> get-buf size resize
81 : set { c-addr u 2:this -- }
84 c-addr this --> get-buf u move
87 : get { 2:this -- c-addr u }
92 \ append string to existing one
93 : cat { c-addr u 2:this -- }
94 this --> get-count u + dup >r
96 c-addr this --> get-buf this --> get-count + u move
101 this --> ?empty if ." (empty) " exit endif
102 this --> .buf --> get-ptr
103 this --> .count --> get
107 : compare ( 2string 2:this -- n )
114 : hashcode ( 2:this -- hashcode )
118 \ destructor method (overrides object --> free)
119 : free ( 2:this -- ) 2dup --> free-buf object => free ;
123 c-string subclass c-hashstring
124 c-2byte obj: .hashcode
126 : set-hashcode { 2:this -- }
127 this --> super --> hashcode
128 this --> .hashcode --> set
131 : get-hashcode ( 2:this -- hashcode )
132 --> .hashcode --> get
135 : set ( c-addr u 2:this -- )
136 2swap 2over --> super --> set
140 : cat ( c-addr u 2:this -- )
141 2swap 2over --> super --> cat