Overview
"Strings" are just bits of text. This sentence is a "string". Most programming languages have some way to deal with strings, and Reva is no exception. Few other languages have the variety of strings that Reva has, and here we'll try to explain the how and why.Kinds of strings
There are four varieties of strings in Reva:- Forth strings
- Counted strings
- Long strings, and
- NUL terminated strings
Forth strings
This is the traditional kind of string which (I think) every Forth dialect supports. It is also the "native" string in Reva. Such a string consists of an (address, length) pair on the stack. Because the "length" is a cell, these kinds of strings may be any size up to 232-1 bytes. That's 4 Gigabytes, which is probably much bigger than your computer could really give you!Counted strings
This is another traditional kind of string. It consists only of an address on the stack. The first byte the address points to is a "count", telling how long the string is. The word count takes the address of a "cstring" and converts it to a "string" (e.g. a Forth string). Many other Forths have this form of string, as it is also mentioned in the ANS standard. It is limited in length to 255 bytes, which is adequate for most uses.A counted string is easily stored using the place word. One appends to it using +place. For example:
" Hi there" pad place " , my dear!" pad +place
After this, typing:
pad count type
will yield the string Hi there, my dear!
Long strings
This is the same as a "cstring", but uses a cell as the count; therefore it can handle up to 4G. Use lcount to convert it to a "string"NUL terminated strings
These are the kind of strings that "C" programmers are used to: an address which points to the first character of a run of characters, the last of which is ASCII NUL (all bits zero). All strings created by Reva are also NUL terminated, for your convenience in passing to external libraries written in "C" or similar languages. Use the word zcount to convert it to a "string".Creating strings
There are quite a few ways in which you might create a string:- Use the double-quote word " . As in "C", you can insert a double-quote by prefixing it with a backslash. You can use the double-quote from the Reva interpreter prompt, for up to eight distinct strings at a time of up to 256 characters each (the ninth string will overwrite the first one). If you use double-quote inside a colon-definition, the string data are placed in a special location and code to generate the string at run-time is compiled.
" This is a string" " This has a \\ backslash"
- quote creates a string of arbitrary length which is just as it was entered. This word does not NUL terminate the string, so do be aware of that.
- create a buffer and store string data there:
create somestring 1000 allot | can hold up to 996 as an lstring ... | do something to store string data there ... create otherstring " store this there" asciiz, | stores a string to "here"
- Read in a file entirely as a string:
" somefile.txt" slurp