--  Verada predefined library: Ada.Strings.Wide_Unbounded (Ada RM A.4.5, A.4.7): strings of any length,
--  copied as other values are. A value holds a Verada.Strings.Texts.Text, whose characters a
--  copy shares and the last holder frees. Each operation is stated by its model, To_Wide_String, and
--  Ada.Strings.Wide_Fixed's operation on it, so that a proof of one carries over. 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. (Generated from
--  tools/library/unbounded.vads.in by tools/library/strings-widths.py.)
with Ada.Strings.Wide_Fixed;
with Ada.Strings.Wide_Maps;
with Verada.Storage;
with Verada.Strings.Texts;

package Ada.Strings.Wide_Unbounded is
   use type Wide_Maps.Wide_Character_Mapping_Function;

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

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

   --  (Verada, for contracts) the position of the character at Index
   function Code (Source : Unbounded_Wide_String; Index : Positive) return Natural
     with Ghost, Global => null,
          Pre => Index <= Length (Source);

   --  the model of a value: its characters, from 1
   function To_Wide_String (Source : Unbounded_Wide_String) return Wide_String
     with Global => null,
          Post => To_Wide_String'Result'Length = Length (Source) and then To_Wide_String'Result'First = 1 and then To_Wide_String'Result'Last = Length (Source)
                  and then (for all I in To_Wide_String'Result'Range => Wide_Character'Pos (To_Wide_String'Result (I)) = Code (Source, I));

   Null_Unbounded_Wide_String : constant Unbounded_Wide_String;

   function To_Unbounded_Wide_String (Source : Wide_String) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String (To_Unbounded_Wide_String'Result) = Source;

   --  a string of Length spaces
   function To_Unbounded_Wide_String (Length : Natural) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String (To_Unbounded_Wide_String'Result) = Wide_Fixed."*" (Length, Wide_Space);

   --  an access to a string, which Free deallocates (pool-specific, as Verada frees only what
   --  an allocator made)
   type Wide_String_Access is access Wide_String;

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

   procedure Set_Unbounded_Wide_String (Target : out Unbounded_Wide_String; Source : Wide_String)
     with Global => null,
          Post => To_Wide_String (Target) = Source;

   function Append (Left, Right : Unbounded_Wide_String) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String (Append'Result) = To_Wide_String (Left) & To_Wide_String (Right);

   function Append (Left : Unbounded_Wide_String; Right : Wide_String) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String (Append'Result) = To_Wide_String (Left) & Right;

   function Append (Left : Wide_String; Right : Unbounded_Wide_String) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String (Append'Result) = Wide_Fixed.Joined (Left, To_Wide_String (Right), "");

   function Append (Left : Unbounded_Wide_String; Right : Wide_Character) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String (Append'Result) = To_Wide_String (Left) & Right;

   function Append (Left : Wide_Character; Right : Unbounded_Wide_String) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String (Append'Result) = Left & To_Wide_String (Right);

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

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

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

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

   function "&" (Left : Unbounded_Wide_String; Right : Wide_String) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String ("&"'Result) = To_Wide_String (Left) & Right;

   function "&" (Left : Wide_String; Right : Unbounded_Wide_String) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String ("&"'Result) = Wide_Fixed.Joined (Left, To_Wide_String (Right), "");

   function "&" (Left : Unbounded_Wide_String; Right : Wide_Character) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String ("&"'Result) = To_Wide_String (Left) & Right;

   function "&" (Left : Wide_Character; Right : Unbounded_Wide_String) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String ("&"'Result) = Left & To_Wide_String (Right);

   function Element (Source : Unbounded_Wide_String; Index : Positive) return Wide_Character
     with Global => null,
          Pre => Index <= Length (Source),
          Post => Element'Result = To_Wide_String (Source) (Index);

   procedure Replace_Element (Source : in out Unbounded_Wide_String; Index : Positive; By : Wide_Character)
     with Global => null,
          Pre => Index <= Length (Source),
          Post => To_Wide_String (Source) = Wide_Fixed.Overwrite (To_Wide_String (Source)'Old, Index, (1 => By));

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

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

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

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

   function "=" (Left : Unbounded_Wide_String; Right : Wide_String) return Boolean
     with Global => null,
          Post => "="'Result = (To_Wide_String (Left) = Right);

   function "=" (Left : Wide_String; Right : Unbounded_Wide_String) return Boolean
     with Global => null,
          Post => "="'Result = (Left = To_Wide_String (Right));

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

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

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

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

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

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

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

   function ">" (Left : Unbounded_Wide_String; Right : Wide_String) return Boolean
     with Global => null,
          Post => ">"'Result = (To_Wide_String (Left) > Right);

   function ">" (Left : Wide_String; Right : Unbounded_Wide_String) return Boolean
     with Global => null,
          Post => ">"'Result = (Left > To_Wide_String (Right));

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

   function ">=" (Left : Unbounded_Wide_String; Right : Wide_String) return Boolean
     with Global => null,
          Post => ">="'Result = (To_Wide_String (Left) >= Right);

   function ">=" (Left : Wide_String; Right : Unbounded_Wide_String) return Boolean
     with Global => null,
          Post => ">="'Result = (Left >= To_Wide_String (Right));

   function Index (Source : Unbounded_Wide_String; Pattern : Wide_String; From : Positive; Going : Direction := Forward;
                   Mapping : Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity) return Natural
     with Global => null,
          Pre => Pattern'Length > 0 and then From <= Length (Source),
          Post => Index'Result = Wide_Fixed.Index (To_Wide_String (Source), Pattern, From, Going, Mapping);

   function Index (Source : Unbounded_Wide_String; Pattern : Wide_String; From : Positive; Going : Direction := Forward;
                   Mapping : Wide_Maps.Wide_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 : Unbounded_Wide_String; Pattern : Wide_String; Going : Direction := Forward;
                   Mapping : Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity) return Natural
     with Global => null,
          Pre => Pattern'Length > 0,
          Post => Index'Result = Wide_Fixed.Index (To_Wide_String (Source), Pattern, Going, Mapping);

   function Index (Source : Unbounded_Wide_String; Pattern : Wide_String; Going : Direction := Forward;
                   Mapping : Wide_Maps.Wide_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 : Unbounded_Wide_String; Set : Wide_Maps.Wide_Character_Set; From : Positive; Test : Membership := Inside;
                   Going : Direction := Forward) return Natural
     with Global => null,
          Pre => From <= Length (Source),
          Post => Index'Result = Wide_Fixed.Index (To_Wide_String (Source), Set, From, Test, Going);

   function Index (Source : Unbounded_Wide_String; Set : Wide_Maps.Wide_Character_Set; Test : Membership := Inside; Going : Direction := Forward) return Natural
     with Global => null,
          Post => Index'Result = Wide_Fixed.Index (To_Wide_String (Source), Set, Test, Going);

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

   function Index_Non_Blank (Source : Unbounded_Wide_String; Going : Direction := Forward) return Natural
     with Global => null,
          Post => Index_Non_Blank'Result = Wide_Fixed.Index_Non_Blank (To_Wide_String (Source), Going);

   function Count (Source : Unbounded_Wide_String; Pattern : Wide_String; Mapping : Wide_Maps.Wide_Character_Mapping := Wide_Maps.Identity) return Natural
     with Global => null,
          Pre => Pattern'Length > 0,
          Post => Count'Result = Wide_Fixed.Count (To_Wide_String (Source), Pattern, Mapping);

   function Count (Source : Unbounded_Wide_String; Pattern : Wide_String; Mapping : Wide_Maps.Wide_Character_Mapping_Function) return Natural
     with Global => null,
          Pre => Pattern'Length > 0 and then Mapping /= null,
          Post => Count'Result <= Length (Source);

   function Count (Source : Unbounded_Wide_String; Set : Wide_Maps.Wide_Character_Set) return Natural
     with Global => null,
          Post => Count'Result = Wide_Fixed.Count (To_Wide_String (Source), Set);

   procedure Find_Token (Source : Unbounded_Wide_String; Set : Wide_Maps.Wide_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) => Wide_Maps.Is_In (To_Wide_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 => Wide_Maps.Is_In (To_Wide_String (Source) (I), Set) /= (Test = Inside))
                        and then (for all I in First .. Last => Wide_Maps.Is_In (To_Wide_String (Source) (I), Set) = (Test = Inside))
                        and then (Last = Length (Source) or else Wide_Maps.Is_In (To_Wide_String (Source) (Last + 1), Set) /= (Test = Inside)));

   procedure Find_Token (Source : Unbounded_Wide_String; Set : Wide_Maps.Wide_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) => Wide_Maps.Is_In (To_Wide_String (Source) (I), Set) /= (Test = Inside))
                   else First <= Last and then Last <= Length (Source)
                        and then (for all I in 1 .. First - 1 => Wide_Maps.Is_In (To_Wide_String (Source) (I), Set) /= (Test = Inside))
                        and then (for all I in First .. Last => Wide_Maps.Is_In (To_Wide_String (Source) (I), Set) = (Test = Inside))
                        and then (Last = Length (Source) or else Wide_Maps.Is_In (To_Wide_String (Source) (Last + 1), Set) /= (Test = Inside)));

   function Translate (Source : Unbounded_Wide_String; Mapping : Wide_Maps.Wide_Character_Mapping) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String (Translate'Result) = Wide_Fixed.Translate (To_Wide_String (Source), Mapping);

   procedure Translate (Source : in out Unbounded_Wide_String; Mapping : Wide_Maps.Wide_Character_Mapping)
     with Global => null,
          Post => To_Wide_String (Source) = Wide_Fixed.Translate (To_Wide_String (Source)'Old, Mapping);

   function Translate (Source : Unbounded_Wide_String; Mapping : Wide_Maps.Wide_Character_Mapping_Function) return Unbounded_Wide_String
     with Global => null,
          Pre => Mapping /= null,
          Post => Length (Translate'Result) = Length (Source);

   procedure Translate (Source : in out Unbounded_Wide_String; Mapping : Wide_Maps.Wide_Character_Mapping_Function)
     with Global => null,
          Pre => Mapping /= null,
          Post => Length (Source) = Length (Source)'Old;

   function Replace_Slice (Source : Unbounded_Wide_String; Low : Positive; High : Natural; By : Wide_String) return Unbounded_Wide_String
     with Global => null,
          Pre => Low <= Length (Source) + 1,
          Post => To_Wide_String (Replace_Slice'Result) = Wide_Fixed.Replace_Slice (To_Wide_String (Source), Low, High, By);

   procedure Replace_Slice (Source : in out Unbounded_Wide_String; Low : Positive; High : Natural; By : Wide_String)
     with Global => null,
          Pre => Low <= Length (Source) + 1,
          Post => To_Wide_String (Source) = Wide_Fixed.Replace_Slice (To_Wide_String (Source)'Old, Low, High, By);

   function Insert (Source : Unbounded_Wide_String; Before : Positive; New_Item : Wide_String) return Unbounded_Wide_String
     with Global => null,
          Pre => Before <= Length (Source) + 1,
          Post => To_Wide_String (Insert'Result) = Wide_Fixed.Insert (To_Wide_String (Source), Before, New_Item);

   procedure Insert (Source : in out Unbounded_Wide_String; Before : Positive; New_Item : Wide_String)
     with Global => null,
          Pre => Before <= Length (Source) + 1,
          Post => To_Wide_String (Source) = Wide_Fixed.Insert (To_Wide_String (Source)'Old, Before, New_Item);

   function Overwrite (Source : Unbounded_Wide_String; Position : Positive; New_Item : Wide_String) return Unbounded_Wide_String
     with Global => null,
          Pre => Position <= Length (Source) + 1,
          Post => To_Wide_String (Overwrite'Result) = Wide_Fixed.Overwrite (To_Wide_String (Source), Position, New_Item);

   procedure Overwrite (Source : in out Unbounded_Wide_String; Position : Positive; New_Item : Wide_String)
     with Global => null,
          Pre => Position <= Length (Source) + 1,
          Post => To_Wide_String (Source) = Wide_Fixed.Overwrite (To_Wide_String (Source)'Old, Position, New_Item);

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

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

   function Trim (Source : Unbounded_Wide_String; Side : Trim_End) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String (Trim'Result) = Wide_Fixed.Trim (To_Wide_String (Source), Side);

   procedure Trim (Source : in out Unbounded_Wide_String; Side : Trim_End)
     with Global => null,
          Post => To_Wide_String (Source) = Wide_Fixed.Trim (To_Wide_String (Source)'Old, Side);

   function Trim (Source : Unbounded_Wide_String; Left, Right : Wide_Maps.Wide_Character_Set) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String (Trim'Result) = Wide_Fixed.Trim (To_Wide_String (Source), Left, Right);

   procedure Trim (Source : in out Unbounded_Wide_String; Left, Right : Wide_Maps.Wide_Character_Set)
     with Global => null,
          Post => To_Wide_String (Source) = Wide_Fixed.Trim (To_Wide_String (Source)'Old, Left, Right);

   function Head (Source : Unbounded_Wide_String; Count : Natural; Pad : Wide_Character := Wide_Space) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String (Head'Result) = Wide_Fixed.Head (To_Wide_String (Source), Count, Pad);

   procedure Head (Source : in out Unbounded_Wide_String; Count : Natural; Pad : Wide_Character := Wide_Space)
     with Global => null,
          Post => To_Wide_String (Source) = Wide_Fixed.Head (To_Wide_String (Source)'Old, Count, Pad);

   function Tail (Source : Unbounded_Wide_String; Count : Natural; Pad : Wide_Character := Wide_Space) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String (Tail'Result) = Wide_Fixed.Tail (To_Wide_String (Source), Count, Pad);

   procedure Tail (Source : in out Unbounded_Wide_String; Count : Natural; Pad : Wide_Character := Wide_Space)
     with Global => null,
          Post => To_Wide_String (Source) = Wide_Fixed.Tail (To_Wide_String (Source)'Old, Count, Pad);

   function "*" (Left : Natural; Right : Wide_Character) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String ("*"'Result) = Wide_Fixed."*" (Left, Right);

   function "*" (Left : Natural; Right : Wide_String) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String ("*"'Result) = Wide_Fixed."*" (Left, Right);

   function "*" (Left : Natural; Right : Unbounded_Wide_String) return Unbounded_Wide_String
     with Global => null,
          Post => To_Wide_String ("*"'Result) = Wide_Fixed."*" (Left, To_Wide_String (Right));

private
   --  the characters' code points, each a Wide_Character's
   type Unbounded_Wide_String is record
      Value : Verada.Strings.Texts.Text;
   end record
     with Type_Invariant =>
            (for all I in 1 .. Verada.Strings.Texts.Length (Unbounded_Wide_String.Value) =>
               Wide_Wide_Character'Pos (Verada.Strings.Texts.To_Wide_Wide_String (Unbounded_Wide_String.Value) (I)) <= Wide_Character'Pos (Wide_Character'Last));

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

   function Code (Source : Unbounded_Wide_String; Index : Positive) return Natural is
     (Wide_Wide_Character'Pos (Verada.Strings.Texts.To_Wide_Wide_String (Source.Value) (Index)));

   Null_Unbounded_Wide_String : constant Unbounded_Wide_String := (Value => <>);
end Ada.Strings.Wide_Unbounded;
