Strings and Runes in Ada

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.

with Ada.Text_IO;
with Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
with Ada.Wide_Wide_Characters.Handling;

procedure Strings_And_Runes is
   use Ada.Text_IO;
   use Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
   use Ada.Wide_Wide_Characters.Handling;

   -- S is a Wide_Wide_String constant assigned a literal value
   -- representing the word "hello" in the Thai language.
   S : constant Wide_Wide_String := "สวัสดี";
begin
   -- Print the length of the string in bytes
   Put_Line ("Len: " & Integer'Image (Encode (S)'Length));

   -- Print the hexadecimal representation of each byte
   Put ("Hex: ");
   for B of Encode (S) loop
      Put (Integer'Image (Character'Pos (B)) & " ");
   end loop;
   New_Line;

   -- Count the number of characters (equivalent to runes)
   Put_Line ("Character count: " & Integer'Image (S'Length));

   -- Iterate over each character and its position
   for I in S'Range loop
      Put_Line (Wide_Wide_Character'Image (S (I)) & " starts at " & Integer'Image (I));
   end loop;

   New_Line;
   Put_Line ("Examining characters:");

   -- Demonstrate passing a Wide_Wide_Character to a function
   for C of S loop
      Examine_Character (C);
   end loop;
end Strings_And_Runes;

procedure Examine_Character (C : Wide_Wide_Character) is
   use Ada.Text_IO;
begin
   -- We can compare a Wide_Wide_Character value directly
   if C = 't' then
      Put_Line ("found tee");
   elsif C = '' then
      Put_Line ("found so sua");
   end if;
end Examine_Character;

This Ada program demonstrates several concepts related to strings and Unicode characters:

  1. We use Wide_Wide_String to represent a Unicode string, which is similar to a string of runes.

  2. 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.

  3. We iterate over the string in two ways: by bytes (after encoding to UTF-8) and by characters (Wide_Wide_Characters).

  4. 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:

$ gnatmake strings_and_runes.adb
$ ./strings_and_runes

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.