Reva: strings

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:
  1. Forth strings
  2. Counted strings
  3. Long strings, and
  4. 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: