Reva: Strings
User Manual
for Reva Forth

Strings

This article is a work in progress.

The Basics

Here is the basic operation to deal with a string:

 " Hello world!"

It looks a lot like a simple quoted string (which is intentional) but note that there is a space after the first quotation mark. In fact, that first quotation mark is a word that scans everything on the input until it reaches another quotation mark. Now puting those letters on the stack would be an option, but not the most practical. Instead, Reva leaves 2 numbers on the stack: the address where this string starts in memory, and the number of characters it contains. You'll see that a lot of words need "a" and "n" on the stack (Address and Number) which means they need a string. One of them is type:

 " Hello world!" type

Easy to guess, this word simply type the string (Be careful because it consumes the numbers). But this is such a common thing to do that there is another word for that:

 ." Hello world!"

." (say dot-quote) write everything until the next quotation mark and leaves nothing on the stack.

Working with Strings

There are plenty of other tools to work with them.

...

Many kind of Strings

Usualy in other languages, strings are NUL terminated. It means that the only reference to a string is its address. You know when you reach the end because there is one more byte which is NUL (NUL or zero is not an ascii character). In Reva you can do whatever you want but the prefered way is the counted string. Instead of adding a NUL at the end, to make a counted string you add the count at the begining.

There are pros and cons for both. The counted string saves time by keeping the length of that string recorded (which is usualy a frequent operation) but that length is limited by the space you dedicate to that number. For example, a regular counted string keeps the number in one byte, which means that the number can't be above 255 characters. (note: This is how words are recorded in the dictionary)

The counted string sounds like a good option because even with NUL terminated strings, one tends to limit the length and to save this value in a variable. Nevertheless you'll find in Reva everything you need to work with larger kind of counted strings or NUL terminated strings.

Before presenting those words, I'd like to avoid a confusion. What we create with " is not a counted string, it is just the address in memory where what you've written is, and it's been counted so that you have the count on the stack. It is just the unified way to deal with string operation we've seen in the previous chapters. A counted string is really when you save that string somewhere with the count at the begining. The world "hello" would be saved like that:


 +---+-----+-----+-----+-----+-----+
 | 5 | 104 | 101 | 108 | 108 | 111 |
 +---+-----+-----+-----+-----+-----+

5 is the count and numbers after are the ascii values for each letter.