String Formatting in Ada

Our first program will demonstrate string formatting. Here’s the full source code:

with Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Float_Text_IO;

procedure String_Formatting is
   type Point is record
      X, Y : Integer;
   end record;

   P : Point := (1, 2);

   procedure Put_Line_With_Format(Format : String; Value : String) is
   begin
      Ada.Text_IO.Put(Format & ": ");
      Ada.Text_IO.Put_Line(Value);
   end Put_Line_With_Format;

begin
   -- Basic record printing
   Ada.Text_IO.Put_Line("struct1: (" & Integer'Image(P.X) & "," & Integer'Image(P.Y) & ")");

   -- Printing record with field names
   Ada.Text_IO.Put_Line("struct2: (X:" & Integer'Image(P.X) & ", Y:" & Integer'Image(P.Y) & ")");

   -- Ada doesn't have a direct equivalent to Go's %#v, so we'll use a similar representation
   Ada.Text_IO.Put_Line("struct3: Point'(X =>" & Integer'Image(P.X) & ", Y =>" & Integer'Image(P.Y) & ")");

   -- Printing the type name
   Ada.Text_IO.Put_Line("type: Point");

   -- Formatting booleans
   Put_Line_With_Format("bool", Boolean'Image(True));

   -- Formatting integers
   Put_Line_With_Format("int", Integer'Image(123));

   -- Ada doesn't have a built-in binary representation, so we'll use decimal
   Put_Line_With_Format("decimal", Integer'Image(14));

   -- Printing character corresponding to an integer
   Put_Line_With_Format("char", Character'Image(Character'Val(33)));

   -- Ada doesn't have built-in hex formatting, so we'll use decimal
   Put_Line_With_Format("decimal", Integer'Image(456));

   -- Formatting floats
   Ada.Float_Text_IO.Put(Item => 78.9, Fore => 1, Aft => 6, Exp => 0);
   Ada.Text_IO.New_Line;

   -- Scientific notation
   Ada.Float_Text_IO.Put(Item => 123400000.0, Fore => 1, Aft => 6, Exp => 3);
   Ada.Text_IO.New_Line;

   -- String formatting
   Put_Line_With_Format("str1", """string""");

   -- Ada doesn't have a direct equivalent to Go's %q, so we'll use the same as str1
   Put_Line_With_Format("str2", """string""");

   -- Ada doesn't have built-in hex string representation, so we'll use the string as-is
   Put_Line_With_Format("str3", "hex this");

   -- Ada doesn't have a direct way to print pointer addresses
   Ada.Text_IO.Put_Line("pointer: <address>");

   -- Width formatting for integers
   Ada.Integer_Text_IO.Put(Item => 12, Width => 6);
   Ada.Integer_Text_IO.Put(Item => 345, Width => 6);
   Ada.Text_IO.New_Line;

   -- Width and precision formatting for floats
   Ada.Float_Text_IO.Put(Item => 1.2, Fore => 3, Aft => 2, Exp => 0);
   Ada.Float_Text_IO.Put(Item => 3.45, Fore => 3, Aft => 2, Exp => 0);
   Ada.Text_IO.New_Line;

   -- Left-justified float formatting
   Ada.Float_Text_IO.Put(Item => 1.2, Fore => 0, Aft => 2, Exp => 0);
   Ada.Text_IO.Put("    ");
   Ada.Float_Text_IO.Put(Item => 3.45, Fore => 0, Aft => 2, Exp => 0);
   Ada.Text_IO.Put("    ");
   Ada.Text_IO.New_Line;

   -- Width formatting for strings
   Ada.Text_IO.Put(Item => "foo", Width => 6);
   Ada.Text_IO.Put(Item => "b", Width => 6);
   Ada.Text_IO.New_Line;

   -- Left-justified string formatting
   Ada.Text_IO.Put(Item => "foo", Width => -6);
   Ada.Text_IO.Put(Item => "b", Width => -6);
   Ada.Text_IO.New_Line;

   -- Ada doesn't have a direct equivalent to Sprintf, but we can use string concatenation
   declare
      S : constant String := "sprintf: a " & "string";
   begin
      Ada.Text_IO.Put_Line(S);
   end;

   -- Writing to standard error
   Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error, "io: an error");
end String_Formatting;

To run the program, save it as string_formatting.adb and use the Ada compiler (e.g., GNAT):

$ gnatmake string_formatting.adb
$ ./string_formatting

This will compile the Ada program and then run it, producing output similar to the original Go program. Note that Ada’s string formatting capabilities are different from Go’s, so some adjustments were made to achieve similar results.

Ada doesn’t have a direct equivalent to Go’s fmt package with its various formatting options, so we’ve used Ada’s standard input/output packages and created custom formatting where needed. The output will be similar, but not identical, to the Go version due to language differences.