--  Verada predefined library: Ada.Strings.Fixed (Ada RM A.4.3), written in Ada and proved
--  like any other unit. A function's string result is indexed from 1; Index and Find_Token
--  give positions in Source. Where the RM raises Index_Error, Pattern_Error or Length_Error,
--  a precondition requires what avoids it. (Generated from tools/library/fixed.vads.in by
--  tools/library/strings-widths.py, one text for the three widths.)
with Ada.Strings.Maps;
with Verada.Storage;

package Ada.Strings.Fixed is
   use type Maps.Character_Mapping_Function;
   use type Maps.Character_Mapping;

   --  the occurrence of Pattern after Source's first Done characters (at Source'First + Done),
   --  each character of Source mapped: Done <= Source'Length - Pattern'Length, and each
   --  mapped character there the pattern's (its expression is in the body, so that a client's
   --  proof does not take it apart)
   function Occurs_After (Source, Pattern : String; Done : Natural; Mapping : Maps.Character_Mapping) return Boolean
     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
   function Count_In (Source : String; Set : Maps.Character_Set; Done : Natural) return Natural is
     (if Done >= Source'Length then 0
      else (if Maps.Is_In (Source (Source'First + Done), Set) then 1 else 0) + Count_In (Source, Set, 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 results of Replace_Slice, Insert and Overwrite)
   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 < Verada.Storage.Length_Bound
                  and then Joined'Result'First = 1;

   --  Source fitted to Length characters, as Move fits it (RM A.4.3(56-58)): longer, the
   --  characters Drop (or, when Drop is Error, Justify) keeps; shorter, padded with Pad on the
   --  side or sides Justify leaves (an odd padding's extra character on the right)
   function Moved (Source : String; Length : Natural; Drop : Truncation; Justify : Alignment; Pad : Character) return String is
     (String'(for I in 1 .. Length =>
                (if Source'Length >= Length
                 then (if Source'Length > Length and then (Drop = Left or else (Drop = Error and then Justify = Right))
                       then Source (Source'Last - (Length - I))
                       else Source (Source'First + (I - 1)))
                 elsif I > (case Justify is when Left => 0, when Right => Length - Source'Length, when Center => (Length - Source'Length) / 2)
                       and then I - (case Justify is when Left => 0, when Right => Length - Source'Length, when Center => (Length - Source'Length) / 2)
                                <= Source'Length
                 then Source (Source'First
                              + (I - 1 - (case Justify is when Left => 0, when Right => Length - Source'Length, when Center => (Length - Source'Length) / 2)))
                 else Pad)))
     with Global => null,
          Post => Moved'Result'Length = Length and then Moved'Result'First = 1;

   --  whether Move may fit Source to Length characters: where the RM raises Length_Error, the
   --  characters Drop = Error would lose are not all Pad
   function Fits (Source : String; Length : Natural; Drop : Truncation; Justify : Alignment; Pad : Character) return Boolean is
     (Source'Length <= Length or else Drop /= Error
      or else (Justify = Left and then (for all I in Source'First + Length .. Source'Last => Source (I) = Pad))
      or else (Justify = Right and then (for all I in Source'First .. Source'Last - Length => Source (I) = Pad)))
     with Global => null;

   --  Source into Target, fitted to Target's length
   procedure Move (Source : String; Target : out String; Drop : Truncation := Error; Justify : Alignment := Left; Pad : Character := Space)
     with Global => null,
          Pre => Fits (Source, Target'Length, Drop, Justify, Pad),
          Post => Target = Moved (Source, Target'Length, Drop, Justify, Pad);

   --  the first (or last) place in Source where Pattern occurs, each character of Source
   --  mapped, searching from From on (Forward) or up to From (Backward); or 0
   function Index (Source : 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 in Source'Range,
          Post => (if Going = Forward
                   then (if Index'Result = 0
                         then (for all D in From - Source'First .. Source'Length - Pattern'Length => not Occurs_After (Source, Pattern, D, Mapping))
                         else Index'Result >= From and then Index'Result - Source'First <= Source'Length - Pattern'Length
                              and then Occurs_After (Source, Pattern, Index'Result - Source'First, Mapping)
                              and then (for all D in From - Source'First .. Index'Result - Source'First - 1 => not Occurs_After (Source, Pattern, D, Mapping)))
                   else (if Index'Result = 0
                         then (for all D in 0 .. From - Source'First + 1 - Pattern'Length => not Occurs_After (Source, Pattern, D, Mapping))
                         else Index'Result >= Source'First and then Index'Result - Source'First <= From - Source'First + 1 - Pattern'Length
                              and then Occurs_After (Source, Pattern, Index'Result - Source'First, Mapping)
                              and then (for all D in Index'Result - Source'First + 1 .. From - Source'First + 1 - Pattern'Length => not Occurs_After (Source, Pattern, D, Mapping))));

   function Index (Source : 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 in Source'Range,
          Post => Index'Result = 0 or else (Index'Result >= Source'First and then Index'Result - Source'First <= Source'Length - Pattern'Length);

   --  the first (or last) place in Source where Pattern occurs, each character of Source
   --  mapped, or 0
   function Index (Source : 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 .. Source'Length - Pattern'Length => not Occurs_After (Source, Pattern, D, Mapping))
                   else Index'Result >= Source'First and then Index'Result - Source'First <= Source'Length - Pattern'Length
                        and then Occurs_After (Source, Pattern, Index'Result - Source'First, Mapping)
                        --  (unmapped, the characters there are the pattern's, and so the slice)
                        and then (if Mapping = Maps.Identity
                                  then (for all I in Index'Result .. Index'Result + (Pattern'Length - 1) =>
                                          Source (I) = Pattern (Pattern'First + (I - Index'Result)))
                                       and then Source (Index'Result .. Index'Result + (Pattern'Length - 1)) = Pattern)
                        and then (if Going = Forward
                                  then (for all D in 0 .. Index'Result - Source'First - 1 => not Occurs_After (Source, Pattern, D, Mapping))
                                  else (for all D in Index'Result - Source'First + 1 .. Source'Length - Pattern'Length => not Occurs_After (Source, Pattern, D, Mapping))));

   function Index (Source : 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 >= Source'First and then Index'Result - Source'First <= Source'Length - Pattern'Length);

   --  the first (or last) place, from From on (Forward) or up to From (Backward), whose
   --  character is in Set (Test Inside) or not (Outside); or 0
   function Index (Source : String; Set : Maps.Character_Set; From : Positive; Test : Membership := Inside;
                   Going : Direction := Forward) return Natural
     with Global => null,
          Pre => From in Source'Range,
          Post => (if Going = Forward
                   then (if Index'Result = 0
                         then (for all I in From .. Source'Last => Maps.Is_In (Source (I), Set) /= (Test = Inside))
                         else Index'Result in From .. Source'Last
                              and then Maps.Is_In (Source (Index'Result), Set) = (Test = Inside)
                              and then (for all I in From .. Index'Result - 1 => Maps.Is_In (Source (I), Set) /= (Test = Inside)))
                   else (if Index'Result = 0
                         then (for all I in Source'First .. From => Maps.Is_In (Source (I), Set) /= (Test = Inside))
                         else Index'Result in Source'First .. From
                              and then Maps.Is_In (Source (Index'Result), Set) = (Test = Inside)
                              and then (for all I in Index'Result .. From => (if I > Index'Result then Maps.Is_In (Source (I), Set) /= (Test = Inside)))));

   function Index (Source : 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 Source'Range => Maps.Is_In (Source (I), Set) /= (Test = Inside))
                   else Index'Result in Source'Range
                        and then Maps.Is_In (Source (Index'Result), Set) = (Test = Inside)
                        and then (if Going = Forward
                                  then (for all I in Source'First .. Index'Result - 1 => Maps.Is_In (Source (I), Set) /= (Test = Inside))
                                  else (for all I in Index'Result .. Source'Last => (if I > Index'Result then Maps.Is_In (Source (I), Set) /= (Test = Inside)))));

   --  the first (or last) character, from From on or up to From, that is not a space; or 0
   function Index_Non_Blank (Source : String; From : Positive; Going : Direction := Forward) return Natural
     with Global => null,
          Pre => From in Source'Range,
          Post => (if Going = Forward
                   then (if Index_Non_Blank'Result = 0
                         then (for all I in From .. Source'Last => Source (I) = Space)
                         else Index_Non_Blank'Result in From .. Source'Last
                              and then Source (Index_Non_Blank'Result) /= Space
                              and then (for all I in From .. Index_Non_Blank'Result - 1 => Source (I) = Space))
                   else (if Index_Non_Blank'Result = 0
                         then (for all I in Source'First .. From => Source (I) = Space)
                         else Index_Non_Blank'Result in Source'First .. From
                              and then Source (Index_Non_Blank'Result) /= Space
                              and then (for all I in Index_Non_Blank'Result .. From => (if I > Index_Non_Blank'Result then Source (I) = Space))));

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

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

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

   --  how many characters are in Set
   function Count (Source : String; Set : Maps.Character_Set) return Natural
     with Global => null,
          Post => Count'Result = Count_In (Source, Set, 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 : String; Set : Maps.Character_Set; From : Positive; Test : Membership;
                         First : out Positive; Last : out Natural)
     with Global => null,
          Pre => From in Source'Range,
          Post => (if Last = 0
                   then First = From
                        and then (for all I in From .. Source'Last => Maps.Is_In (Source (I), Set) /= (Test = Inside))
                   else First in From .. Last and then Last <= Source'Last
                        and then (for all I in From .. First - 1 => Maps.Is_In (Source (I), Set) /= (Test = Inside))
                        and then (for all I in First .. Last => Maps.Is_In (Source (I), Set) = (Test = Inside))
                        and then (Last = Source'Last or else Maps.Is_In (Source (Last + 1), Set) /= (Test = Inside)));

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

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

   procedure Translate (Source : in out String; Mapping : Maps.Character_Mapping)
     with Global => null,
          Post => (for all I in Source'Range => Source (I) = Maps.Value (Mapping, Source'Old (I)));

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

   procedure Translate (Source : in out String; Mapping : Maps.Character_Mapping_Function)
     with Global => null,
          Pre => Mapping /= null;

   --  the characters Low .. High replaced by By (By inserted before Low when High < Low)
   function Replace_Slice (Source : String; Low : Positive; High : Natural; By : String) return String
     with Global => null,
          Pre => Low - 1 <= Source'Last and then (if High < Low then Low >= Source'First else High >= Source'First - 1),
          Post => Replace_Slice'Result'First = 1 and then Replace_Slice'Result'Length < Verada.Storage.Length_Bound
                  and then Replace_Slice'Result
                  = Joined (Source (Source'First .. Low - 1), By, (if High >= Source'Last then "" else Source (Integer'Max (High + 1, Low) .. Source'Last)));

   procedure Replace_Slice (Source : in out String; Low : Positive; High : Natural; By : String;
                            Drop : Truncation := Error; Justify : Alignment := Left; Pad : Character := Space)
     with Global => null,
          Pre => Low - 1 <= Source'Last and then (if High < Low then Low >= Source'First else High >= Source'First - 1)
                 and then Fits (Replace_Slice (Source, Low, High, By), Source'Length, Drop, Justify, Pad),
          Post => Source = Moved (Replace_Slice (Source'Old, Low, High, By), Source'Length, Drop, Justify, Pad);

   --  New_Item inserted before Before
   function Insert (Source : String; Before : Positive; New_Item : String) return String
     with Global => null,
          Pre => Before - 1 <= Source'Last and then Before >= Source'First,
          Post => Insert'Result'First = 1 and then Insert'Result'Length < Verada.Storage.Length_Bound
                  and then Insert'Result = Joined (Source (Source'First .. Before - 1), New_Item, Source (Before .. Source'Last));

   procedure Insert (Source : in out String; Before : Positive; New_Item : String; Drop : Truncation := Error)
     with Global => null,
          Pre => Before - 1 <= Source'Last and then Before >= Source'First
                 and then Fits (Insert (Source, Before, New_Item), Source'Length, Drop, Left, Space),
          Post => Source = Moved (Insert (Source'Old, Before, New_Item), Source'Length, Drop, Left, Space);

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

   procedure Overwrite (Source : in out String; Position : Positive; New_Item : String; Drop : Truncation := Right)
     with Global => null,
          Pre => Position - 1 <= Source'Last and then Position >= Source'First
                 and then Fits (Overwrite (Source, Position, New_Item), Source'Length, Drop, Left, Space),
          Post => Source = Moved (Overwrite (Source'Old, Position, New_Item), Source'Length, Drop, Left, Space);

   --  the characters From .. Through removed (none when Through < From)
   function Delete (Source : String; From : Positive; Through : Natural) return String
     with Global => null,
          Pre => Through < From or else (From - 1 <= Source'Last and then Through >= Source'First - 1),
          Post => Delete'Result'First = 1 and then Delete'Result'Length < Verada.Storage.Length_Bound
                  and then Delete'Result
                  = (if Through < From then Joined (Source, "", "")
                     else Joined (Source (Source'First .. From - 1), "", (if Through >= Source'Last then "" else Source (Integer'Max (Through + 1, From) .. Source'Last))));

   procedure Delete (Source : in out String; From : Positive; Through : Natural; Justify : Alignment := Left; Pad : Character := Space)
     with Global => null,
          Pre => Through < From or else (From - 1 <= Source'Last and then Through >= Source'First - 1),
          Post => Source = Moved (Delete (Source'Old, From, Through), Source'Length, Error, Justify, Pad);

   --  Source without its leading spaces (Left), trailing ones (Right) or both
   function Trim (Source : String; Side : Trim_End) return String
     with Global => null,
          Post => Trim'Result'First = 1 and then Trim'Result'Length < Verada.Storage.Length_Bound
                  and then (if Index_Non_Blank (Source) = 0 then Trim'Result'Length = 0
                   else Trim'Result
                        = Joined (Source ((if Side = Right then Source'First else Index_Non_Blank (Source))
                                          .. (if Side = Left then Source'Last else Index_Non_Blank (Source, Backward))), "", ""));

   procedure Trim (Source : in out String; Side : Trim_End; Justify : Alignment := Left; Pad : Character := Space)
     with Global => null,
          Post => Source = Moved (Trim (Source'Old, Side), Source'Length, Error, Justify, Pad);

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

   procedure Trim (Source : in out String; Left, Right : Maps.Character_Set; Justify : Alignment := Strings.Left; Pad : Character := Space)
     with Global => null,
          Post => Source = Moved (Trim (Source'Old, Left, Right), Source'Length, Error, Justify, Pad);

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

   procedure Head (Source : in out String; Count : Natural; Justify : Alignment := Left; Pad : Character := Space)
     with Global => null,
          Pre => Fits (Head (Source, Count, Pad), Source'Length, Error, Justify, Pad),
          Post => Source = Moved (Head (Source'Old, Count, Pad), Source'Length, Error, Justify, Pad);

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

   procedure Tail (Source : in out String; Count : Natural; Justify : Alignment := Left; Pad : Character := Space)
     with Global => null,
          Pre => Fits (Tail (Source, Count, Pad), Source'Length, Error, Justify, Pad),
          Post => Source = Moved (Tail (Source'Old, Count, Pad), Source'Length, Error, Justify, Pad);

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

   --  Left copies of Right: Right's characters first, then each a copy of the one Right'Length
   --  before it
   function "*" (Left : Natural; Right : String) return String
     with Global => null,
          Post => Long_Long_Long_Integer ("*"'Result'Length) = Long_Long_Long_Integer (Left) * Long_Long_Long_Integer (Right'Length)
                  and then "*"'Result'First = 1
                  and then (for all I in 1 .. "*"'Result'Length =>
                              "*"'Result (I) = (if I <= Right'Length then Right (Right'First + (I - 1)) else "*"'Result (I - Right'Length)));
end Ada.Strings.Fixed;
