DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Package Verada.Strings.Texts

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.

Text

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

Length

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

Reserve

procedure Reserve (Count : Long_Long_Long_Integer)
  with Global => null, Always_Terminates,
       Post => Count < 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)

To_Wide_Wide_String

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

the model: the code points, from 1

Is_Latin_1

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;

whether every character is Latin-1 (a Character)

To_String

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

To_Text

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;

To_Text_Of

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;

a text of these code points

Identical

function Identical (Left, Right : Text) return Boolean
  with Ghost, Global => null;

whether two texts are the same text, not only equal ones: the same characters, shared

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));
function "<" (Left, Right : Text) return Boolean
  with Global => null,
       Post => "<"'Result = (To_Wide_Wide_String (Left) < To_Wide_Wide_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 : 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 s * n: Left's characters Right times over, the empty text when Right <= 0

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 n * s, the same

Item

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

Python's s[i]: the text of the one character at Index

Iter_First

function Iter_First (Source : Text) return Natural is (1)
  with Global => null;

the iteration over a text's characters, by position from 1

Iter_Next

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

Iter_Has_Element

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

Iter_Element

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

Element

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

a Latin-1 character

Wide_Element

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

any character

Put

procedure Put (Source : Text)
  with Global => (In_Out => Ada.Text_IO.File_System);

the text written to standard output, as UTF-8

Share

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;

a copy holds the characters too (and is the same text: an array of texts copied is the array it was)

Release

procedure Release (X : in out Text)
  with Global => null,
       Post => Length (X) = 0;

X no longer holds them (the last holder frees them)

Put_Line

procedure Put_Line (Source : Text)
  with Global => (In_Out => Ada.Text_IO.File_System);

the text written to standard output, as UTF-8, and the line ended