Strings and Runes in Fortress
Our first program will demonstrate string handling and rune concepts in Fortress. Here’s the full source code:
In this Fortress program, we’re working with strings and Unicode characters (runes). Fortress treats strings as sequences of Unicode code points.
We start by declaring a string s
containing Thai characters. The size()
method gives us the length of the string in code units (which may not equal the number of visible characters for non-ASCII text).
We then iterate over each code unit in the string, printing its hexadecimal representation. This shows the raw bytes that make up the UTF-16 encoding of the string.
To count the actual number of Unicode characters (code points), we use the codePoints()
method, which returns an array of code points.
We demonstrate two ways of iterating over the code points in the string:
- Using a for loop with
zipWithIndex()
to get both the code point and its starting index. - Using explicit iteration with
codePointAt()
, which returns both the code point and its width in code units.
The examineRune
function demonstrates how to compare a code point with specific Unicode characters.
Here’s an example of what the output might look like:
Note that Fortress, being a more academic language, might have different conventions or libraries for some of these operations. This translation attempts to capture the spirit of the original code while using Fortress-like syntax and idioms.