--  Verada.Strings.Texts: strings of any length, copied as other values are, with no size to
--  choose. A copy shares its characters (which never change, as
--  Python's strings do not), counted by the run time, and the last holder frees them. A
--  text's characters are code points (D171): its model is its Wide_Wide_String value,
--  To_Wide_Wide_String; two texts are equal when their models are. One made from a String
--  is Latin-1, and so is the joining of two that are: such a text's String is To_String.
--  Only memory limits a text's length: it is below Verada.Storage.Length_Bound, as no memory
--  holds more, and an operation that would make a longer one raises Storage_Error, as an
--  allocation the heap cannot make does. So no operation has a precondition on lengths.
with Ada.Text_IO;
with Verada.Storage;

package Verada.Strings.Texts is
   type Text is private
     with Default_Initial_Condition => Length (Text) = 0,
          Finalizable => (Adjust => Share, Finalize => Release, Relaxed_Finalization => True),
          --  (a `for` over a text takes its characters, each a text of one, as Python's
          --  `for ch in s` does)
          Iterable => (First => Iter_First, Next => Iter_Next, Has_Element => Iter_Has_Element, Element => Iter_Element);

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

   --  room for a text of Count characters, as a result is about to be made: Storage_Error
   --  when memory cannot hold it (an operation calls it first, and needs no precondition on
   --  the length it makes)
   procedure Reserve (Count : Long_Long_Long_Integer)
     with Global => null, Always_Terminates,
          Post => Count < Verada.Storage.Length_Bound;

   --  the model: the code points, from 1
   function To_Wide_Wide_String (Source : Text) return Wide_Wide_String
     with Global => null,
          Post => To_Wide_Wide_String'Result'Length = Length (Source)
                  and then (To_Wide_Wide_String'Result'Length = 0 or else To_Wide_Wide_String'Result'First = 1)
                  and then (for all I in To_Wide_Wide_String'Result'Range => Wide_Wide_Character'Pos (To_Wide_Wide_String'Result (I)) <= 16#10FFFF#);

   --  whether every character is Latin-1 (a Character)
   function Is_Latin_1 (Source : Text) return Boolean is
     (for all I in 1 .. Length (Source) => Wide_Wide_Character'Pos (To_Wide_Wide_String (Source) (I)) <= 255)
     with Global => null;

   function To_String (Source : Text) return String
     with Global => null,
          Pre => Is_Latin_1 (Source),
          Post => To_String'Result'Length = Length (Source)
                  and then (To_String'Result'Length = 0 or else To_String'Result'First = 1)
                  and then (for all I in To_String'Result'Range => Character'Pos (To_String'Result (I)) = Wide_Wide_Character'Pos (To_Wide_Wide_String (Source) (I)));

   function To_Text (Source : String) return Text
     with Global => null,
          Post => Length (To_Text'Result) = Source'Length
                  and then Is_Latin_1 (To_Text'Result)
                  and then To_String (To_Text'Result) = Source;

   --  a text of these code points
   function To_Text_Of (Source : Wide_Wide_String) return Text
     with Global => null,
          Pre => (for all I in Source'Range => Wide_Wide_Character'Pos (Source (I)) <= 16#10FFFF#),
          Post => Length (To_Text_Of'Result) = Source'Length
                  and then To_Wide_Wide_String (To_Text_Of'Result) = Source;

   --  whether two texts are the same text, not only equal ones: the same characters, shared
   function Identical (Left, Right : Text) return Boolean
     with Ghost, Global => null;

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

   function "&" (Left, Right : Text) return Text
     with Global => null,
          Post => Length ("&"'Result) = Length (Left) + Length (Right)
                  and then To_Wide_Wide_String ("&"'Result) = To_Wide_Wide_String (Left) & To_Wide_Wide_String (Right)
                  and then (if Is_Latin_1 (Left) and then Is_Latin_1 (Right)
                            then Is_Latin_1 ("&"'Result) and then To_String ("&"'Result) = To_String (Left) & To_String (Right));

   --  Python's str ordering (`s < t` and the rest): lexicographic by code point, a proper
   --  prefix first, which is Ada's ordering of the models
   function "<" (Left, Right : Text) return Boolean
     with Global => null,
          Post => "<"'Result = (To_Wide_Wide_String (Left) < To_Wide_Wide_String (Right));

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

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

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

   --  Python's `s * n`: Left's characters Right times over, the empty text when Right <= 0
   function "*" (Left : Text; Right : Integer) return Text
     with Global => null,
          Post => Length ("*"'Result) = (if Right <= 0 then 0 else Length (Left) * Right)
                  and then (for all I in 1 .. Length ("*"'Result) =>
                              To_Wide_Wide_String ("*"'Result) (I) = To_Wide_Wide_String (Left) ((I - 1) mod Length (Left) + 1));

   --  Python's `n * s`, the same
   function "*" (Left : Integer; Right : Text) return Text
     with Global => null,
          Post => Length ("*"'Result) = (if Left <= 0 then 0 else Length (Right) * Left)
                  and then (for all I in 1 .. Length ("*"'Result) =>
                              To_Wide_Wide_String ("*"'Result) (I) = To_Wide_Wide_String (Right) ((I - 1) mod Length (Right) + 1));

   --  Python's s[i]: the text of the one character at Index
   function Item (Source : Text; Index : Positive) return Text
     with Global => null,
          Pre => Index <= Length (Source),
          Post => Length (Item'Result) = 1
                  and then To_Wide_Wide_String (Item'Result) (1) = To_Wide_Wide_String (Source) (Index);

   --  the iteration over a text's characters, by position from 1
   function Iter_First (Source : Text) return Natural is (1)
     with Global => null;

   function Iter_Next (Source : Text; Position : Natural) return Natural is
     (if Position < Natural'Last then Position + 1 else Position)
     with Global => null;

   function Iter_Has_Element (Source : Text; Position : Natural) return Boolean is
     (Position in 1 .. Length (Source))
     with Global => null;

   function Iter_Element (Source : Text; Position : Natural) return Text is
     (Item (Source, Position))
     with Global => null,
          Pre => Iter_Has_Element (Source, Position);

   --  a Latin-1 character
   function Element (Source : Text; Index : Positive) return Character
     with Global => null,
          Pre => Index <= Length (Source) and then Wide_Wide_Character'Pos (To_Wide_Wide_String (Source) (Index)) <= 255,
          Post => Character'Pos (Element'Result) = Wide_Wide_Character'Pos (To_Wide_Wide_String (Source) (Index));

   --  any character
   function Wide_Element (Source : Text; Index : Positive) return Wide_Wide_Character
     with Global => null,
          Pre => Index <= Length (Source),
          Post => Wide_Element'Result = To_Wide_Wide_String (Source) (Index);

   --  the text written to standard output, as UTF-8
   procedure Put (Source : Text)
     with Global => (In_Out => Ada.Text_IO.File_System);

   --  a copy holds the characters too (and is the same text: an array of texts copied is
   --  the array it was)
   procedure Share (X : in out Text)
     with Global => null,
          Post => Identical (X, X'Old)
                  and Length (X) = Length (X)'Old and To_Wide_Wide_String (X) = To_Wide_Wide_String (X)'Old;

   --  X no longer holds them (the last holder frees them)
   procedure Release (X : in out Text)
     with Global => null,
          Post => Length (X) = 0;

   --  the text written to standard output, as UTF-8, and the line ended
   procedure Put_Line (Source : Text)
     with Global => (In_Out => Ada.Text_IO.File_System);

private
   --  the run time's text: the address of its characters and their count, 0 for none
   type Handle is mod 2 ** 64;

   type Text is record
      Data : Handle := 0;
   end record;

   procedure Write (H : Handle)
     with Import, Convention => Ada, External_Name => "verada_rt_text_put",
          Global => (In_Out => Ada.Text_IO.File_System);

   function Text_Length (H : Handle) return Natural
     with Import, Convention => Ada, External_Name => "verada_rt_text_length", Global => null,
          Post => Text_Length'Result < Verada.Storage.Length_Bound;

   function Identical (Left, Right : Text) return Boolean is (Left.Data = Right.Data);

   --  (the empty text has no handle: its length is known without the run time)
   function Length (Source : Text) return Natural is
     (if Source.Data = 0 then 0 else Text_Length (Source.Data));
end Verada.Strings.Texts;
