--  A function's unconstrained local made from a call's result, then given to another call in
--  its return: the local's storage ends with the function, after the value it returns is
--  made (a callee that makes its result before it reads its argument read it freed before)
with Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
with Ada.Text_IO;
procedure Main is
   function Ticks (N : Natural) return Wide_Wide_String is
     (1 .. N => Wide_Wide_Character'Val (16#2713#))
     with Pre => N <= 1000;

   --  (its result is made first, then filled from its argument)
   function Framed (S : String) return String
     with Pre => S'Length <= 1000,
          Post => Framed'Result'Length = S'Length + 2
   is
      Result : String (1 .. S'Length + 2) := (others => '|');
   begin
      for I in S'Range loop
         Result (I - S'First + 2) := S (I);
      end loop;
      return Result;
   end Framed;

   function Encoded (N : Natural) return String
     with Pre => N <= 100
   is
      Wide : constant Wide_Wide_String := Ticks (N);
   begin
      return Ada.Strings.UTF_Encoding.Wide_Wide_Strings.Encode (Wide);
   end Encoded;

   --  (a result passed on to a further call)
   function Twice_Framed (N : Natural) return String
     with Pre => N <= 100,
          Post => Twice_Framed'Result'Length = N + 4
   is
      Local : constant String := (1 .. N => 'x');
   begin
      return Framed (Framed (Local));
   end Twice_Framed;

   --  (a local of a block in the function)
   function In_Block (N : Natural) return String
     with Pre => N <= 100
   is
   begin
      declare
         Local : constant String := Twice_Framed (N);
      begin
         return Framed (Local);
      end;
   end In_Block;
begin
   Ada.Text_IO.Put_Line ("encoded bytes:" & Natural'Image (Encoded (3)'Length));
   Ada.Text_IO.Put_Line (Twice_Framed (3));
   Ada.Text_IO.Put_Line (In_Block (2));
end Main;
