--  Verada predefined library: Ada.Strings.Bounded (Ada RM A.4.4): strings of at most Max
--  characters, with value semantics. Each operation is stated by its model, To_String, and
--  Ada.Strings.Fixed's operation on it, so that a proof of one carries over. Where the RM
--  truncates (a Drop parameter) or raises Length_Error, a precondition requires that the
--  result fits, and Index_Error and Pattern_Error likewise. (Generated from
--  tools/library/bounded.vads.in by tools/library/strings-widths.py.)
with Ada.Strings.Fixed;
with Ada.Strings.Maps;

package Ada.Strings.Bounded is
   generic
      Max : Positive;
   package Generic_Bounded_Length is
      use type Maps.Character_Mapping_Function;

      Max_Length : constant Positive := Max;

      subtype Length_Range is Natural range 0 .. Max_Length;

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

      function Length (Source : Bounded_String) return Length_Range
        with Global => null;

      --  the model of a value: its characters, from 1
      function To_String (Source : Bounded_String) return String
        with Global => null,
             Post => To_String'Result'Length = Length (Source) and then To_String'Result'First = 1 and then To_String'Result'Last = Length (Source);

      function Null_Bounded_String return Bounded_String
        with Global => null,
             Post => Length (Null_Bounded_String'Result) = 0;

      function To_Bounded_String (Source : String) return Bounded_String
        with Global => null,
             Pre => Source'Length <= Max_Length,
             Post => To_String (To_Bounded_String'Result) = Source;

      procedure Set_Bounded_String (Target : out Bounded_String; Source : String)
        with Global => null,
             Pre => Source'Length <= Max_Length,
             Post => To_String (Target) = Source;

      function Append (Left, Right : Bounded_String) return Bounded_String
        with Global => null,
             Pre => Length (Right) <= Max_Length - Length (Left),
             Post => To_String (Append'Result) = To_String (Left) & To_String (Right);

      function Append (Left : Bounded_String; Right : String) return Bounded_String
        with Global => null,
             Pre => Right'Length <= Max_Length - Length (Left),
             Post => To_String (Append'Result) = To_String (Left) & Right;

      function Append (Left : String; Right : Bounded_String) return Bounded_String
        with Global => null,
             Pre => Left'Length <= Max_Length - Length (Right),
             Post => To_String (Append'Result) = Fixed.Joined (Left, To_String (Right), "");

      function Append (Left : Bounded_String; Right : Character) return Bounded_String
        with Global => null,
             Pre => Length (Left) < Max_Length,
             Post => To_String (Append'Result) = To_String (Left) & Right;

      function Append (Left : Character; Right : Bounded_String) return Bounded_String
        with Global => null,
             Pre => Length (Right) < Max_Length,
             Post => To_String (Append'Result) = Left & To_String (Right);

      procedure Append (Source : in out Bounded_String; New_Item : Bounded_String)
        with Global => null,
             Pre => Length (New_Item) <= Max_Length - Length (Source),
             Post => To_String (Source) = To_String (Source)'Old & To_String (New_Item);

      procedure Append (Source : in out Bounded_String; New_Item : String)
        with Global => null,
             Pre => New_Item'Length <= Max_Length - Length (Source),
             Post => To_String (Source) = To_String (Source)'Old & New_Item;

      procedure Append (Source : in out Bounded_String; New_Item : Character)
        with Global => null,
             Pre => Length (Source) < Max_Length,
             Post => To_String (Source) = To_String (Source)'Old & New_Item;

      function "&" (Left, Right : Bounded_String) return Bounded_String
        with Global => null,
             Pre => Length (Right) <= Max_Length - Length (Left),
             Post => To_String ("&"'Result) = To_String (Left) & To_String (Right);

      function "&" (Left : Bounded_String; Right : String) return Bounded_String
        with Global => null,
             Pre => Right'Length <= Max_Length - Length (Left),
             Post => To_String ("&"'Result) = To_String (Left) & Right;

      function "&" (Left : String; Right : Bounded_String) return Bounded_String
        with Global => null,
             Pre => Left'Length <= Max_Length - Length (Right),
             Post => To_String ("&"'Result) = Fixed.Joined (Left, To_String (Right), "");

      function "&" (Left : Bounded_String; Right : Character) return Bounded_String
        with Global => null,
             Pre => Length (Left) < Max_Length,
             Post => To_String ("&"'Result) = To_String (Left) & Right;

      function "&" (Left : Character; Right : Bounded_String) return Bounded_String
        with Global => null,
             Pre => Length (Right) < Max_Length,
             Post => To_String ("&"'Result) = Left & To_String (Right);

      function Element (Source : Bounded_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 Bounded_String; Index : Positive; By : Character)
        with Global => null,
             Pre => Index <= Length (Source),
             Post => To_String (Source) = Fixed.Overwrite (To_String (Source)'Old, Index, (1 => By));

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

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

      procedure Bounded_Slice (Source : Bounded_String; Target : out Bounded_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 : Bounded_String) return Boolean
        with Global => null,
             Post => "="'Result = (To_String (Left) = To_String (Right));

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      function Index (Source : Bounded_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 => Index'Result = Fixed.Index (To_String (Source), Pattern, From, Going, Mapping);

      function Index (Source : Bounded_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;

      function Index (Source : Bounded_String; Pattern : String; Going : Direction := Forward;
                      Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
        with Global => null,
             Pre => Pattern'Length > 0,
             Post => Index'Result = Fixed.Index (To_String (Source), Pattern, Going, Mapping);

      function Index (Source : Bounded_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;

      function Index (Source : Bounded_String; Set : Maps.Character_Set; From : Positive; Test : Membership := Inside;
                      Going : Direction := Forward) return Natural
        with Global => null,
             Pre => From <= Length (Source),
             Post => Index'Result = Fixed.Index (To_String (Source), Set, From, Test, Going);

      function Index (Source : Bounded_String; Set : Maps.Character_Set; Test : Membership := Inside; Going : Direction := Forward) return Natural
        with Global => null,
             Post => Index'Result = Fixed.Index (To_String (Source), Set, Test, Going);

      function Index_Non_Blank (Source : Bounded_String; From : Positive; Going : Direction := Forward) return Natural
        with Global => null,
             Pre => From <= Length (Source),
             Post => Index_Non_Blank'Result = Fixed.Index_Non_Blank (To_String (Source), From, Going);

      function Index_Non_Blank (Source : Bounded_String; Going : Direction := Forward) return Natural
        with Global => null,
             Post => Index_Non_Blank'Result = Fixed.Index_Non_Blank (To_String (Source), Going);

      function Count (Source : Bounded_String; Pattern : String; Mapping : Maps.Character_Mapping := Maps.Identity) return Natural
        with Global => null,
             Pre => Pattern'Length > 0,
             Post => Count'Result = Fixed.Count (To_String (Source), Pattern, Mapping);

      function Count (Source : Bounded_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);

      function Count (Source : Bounded_String; Set : Maps.Character_Set) return Natural
        with Global => null,
             Post => Count'Result = Fixed.Count (To_String (Source), Set);

      procedure Find_Token (Source : Bounded_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)));

      procedure Find_Token (Source : Bounded_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)));

      function Translate (Source : Bounded_String; Mapping : Maps.Character_Mapping) return Bounded_String
        with Global => null,
             Post => To_String (Translate'Result) = Fixed.Translate (To_String (Source), Mapping);

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

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

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

      function Replace_Slice (Source : Bounded_String; Low : Positive; High : Natural; By : String) return Bounded_String
        with Global => null,
             Pre => Low <= Length (Source) + 1
                    and then Fixed.Replace_Slice (To_String (Source), Low, High, By)'Length <= Max_Length,
             Post => To_String (Replace_Slice'Result) = Fixed.Replace_Slice (To_String (Source), Low, High, By);

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

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

      procedure Insert (Source : in out Bounded_String; Before : Positive; New_Item : String)
        with Global => null,
             Pre => Before <= Length (Source) + 1 and then New_Item'Length <= Max_Length - Length (Source),
             Post => To_String (Source) = Fixed.Insert (To_String (Source)'Old, Before, New_Item);

      function Overwrite (Source : Bounded_String; Position : Positive; New_Item : String) return Bounded_String
        with Global => null,
             Pre => Position <= Length (Source) + 1 and then New_Item'Length <= Max_Length - (Position - 1),
             Post => To_String (Overwrite'Result) = Fixed.Overwrite (To_String (Source), Position, New_Item);

      procedure Overwrite (Source : in out Bounded_String; Position : Positive; New_Item : String)
        with Global => null,
             Pre => Position <= Length (Source) + 1 and then New_Item'Length <= Max_Length - (Position - 1),
             Post => To_String (Source) = Fixed.Overwrite (To_String (Source)'Old, Position, New_Item);

      function Delete (Source : Bounded_String; From : Positive; Through : Natural) return Bounded_String
        with Global => null,
             Pre => Through < From or else From <= Length (Source) + 1,
             Post => To_String (Delete'Result) = Fixed.Delete (To_String (Source), From, Through);

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

      function Trim (Source : Bounded_String; Side : Trim_End) return Bounded_String
        with Global => null,
             Post => To_String (Trim'Result) = Fixed.Trim (To_String (Source), Side);

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

      function Trim (Source : Bounded_String; Left, Right : Maps.Character_Set) return Bounded_String
        with Global => null,
             Post => To_String (Trim'Result) = Fixed.Trim (To_String (Source), Left, Right);

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

      function Head (Source : Bounded_String; Count : Natural; Pad : Character := Space) return Bounded_String
        with Global => null,
             Pre => Count <= Max_Length,
             Post => To_String (Head'Result) = Fixed.Head (To_String (Source), Count, Pad);

      procedure Head (Source : in out Bounded_String; Count : Natural; Pad : Character := Space)
        with Global => null,
             Pre => Count <= Max_Length,
             Post => To_String (Source) = Fixed.Head (To_String (Source)'Old, Count, Pad);

      function Tail (Source : Bounded_String; Count : Natural; Pad : Character := Space) return Bounded_String
        with Global => null,
             Pre => Count <= Max_Length,
             Post => To_String (Tail'Result) = Fixed.Tail (To_String (Source), Count, Pad);

      procedure Tail (Source : in out Bounded_String; Count : Natural; Pad : Character := Space)
        with Global => null,
             Pre => Count <= Max_Length,
             Post => To_String (Source) = Fixed.Tail (To_String (Source)'Old, Count, Pad);

      function "*" (Left : Natural; Right : Character) return Bounded_String
        with Global => null,
             Pre => Left <= Max_Length,
             Post => To_String ("*"'Result) = Fixed."*" (Left, Right);

      function "*" (Left : Natural; Right : String) return Bounded_String
        with Global => null,
             Pre => Long_Long_Long_Integer (Left) * Long_Long_Long_Integer (Right'Length) <= Long_Long_Long_Integer (Max_Length),
             Post => To_String ("*"'Result) = Fixed."*" (Left, Right);

      function "*" (Left : Natural; Right : Bounded_String) return Bounded_String
        with Global => null,
             Pre => Long_Long_Long_Integer (Left) * Long_Long_Long_Integer (Length (Right)) <= Long_Long_Long_Integer (Max_Length),
             Post => To_String ("*"'Result) = Fixed."*" (Left, To_String (Right));

      function Replicate (Count : Natural; Item : Character) return Bounded_String
        with Global => null,
             Pre => Count <= Max_Length,
             Post => To_String (Replicate'Result) = Fixed."*" (Count, Item);

      function Replicate (Count : Natural; Item : String) return Bounded_String
        with Global => null,
             Pre => Long_Long_Long_Integer (Count) * Long_Long_Long_Integer (Item'Length) <= Long_Long_Long_Integer (Max_Length),
             Post => To_String (Replicate'Result) = Fixed."*" (Count, Item);

      function Replicate (Count : Natural; Item : Bounded_String) return Bounded_String
        with Global => null,
             Pre => Long_Long_Long_Integer (Count) * Long_Long_Long_Integer (Length (Item)) <= Long_Long_Long_Integer (Max_Length),
             Post => To_String (Replicate'Result) = Fixed."*" (Count, To_String (Item));
   private
      type Bounded_String is record
         Length : Length_Range := 0;
         Data : String (1 .. Max_Length) := (others => Space);
      end record;

      function Length (Source : Bounded_String) return Length_Range is (Source.Length);

      function To_String (Source : Bounded_String) return String is (Source.Data (1 .. Source.Length));
   end Generic_Bounded_Length;
end Ada.Strings.Bounded;
