--  Ada.Strings.Unbounded (RM A.4.5): strings of any length, copied as other values are. An
--  Unbounded_String holds a Verada.Strings.Texts.Text, whose characters a copy shares and the
--  last holder frees, so the type is not limited. The model of a value is its String,
--  To_String, from 1; each operation's contract states its result in that model.
--  Only memory limits the length (Verada.Storage): an operation that would make a longer
--  string raises Storage_Error, as an allocation the heap cannot make does. Where the RM
--  raises Index_Error or Pattern_Error, a precondition requires what avoids it.
with Ada.Strings.Maps;
with Verada.Storage;
with Verada.Strings.Texts;

package Ada.Strings.Unbounded is
   use type Maps.Character_Mapping_Function;

   type Unbounded_String is private
     with Default_Initial_Condition => Length (Unbounded_String) = 0;

   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);

   Null_Unbounded_String : constant Unbounded_String;

   --  an access to a string, which Free deallocates (RM A.4.5(7)): pool-specific, as
   --  Verada frees only what an allocator made
   type String_Access is access String;

   procedure Free (X : in out String_Access)
     with Global => null,
          Post => X = null;

   --  the occurrence of Pattern after Source's first Done characters (at Source'First + Done),
   --  each character of Source mapped
   function Occurs_After (Source, Pattern : String; Done : Natural; Mapping : Maps.Character_Mapping) return Boolean is
     (Done <= Source'Length - Pattern'Length
      and then (for all K in 0 .. Pattern'Length - 1 =>
                  Maps.Value (Mapping, Source (Source'First + Done + K)) = Pattern (Pattern'First + K)))
     with Ghost, Global => null;

   --  how many times Pattern occurs after Source's first Done characters without overlapping,
   --  each found from the left
   function Count_After (Source, Pattern : String; Done : Natural; Mapping : Maps.Character_Mapping) return Natural is
     (if Done > Source'Length - Pattern'Length then 0
      elsif Occurs_After (Source, Pattern, Done, Mapping) then 1 + Count_After (Source, Pattern, Done + Pattern'Length, Mapping)
      else Count_After (Source, Pattern, Done + 1, Mapping))
     with Ghost, Global => null,
          Pre => Pattern'Length > 0 and then Done <= Source'Length,
          Post => Count_After'Result <= Source'Length - Done,
          Subprogram_Variant => (Decreases => Source'Length - Done);

   --  how many characters after Source's first Done characters are in Set (Test Inside) or
   --  not (Outside)
   function Count_In (Source : String; Set : Maps.Character_Set; Test : Membership; Done : Natural) return Natural is
     (if Done >= Source'Length then 0
      else (if Maps.Is_In (Source (Source'First + Done), Set) = (Test = Inside) then 1 else 0)
           + Count_In (Source, Set, Test, Done + 1))
     with Ghost, Global => null,
          Pre => Done <= Source'Length,
          Post => Count_In'Result <= Source'Length - Done,
          Subprogram_Variant => (Decreases => Source'Length - Done);

   --  A, B and C joined, from 1 (the RM's results of Replace_Slice, Insert and Overwrite,
   --  whatever their parts' bounds)
   function Joined (A, B, C : String) return String is
     (String'(for I in 1 .. A'Length + B'Length + C'Length =>
                (if I <= A'Length then A (A'First + (I - 1))
                 elsif I <= A'Length + B'Length then B (B'First + (I - 1 - A'Length))
                 else C (C'First + (I - 1 - A'Length - B'Length)))))
     with Global => null,
          Pre => Long_Long_Long_Integer (A'Length) + Long_Long_Long_Integer (B'Length) + Long_Long_Long_Integer (C'Length)
                 < Verada.Storage.Length_Bound,
          Post => Joined'Result'Length = A'Length + B'Length + C'Length
                  and then (Joined'Result'Length = 0 or else Joined'Result'First = 1);

   function To_Unbounded_String (Source : String) return Unbounded_String
     with Global => null,
          Post => To_String (To_Unbounded_String'Result) = Source;

   --  a string of Length characters (spaces)
   function To_Unbounded_String (Length : Natural) return Unbounded_String
     with Global => null,
          Post => Unbounded.Length (To_Unbounded_String'Result) = Length;

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

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

   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;

   function "&" (Left, Right : Unbounded_String) return Unbounded_String
     with Global => null,
          Post => To_String ("&"'Result) = To_String (Left) & To_String (Right);

   function "&" (Left : Unbounded_String; Right : String) return Unbounded_String
     with Global => null,
          Post => To_String ("&"'Result) = To_String (Left) & Right;

   function "&" (Left : String; Right : Unbounded_String) return Unbounded_String
     with Global => null,
          Post => To_String ("&"'Result) = Joined (Left, To_String (Right), "");

   function "&" (Left : Unbounded_String; Right : Character) return Unbounded_String
     with Global => null,
          Post => To_String ("&"'Result) = To_String (Left) & Right;

   function "&" (Left : Character; Right : Unbounded_String) return Unbounded_String
     with Global => null,
          Post => To_String ("&"'Result) = Joined (String'(1 => Left), To_String (Right), "");

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

   procedure Replace_Element (Source : in out Unbounded_String; Index : Positive; By : Character)
     with Global => null,
          Pre => Index <= Length (Source),
          Post => To_String (Source) = To_String (Source)'Old (1 .. Index - 1) & By & To_String (Source)'Old (Index + 1 .. Length (Source)'Old);

   --  the characters at Low .. High, with those bounds
   function Slice (Source : Unbounded_String; Low : Positive; High : Natural) return String
     with Global => null,
          Pre => Low <= Length (Source) + 1 and then High <= Length (Source),
          Post => Slice'Result'First = Low and then Slice'Result'Last = High
                  and then Slice'Result = To_String (Source) (Low .. High);

   function Unbounded_Slice (Source : Unbounded_String; Low : Positive; High : Natural) return Unbounded_String
     with Global => null,
          Pre => Low <= Length (Source) + 1 and then High <= Length (Source),
          Post => To_String (Unbounded_Slice'Result) = To_String (Source) (Low .. High);

   procedure Unbounded_Slice (Source : Unbounded_String; Target : out Unbounded_String; Low : Positive; High : Natural)
     with Global => null,
          Pre => Low <= Length (Source) + 1 and then High <= Length (Source),
          Post => To_String (Target) = To_String (Source) (Low .. High);

   function "=" (Left, Right : Unbounded_String) return Boolean
     with Global => null,
          Post => "="'Result = (To_String (Left) = To_String (Right));

   function "=" (Left : Unbounded_String; Right : String) return Boolean
     with Global => null,
          Post => "="'Result = (To_String (Left) = Right);

   function "=" (Left : String; Right : Unbounded_String) return Boolean
     with Global => null,
          Post => "="'Result = (Left = To_String (Right));

   function "<" (Left, Right : Unbounded_String) return Boolean
     with Global => null,
          Post => "<"'Result = (To_String (Left) < To_String (Right));

   function "<" (Left : Unbounded_String; Right : String) return Boolean
     with Global => null,
          Post => "<"'Result = (To_String (Left) < Right);

   function "<" (Left : String; Right : Unbounded_String) return Boolean
     with Global => null,
          Post => "<"'Result = (Left < To_String (Right));

   function "<=" (Left, Right : Unbounded_String) return Boolean
     with Global => null,
          Post => "<="'Result = (To_String (Left) <= To_String (Right));

   function "<=" (Left : Unbounded_String; Right : String) return Boolean
     with Global => null,
          Post => "<="'Result = (To_String (Left) <= Right);

   function "<=" (Left : String; Right : Unbounded_String) return Boolean
     with Global => null,
          Post => "<="'Result = (Left <= To_String (Right));

   function ">" (Left, Right : Unbounded_String) return Boolean
     with Global => null,
          Post => ">"'Result = (To_String (Left) > To_String (Right));

   function ">" (Left : Unbounded_String; Right : String) return Boolean
     with Global => null,
          Post => ">"'Result = (To_String (Left) > Right);

   function ">" (Left : String; Right : Unbounded_String) return Boolean
     with Global => null,
          Post => ">"'Result = (Left > To_String (Right));

   function ">=" (Left, Right : Unbounded_String) return Boolean
     with Global => null,
          Post => ">="'Result = (To_String (Left) >= To_String (Right));

   function ">=" (Left : Unbounded_String; Right : String) return Boolean
     with Global => null,
          Post => ">="'Result = (To_String (Left) >= Right);

   function ">=" (Left : String; Right : Unbounded_String) return Boolean
     with Global => null,
          Post => ">="'Result = (Left >= To_String (Right));

   --  the first (Forward) or last (Backward) place where Pattern occurs, each character of
   --  Source mapped, or 0 when it does not
   function Index (Source : Unbounded_String; Pattern : String; Going : Direction := Forward;
                   Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
     with Global => null,
          Pre => Pattern'Length > 0,
          Post => (if Index'Result = 0
                   then (for all D in 0 .. Length (Source) - Pattern'Length => not Occurs_After (To_String (Source), Pattern, D, Mapping))
                   else Index'Result <= Length (Source) - Pattern'Length + 1
                        and then Occurs_After (To_String (Source), Pattern, Index'Result - 1, Mapping)
                        and then (if Going = Forward
                                  then (for all D in 0 .. Index'Result - 2 => not Occurs_After (To_String (Source), Pattern, D, Mapping))
                                  else (for all D in Index'Result .. Length (Source) - Pattern'Length => not Occurs_After (To_String (Source), Pattern, D, Mapping))));

   --  as Index with a mapping, the mapping a function's
   function Index (Source : Unbounded_String; Pattern : String; Going : Direction := Forward;
                   Mapping : Maps.Character_Mapping_Function) return Natural
     with Global => null,
          Pre => Pattern'Length > 0 and then Mapping /= null,
          Post => Index'Result = 0 or else Index'Result <= Length (Source) - Pattern'Length + 1;

   --  as Index, in Source's characters from From on (Forward) or up to From (Backward)
   function Index (Source : Unbounded_String; Pattern : String; From : Positive; Going : Direction := Forward;
                   Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
     with Global => null,
          Pre => Pattern'Length > 0 and then From <= Length (Source),
          Post => (if Going = Forward
                   then (if Index'Result = 0
                         then (for all D in From - 1 .. Length (Source) - Pattern'Length => not Occurs_After (To_String (Source), Pattern, D, Mapping))
                         else Index'Result >= From and then Index'Result <= Length (Source) - Pattern'Length + 1
                              and then Occurs_After (To_String (Source), Pattern, Index'Result - 1, Mapping)
                              and then (for all D in From - 1 .. Index'Result - 2 => not Occurs_After (To_String (Source), Pattern, D, Mapping)))
                   else (if Index'Result = 0
                         then (for all D in 0 .. From - Pattern'Length => not Occurs_After (To_String (Source), Pattern, D, Mapping))
                         else Index'Result <= From - Pattern'Length + 1
                              and then Occurs_After (To_String (Source), Pattern, Index'Result - 1, Mapping)
                              and then (for all D in Index'Result .. From - Pattern'Length => not Occurs_After (To_String (Source), Pattern, D, Mapping))));

   function Index (Source : Unbounded_String; Pattern : String; From : Positive; Going : Direction := Forward;
                   Mapping : Maps.Character_Mapping_Function) return Natural
     with Global => null,
          Pre => Pattern'Length > 0 and then Mapping /= null and then From <= Length (Source),
          Post => Index'Result = 0 or else Index'Result <= Length (Source) - Pattern'Length + 1;

   --  the first (or last) place whose character is in Set (Test Inside) or not (Outside),
   --  or 0
   function Index (Source : Unbounded_String; Set : Maps.Character_Set; Test : Membership := Inside;
                   Going : Direction := Forward) return Natural
     with Global => null,
          Post => (if Index'Result = 0
                   then (for all I in 1 .. Length (Source) => Maps.Is_In (To_String (Source) (I), Set) /= (Test = Inside))
                   else Index'Result <= Length (Source)
                        and then Maps.Is_In (To_String (Source) (Index'Result), Set) = (Test = Inside)
                        and then (if Going = Forward
                                  then (for all I in 1 .. Index'Result - 1 => Maps.Is_In (To_String (Source) (I), Set) /= (Test = Inside))
                                  else (for all I in Index'Result + 1 .. Length (Source) => Maps.Is_In (To_String (Source) (I), Set) /= (Test = Inside))));

   function Index (Source : Unbounded_String; Set : Maps.Character_Set; From : Positive; Test : Membership := Inside;
                   Going : Direction := Forward) return Natural
     with Global => null,
          Pre => From <= Length (Source),
          Post => (if Going = Forward
                   then (if Index'Result = 0
                         then (for all I in From .. Length (Source) => Maps.Is_In (To_String (Source) (I), Set) /= (Test = Inside))
                         else Index'Result in From .. Length (Source)
                              and then Maps.Is_In (To_String (Source) (Index'Result), Set) = (Test = Inside)
                              and then (for all I in From .. Index'Result - 1 => Maps.Is_In (To_String (Source) (I), Set) /= (Test = Inside)))
                   else (if Index'Result = 0
                         then (for all I in 1 .. From => Maps.Is_In (To_String (Source) (I), Set) /= (Test = Inside))
                         else Index'Result <= From
                              and then Maps.Is_In (To_String (Source) (Index'Result), Set) = (Test = Inside)
                              and then (for all I in Index'Result + 1 .. From => Maps.Is_In (To_String (Source) (I), Set) /= (Test = Inside))));

   --  the first (or last) character that is not a space, or 0
   function Index_Non_Blank (Source : Unbounded_String; Going : Direction := Forward) return Natural
     with Global => null,
          Post => (if Index_Non_Blank'Result = 0
                   then (for all I in 1 .. Length (Source) => To_String (Source) (I) = Space)
                   else Index_Non_Blank'Result <= Length (Source)
                        and then To_String (Source) (Index_Non_Blank'Result) /= Space
                        and then (if Going = Forward
                                  then (for all I in 1 .. Index_Non_Blank'Result - 1 => To_String (Source) (I) = Space)
                                  else (for all I in Index_Non_Blank'Result + 1 .. Length (Source) => To_String (Source) (I) = Space)));

   function Index_Non_Blank (Source : Unbounded_String; From : Positive; Going : Direction := Forward) return Natural
     with Global => null,
          Pre => From <= Length (Source),
          Post => (if Going = Forward
                   then (if Index_Non_Blank'Result = 0
                         then (for all I in From .. Length (Source) => To_String (Source) (I) = Space)
                         else Index_Non_Blank'Result in From .. Length (Source)
                              and then To_String (Source) (Index_Non_Blank'Result) /= Space
                              and then (for all I in From .. Index_Non_Blank'Result - 1 => To_String (Source) (I) = Space))
                   else (if Index_Non_Blank'Result = 0
                         then (for all I in 1 .. From => To_String (Source) (I) = Space)
                         else Index_Non_Blank'Result <= From
                              and then To_String (Source) (Index_Non_Blank'Result) /= Space
                              and then (for all I in Index_Non_Blank'Result + 1 .. From => To_String (Source) (I) = Space)));

   --  how many times Pattern occurs, without overlapping, each character of Source mapped
   function Count (Source : Unbounded_String; Pattern : String; Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
     with Global => null,
          Pre => Pattern'Length > 0,
          Post => Count'Result = Count_After (To_String (Source), Pattern, 0, Mapping);

   function Count (Source : Unbounded_String; Pattern : String; Mapping : Maps.Character_Mapping_Function) return Natural
     with Global => null,
          Pre => Pattern'Length > 0 and then Mapping /= null,
          Post => Count'Result <= Length (Source);

   --  how many characters are in Set
   function Count (Source : Unbounded_String; Set : Maps.Character_Set) return Natural
     with Global => null,
          Post => Count'Result = Count_In (To_String (Source), Set, Inside, 0);

   --  the first run of characters from From on that are all in Set (Test Inside) or all not
   --  (Outside), as long as it goes: First .. Last; First = From and Last = 0 when there is none
   procedure Find_Token (Source : Unbounded_String; Set : Maps.Character_Set; From : Positive; Test : Membership;
                         First : out Positive; Last : out Natural)
     with Global => null,
          Pre => From <= Length (Source),
          Post => (if Last = 0
                   then First = From
                        and then (for all I in From .. Length (Source) => Maps.Is_In (To_String (Source) (I), Set) /= (Test = Inside))
                   else First in From .. Last and then Last <= Length (Source)
                        and then (for all I in From .. First - 1 => Maps.Is_In (To_String (Source) (I), Set) /= (Test = Inside))
                        and then (for all I in First .. Last => Maps.Is_In (To_String (Source) (I), Set) = (Test = Inside))
                        and then (Last = Length (Source) or else Maps.Is_In (To_String (Source) (Last + 1), Set) /= (Test = Inside)));

   --  as Find_Token from the first character (First = 1 and Last = 0 when there is none)
   procedure Find_Token (Source : Unbounded_String; Set : Maps.Character_Set; Test : Membership;
                         First : out Positive; Last : out Natural)
     with Global => null,
          Post => (if Last = 0
                   then First = 1
                        and then (for all I in 1 .. Length (Source) => Maps.Is_In (To_String (Source) (I), Set) /= (Test = Inside))
                   else First <= Last and then Last <= Length (Source)
                        and then (for all I in 1 .. First - 1 => Maps.Is_In (To_String (Source) (I), Set) /= (Test = Inside))
                        and then (for all I in First .. Last => Maps.Is_In (To_String (Source) (I), Set) = (Test = Inside))
                        and then (Last = Length (Source) or else Maps.Is_In (To_String (Source) (Last + 1), Set) /= (Test = Inside)));

   --  each character mapped
   function Translate (Source : Unbounded_String; Mapping : Maps.Character_Mapping) return Unbounded_String
     with Global => null,
          Post => Length (Translate'Result) = Length (Source)
                  and then (for all I in 1 .. Length (Source) => To_String (Translate'Result) (I) = Maps.Value (Mapping, To_String (Source) (I)));

   procedure Translate (Source : in out Unbounded_String; Mapping : Maps.Character_Mapping)
     with Global => null,
          Post => To_String (Source) = To_String (Translate (Source'Old, Mapping));

   function Translate (Source : Unbounded_String; Mapping : Maps.Character_Mapping_Function) return Unbounded_String
     with Global => null,
          Pre => Mapping /= null,
          Post => Length (Translate'Result) = Length (Source);

   procedure Translate (Source : in out Unbounded_String; Mapping : Maps.Character_Mapping_Function)
     with Global => null,
          Pre => Mapping /= null,
          Post => Length (Source) = Length (Source)'Old;

   --  the characters Low .. High replaced by By (By inserted before Low when High < Low)
   function Replace_Slice (Source : Unbounded_String; Low : Positive; High : Natural; By : String) return Unbounded_String
     with Global => null,
          Pre => Low <= Length (Source) + 1,
          Post => To_String (Replace_Slice'Result)
                  = Joined (To_String (Source) (1 .. Low - 1), By, To_String (Source) (Integer'Max (Integer'Min (High, Length (Source)) + 1, Low) .. Length (Source)));

   procedure Replace_Slice (Source : in out Unbounded_String; Low : Positive; High : Natural; By : String)
     with Global => null,
          Pre => Low <= Length (Source) + 1,
          Post => To_String (Source) = To_String (Replace_Slice (Source'Old, Low, High, By));

   function Insert (Source : Unbounded_String; Before : Positive; New_Item : String) return Unbounded_String
     with Global => null,
          Pre => Before <= Length (Source) + 1,
          Post => To_String (Insert'Result)
                  = Joined (To_String (Source) (1 .. Before - 1), New_Item, To_String (Source) (Before .. Length (Source)));

   procedure Insert (Source : in out Unbounded_String; Before : Positive; New_Item : String)
     with Global => null,
          Pre => Before <= Length (Source) + 1,
          Post => To_String (Source) = To_String (Insert (Source'Old, Before, New_Item));

   --  New_Item written over the characters from Position on, the string extended where it
   --  runs past the end
   function Overwrite (Source : Unbounded_String; Position : Positive; New_Item : String) return Unbounded_String
     with Global => null,
          Pre => Position <= Length (Source) + 1,
          Post => To_String (Overwrite'Result)
                  = Joined (To_String (Source) (1 .. Position - 1), New_Item,
                            To_String (Source) ((if New_Item'Length > Length (Source) - Position then Length (Source) + 1 else Position + New_Item'Length) .. Length (Source)));

   procedure Overwrite (Source : in out Unbounded_String; Position : Positive; New_Item : String)
     with Global => null,
          Pre => Position <= Length (Source) + 1,
          Post => To_String (Source) = To_String (Overwrite (Source'Old, Position, New_Item));

   --  the characters From .. Through removed (none when Through < From)
   function Delete (Source : Unbounded_String; From : Positive; Through : Natural) return Unbounded_String
     with Global => null,
          Pre => Through < From or else From <= Length (Source) + 1,
          Post => To_String (Delete'Result)
                  = (if Through < From then To_String (Source)
                     else To_String (Source) (1 .. From - 1) & To_String (Source) (Integer'Max (Integer'Min (Through, Length (Source)) + 1, From) .. Length (Source)));

   procedure Delete (Source : in out Unbounded_String; From : Positive; Through : Natural)
     with Global => null,
          Pre => Through < From or else From <= Length (Source) + 1,
          Post => To_String (Source) = To_String (Delete (Source'Old, From, Through));

   --  Source without its leading spaces (Left), trailing ones (Right) or both
   function Trim (Source : Unbounded_String; Side : Trim_End) return Unbounded_String
     with Global => null,
          Post => (if Index_Non_Blank (Source) = 0 then Length (Trim'Result) = 0
                   else To_String (Trim'Result)
                        = To_String (Source) ((if Side = Right then 1 else Index_Non_Blank (Source))
                                              .. (if Side = Left then Length (Source) else Index_Non_Blank (Source, Backward))));

   procedure Trim (Source : in out Unbounded_String; Side : Trim_End)
     with Global => null,
          Post => To_String (Source) = To_String (Trim (Source'Old, Side));

   --  Source without its leading characters that are in Left and its trailing ones in Right
   function Trim (Source : Unbounded_String; Left, Right : Maps.Character_Set) return Unbounded_String
     with Global => null,
          Post => (if Index (Source, Left, Outside) = 0 or else Index (Source, Right, Outside, Backward) < Index (Source, Left, Outside)
                   then Length (Trim'Result) = 0
                   else To_String (Trim'Result) = To_String (Source) (Index (Source, Left, Outside) .. Index (Source, Right, Outside, Backward)));

   procedure Trim (Source : in out Unbounded_String; Left, Right : Maps.Character_Set)
     with Global => null,
          Post => To_String (Source) = To_String (Trim (Source'Old, Left, Right));

   --  the first Count characters, padded with Pad
   function Head (Source : Unbounded_String; Count : Natural; Pad : Character := Space) return Unbounded_String
     with Global => null,
          Post => Length (Head'Result) = Count
                  and then (for all I in 1 .. Count => To_String (Head'Result) (I) = (if I <= Length (Source) then To_String (Source) (I) else Pad));

   procedure Head (Source : in out Unbounded_String; Count : Natural; Pad : Character := Space)
     with Global => null,
          Post => To_String (Source) = To_String (Head (Source'Old, Count, Pad));

   --  the last Count characters, padded on the left with Pad
   function Tail (Source : Unbounded_String; Count : Natural; Pad : Character := Space) return Unbounded_String
     with Global => null,
          Post => Length (Tail'Result) = Count
                  and then (for all I in 1 .. Count =>
                              To_String (Tail'Result) (I) = (if I > Count - Length (Source) then To_String (Source) (I - (Count - Length (Source))) else Pad));

   procedure Tail (Source : in out Unbounded_String; Count : Natural; Pad : Character := Space)
     with Global => null,
          Post => To_String (Source) = To_String (Tail (Source'Old, Count, Pad));

   --  Left copies of Right
   function "*" (Left : Natural; Right : Character) return Unbounded_String
     with Global => null,
          Post => Length ("*"'Result) = Left and then (for all I in 1 .. Left => To_String ("*"'Result) (I) = Right);

   function "*" (Left : Natural; Right : String) return Unbounded_String
     with Global => null,
          Post => Long_Long_Long_Integer (Length ("*"'Result)) = Long_Long_Long_Integer (Left) * Long_Long_Long_Integer (Right'Length)
                  and then (for all I in 1 .. Length ("*"'Result) => To_String ("*"'Result) (I) = Right (Right'First + (I - 1) mod Right'Length));

   function "*" (Left : Natural; Right : Unbounded_String) return Unbounded_String
     with Global => null,
          Post => Long_Long_Long_Integer (Length ("*"'Result)) = Long_Long_Long_Integer (Left) * Long_Long_Long_Integer (Length (Right))
                  and then (for all I in 1 .. Length ("*"'Result) => To_String ("*"'Result) (I) = To_String (Right) ((I - 1) mod Length (Right) + 1));
private
   --  the characters, all Latin-1 (each operation makes its text from a String)
   type Unbounded_String is record
      Value : Verada.Strings.Texts.Text;
   end record
     with Type_Invariant => Verada.Strings.Texts.Is_Latin_1 (Unbounded_String.Value);

   function Length (Source : Unbounded_String) return Natural is (Verada.Strings.Texts.Length (Source.Value));

   function To_String (Source : Unbounded_String) return String is (Verada.Strings.Texts.To_String (Source.Value));

   Null_Unbounded_String : constant Unbounded_String := (Value => <>);
end Ada.Strings.Unbounded;
