Strings
From RevaWiki
Return to the main manual page
Glossary entry
Contents |
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 a 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 " like so:
- " This is a string"
- Note that you can insert a double-quote by prefixing it with a backslash like so:
- " This has a \\ backslash"
- You can use the double-quote word from the ok> prompt, as well as from within a source file or inside a word definition. However:
- An interactively created string is allocated from a rotating buffer holding up to eight distinct strings. If you create a ninth, you will notice that the first string got overwritten. The limit in size is 256 bytes.
- A compiled string is compiled in-line into the word which uses it. It is also limited to 256 bytes in length.
- 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
