🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.forth
Article #269

$trings (was: Braindamaged strings)

#269
From: Anton Ertl
Date: Tue, 01 Dec 2026 08:09
57 lines
3405 bytes
albert@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
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: https://forth-standard.org/
EuroForth 2026 CFP: http://www.euroforth.org/ef26/cfp.html
Technical Details
Message-ID: <2026Dec1.090920@mips.complang.tuwien.ac.at>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <nnd$2790af4d$71f56696@06eb3ea8c34cc53c>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: $trings (was: Braindamaged strings)
Date: Tue, 01 Dec 2026 08:09:20 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 57
Message-ID: <2026Dec1.090920@mips.complang.tuwien.ac.at>
References: <nnd$2790af4d$71f56696@06eb3ea8c34cc53c>
Injection-Date: Tue, 01 Dec 2026 08:45:08 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1483841"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX196Vj15uP2jy3M+wresY/Bd"; posting-host="8e35d99638f608aa487eea8644675695"
Cancel-Lock: sha1:6rc/HM9K+f4VBNLfC5IbCXkv1U8= sha256:dHTt5PqOUm1PCcOuyEqdauyJPxVnLb4pNH0UCO+HHko=
	sha1:SnFDJKcErYESy8oovlFy5mzZ1+A= sha256:AVo+yDGM5oHPlbBKUPPmb1X3TYWlNm8iAHuhsMEgTFk=
X-newsreader: xrn 10.11