Our first example demonstrates strings and runes in Ada. Ada uses the concept of “Wide_Character” and “Wide_Wide_Character” to represent Unicode characters, which are similar to runes in other languages.
This Ada program demonstrates several concepts related to strings and Unicode characters:
We use Wide_Wide_String to represent a Unicode string, which is similar to a string of runes.
The Encode function from Ada.Strings.UTF_Encoding.Wide_Wide_Strings is used to convert the Wide_Wide_String to a UTF-8 encoded string, allowing us to get the byte representation.
We iterate over the string in two ways: by bytes (after encoding to UTF-8) and by characters (Wide_Wide_Characters).
The Examine_Character procedure demonstrates how to compare Wide_Wide_Characters, which is similar to comparing runes.
To run this program, save it as strings_and_runes.adb and compile it using an Ada compiler like GNAT:
The output will show the length of the string in bytes, its hexadecimal representation, the number of characters, and information about each character’s position and value.
This example illustrates how Ada handles Unicode strings and characters, providing functionality similar to Go’s string and rune types.