15.6.2.1909.40 NAME>STRING name-to-string TOOLS EXT

( nt -- c-addr u )

NAME>STRING returns the name of the word nt in the character string c-addr u. The case of the characters in the string is implementation-dependent. The buffer containing c-addr u may be transient and valid until the next invocation of NAME>STRING. A program shall not write into the buffer containing the resulting string.

See:

ContributeContributions

ruvavatar of ruv [374] `NAME>STRING` result is transientRequest for clarification2025-01-22 16:50:21

15.6.2.1909.40 NAME>STRING says:

The buffer containing c-addr u may be transient and valid until the next invocation of NAME>STRING.

What is the point of allowing the returned string to be in a transient buffer? Which implementation approach benefits from that?

In typical implementations, the lifetime of the returned string is the same as the lifetime of nt.

AntonErtlavatar of AntonErtl

I think the idea was to allow systems that store definition names in a representation other than that returned by NAME>STRING. One example would be the fig-Forth representation of names, which sets the high bit of the last byte (fig-Forth only supports names in ASCII).

Now that we have had NAME>STRING in the standard for a decade, we can look at the systems that actually implement this word. If they all return a name that lives as long as the definition, we could enhance this word by giving that guarantee. But who will examine the systems and make the proposal?

Closed

ruvavatar of ruv

I have checked. There are about 22 Forth systems on GitHub that provide name>string implemented in Forth (see the search results).

Among these systems there is only one system, namely solo-forth (description: "Standard Forth system for ZX Spectrum 128 and compatible computers, with disk drives"), in which the word name>string returns a string in a transit buffer. This system has to copy the resulting string into the transient buffer because the header space and the data space are located in different address spaces.

ruvavatar of ruv

So, the only advantage of a transient result is that it allows to save memory in some cases. And it only makes sense when saving 10-100 KiB of memory (say, 8% of compiled code size) matters.

An example of approach that can benefit is the use of a trie data structure (prefix tree) to implement efficient searching of word lists. This data structure does not need to store entire strings. Therefore, to save memory, a transient string for a word name can be constructed each time it is needed.

Reply New Version