albe…@spenarnc.xs4all.nl writes:
>In this day and age it is time to terminate the use of characters >to store a string count.
Forthunately, the designers of Forth-94 already recognized this over three decades ago, and focussed on the c-addr u representation, with only a few words existing for dealing with the counted-string legacy.
>With great pleasure I see that Gforth has adopted the use of a cell >to store the count of a string, officially. >$@ replaces COUNT.
Not really, as explained below.
>$! has no equivalent in brain damaged strings
Exactly. The $tring implementation differs from COUNTed strings in more than just the count size. The single-cell $tring representation does not point to the count cell (as it would, if $trings were just COUNTed strings with bigger counts). Instead, what you have on the stack is the address of a cell (e.g., it may come from a variable). E.g., here's how you use it: variable mystring \ 0 mystring ! \ automatic for variables in Gforth s" bla" mystring $! mystring $@ type What's remarkable here is that you do not have to allocate space for the string data explicitly, allocating a cell is enough. $! ALLOCATEs space for the string data and its length, and stores the address of that memory in the given cell. When the cell is overwritten by another $-word, e.g., by another $tring, the old string that the cell points to is freed. So it is important to initialize these cells to 0 at the beginning, and then only work on them with $-words. It's also important that any c-addr u pairs derived from the cell have been consumed at that point (they will become invalid once the string data has been freed). When you keep this discipline, the reward is that the storage allocation of strings becomes much easier: You only need to allocate a cell for any string you want to manage, and do not have to worry about the length of the string. You also do not need a string stack, the data stack holds the addresses of the cells just fine (but OTOH, you need to deal with those cells). - anton