--  Verada.Strings.Unbounded: strings of any length, stored on the heap
--  and freed where the object dies (a reclaimed type). Unlike
--  Ada.Strings.Unbounded, the type is limited: there are no copies, and a value is given or
--  changed by the procedures here. The model of an object is its String value, To_String.
--  Only memory limits the length (Verada.Storage): Append raises Storage_Error when memory
--  cannot hold the result.
with Verada.Storage;

package Verada.Strings.Unbounded is
   type Unbounded_String is limited private
     with Default_Initial_Condition => Length (Unbounded_String) = 0,
          Finalizable => (Finalize => Free, Relaxed_Finalization => True);

   function Length (Source : Unbounded_String) return Natural
     with Global => null,
          Post => Length'Result < Verada.Storage.Length_Bound;

   function To_String (Source : Unbounded_String) return String
     with Global => null,
          Post => To_String'Result'Length = Length (Source)
                  and then (To_String'Result'Length = 0 or else To_String'Result'First = 1);

   function Element (Source : Unbounded_String; Index : Positive) return Character
     with Global => null,
          Pre => Index <= Length (Source),
          Post => Element'Result = To_String (Source) (Index);

   procedure Set (Target : in out Unbounded_String; Source : String)
     with Global => null,
          Post => To_String (Target) = Source;

   procedure Append (Source : in out Unbounded_String; New_Item : String)
     with Global => null,
          Post => To_String (Source) = To_String (Source)'Old & New_Item;

   procedure Append (Source : in out Unbounded_String; New_Item : Character)
     with Global => null,
          Post => To_String (Source) = To_String (Source)'Old & New_Item;

   --  Move (From, To): To takes From's value, From becomes empty
   procedure Move (From : in out Unbounded_String; To : in out Unbounded_String)
     with Global => null,
          Post => To_String (To) = To_String (From)'Old and then Length (From) = 0;

   procedure Free (Source : in out Unbounded_String)
     with Global => null,
          Post => Length (Source) = 0;
private
   type String_Access is access String;

   --  Data is null for the empty string; otherwise it designates the characters, from 1,
   --  fewer than Length_Bound of them (each operation reserves the room first); the invariant is on the full view, where Verada takes a private type's invariant (contracts), and
   --  reads the representation directly (it may not call the type's visible operations)
   type Unbounded_String is limited record
      Data : String_Access;
   end record
     with Type_Invariant =>
       Unbounded_String.Data = null
         or else (Unbounded_String.Data'First = 1 and then Unbounded_String.Data'Length > 0
                  and then Unbounded_String.Data'Length < Verada.Storage.Length_Bound);

   function Length (Source : Unbounded_String) return Natural is
     (if Source.Data = null then 0 else Source.Data'Length);

   function To_String (Source : Unbounded_String) return String is
     (if Source.Data = null then "" else Source.Data.all);

   function Element (Source : Unbounded_String; Index : Positive) return Character is
     (Source.Data (Index));
end Verada.Strings.Unbounded;
