1 \ ** ficl/softwords/string.fr
2 \ A useful dynamic string class
3 \ John Sadler 14 Sep 1998
6 \ counted string, buffer sized dynamically
9 \ s" arf arf!!" str --> set
10 \ s" woof woof woof " str --> cat
13 \ $FreeBSD: src/sys/boot/ficl/softwords/string.fr,v 1.1.2.1 2000/07/06 23:51:46 obrien Exp $
15 .( loading ficl string class ) cr
18 object subclass c-string
24 : get-count ( 2this -- count ) c-string => .count c-4byte => get ;
25 : set-count ( count 2this -- ) c-string => .count c-4byte => set ;
27 : ?empty ( 2this -- flag ) --> get-count 0= ;
29 : get-buflen ( 2this -- len ) c-string => .buflen c-4byte => get ;
30 : set-buflen ( len 2this -- ) c-string => .buflen c-4byte => set ;
32 : get-buf ( 2this -- ptr ) c-string => .buf c-ptr => get-ptr ;
33 : set-buf { ptr len 2this -- }
34 ptr 2this c-string => .buf c-ptr => set-ptr
35 len 2this c-string => set-buflen
38 \ set buffer to null and buflen to zero
39 : clr-buf ( 2this -- )
40 0 0 2over c-string => set-buf
41 0 -rot c-string => set-count
44 \ free the buffer if there is one, set buf pointer to null
45 : free-buf { 2this -- }
46 2this c-string => get-buf
49 abort" c-string free failed"
50 2this c-string => clr-buf
54 \ guarantee buffer is large enough to hold size chars
55 : size-buf { size 2this -- }
56 size 0< abort" need positive size for size-buf"
58 2this --> free-buf exit
61 \ force buflen to be a positive multiple of min-buf chars
62 c-string => min-buf size over / 1+ * chars to size
64 \ if buffer is null, allocate one, else resize it
65 2this --> get-buflen 0=
69 size 2this --> set-buf
70 size 2this --> set-buflen
74 size 2this --> get-buflen > if
75 2this --> get-buf size resize
77 size 2this --> set-buf
81 : set { c-addr u 2this -- }
84 c-addr 2this --> get-buf u move
87 : get { 2this -- c-addr u }
92 \ append string to existing one
93 : cat { c-addr u 2this -- }
94 2this --> get-count u + dup >r
96 c-addr 2this --> get-buf 2this --> get-count + u move
97 r> 2this --> set-count
101 2this --> ?empty if ." (empty) " exit endif
102 2this --> .buf --> get-ptr
103 2this --> .count --> get
107 : compare ( 2string 2this -- n )
114 : hashcode ( 2this -- hashcode )
118 \ destructor method (overrides object --> free)
119 : free ( 2this -- ) 2dup c-string => free-buf object => free ;
123 c-string subclass c-hashstring
124 c-2byte obj: .hashcode
126 : set-hashcode { 2this -- }
127 2this --> super --> hashcode
128 2this --> .hashcode --> set
131 : get-hashcode ( 2this -- hashcode )
132 --> .hashcode --> get
135 : set ( c-addr u 2this -- )
136 2swap 2over --> super --> set
140 : cat ( c-addr u 2this -- )
141 2swap 2over --> super --> cat