DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Package Ada.Strings.Wide_Fixed

Verada predefined library: Ada.Strings.Wide_Fixed (Ada RM A.4.3, A.4.7), 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.vb, one text for the three widths.)

Occurs_After

function Occurs_After (Source, Pattern : Wide_String; Done : Natural; Mapping : Wide_Maps.Wide_Character_Mapping) return Boolean
  with Ghost, Global => null;

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)

Count_After

function Count_After (Source, Pattern : Wide_String; Done : Natural; Mapping : Wide_Maps.Wide_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 times Pattern occurs after Source's first Done characters without overlapping, each found from the left

Count_In

function Count_In (Source : Wide_String; Set : Wide_Maps.Wide_Character_Set; Done : Natural) return Natural is
  (if Done >= Source'Length then 0
   else (if Wide_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);

how many characters after Source's first Done characters are in Set

Joined

function Joined (A, B, C : Wide_String) return Wide_String is
  (Wide_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;

A, B and C joined, from 1 (the results of Replace_Slice, Insert and Overwrite)

Moved

function Moved (Source : Wide_String; Length : Natural; Drop : Truncation; Justify : Alignment; Pad : Wide_Character) return Wide_String is
  (Wide_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;

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)

Fits

function Fits (Source : Wide_String; Length : Natural; Drop : Truncation; Justify : Alignment; Pad : Wide_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;

whether Move may fit Source to Length characters: where the RM raises Length_Error, the characters Drop = Error would lose are not all Pad

Move

procedure Move (Source : Wide_String; Target : out Wide_String; Drop : Truncation := Error; Justify : Alignment := Left; Pad : Wide_Character := Wide_Space)
  with Global => null,
       Pre => Fits (Source, Target'Length, Drop, Justify, Pad),
       Post => Target = Moved (Source, Target'Length, Drop, Justify, Pad);

Source into Target, fitted to Target's length

Index

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

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

Index

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

Index

function Index (Source : 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 => (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 = Wide_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))));

the first (or last) place in Source where Pattern occurs, each character of Source mapped, or 0

Index

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

Index

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

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

Index

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

Index_Non_Blank

function Index_Non_Blank (Source : Wide_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) = Wide_Space)
                      else Index_Non_Blank'Result in From .. Source'Last
                           and then Source (Index_Non_Blank'Result) /= Wide_Space
                           and then (for all I in From .. Index_Non_Blank'Result - 1 => Source (I) = Wide_Space))
                else (if Index_Non_Blank'Result = 0
                      then (for all I in Source'First .. From => Source (I) = Wide_Space)
                      else Index_Non_Blank'Result in Source'First .. From
                           and then Source (Index_Non_Blank'Result) /= Wide_Space
                           and then (for all I in Index_Non_Blank'Result .. From => (if I > Index_Non_Blank'Result then Source (I) = Wide_Space))));

the first (or last) character, from From on or up to From, that is not a space; or 0

Index_Non_Blank

function Index_Non_Blank (Source : Wide_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) = Wide_Space)
                else Index_Non_Blank'Result in Source'Range
                     and then Source (Index_Non_Blank'Result) /= Wide_Space
                     and then (if Going = Forward
                               then (for all I in Source'Range => (if I < Index_Non_Blank'Result then Source (I) = Wide_Space))
                               else (for all I in Source'Range => (if I > Index_Non_Blank'Result then Source (I) = Wide_Space))));

the first (or last) character of Source that is not a space, or 0

Count

function Count (Source : 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 = Count_After (Source, Pattern, 0, Mapping);

how many times Pattern occurs, without overlapping, each character of Source mapped

Count

function Count (Source : 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 <= Source'Length;

Count

function Count (Source : Wide_String; Set : Wide_Maps.Wide_Character_Set) return Natural
  with Global => null,
       Post => Count'Result = Count_In (Source, Set, 0);

how many characters are in Set

Find_Token

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

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

Find_Token

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

Translate

function Translate (Source : Wide_String; Mapping : Wide_Maps.Wide_Character_Mapping) return Wide_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) = Wide_Maps.Value (Mapping, Source (Source'First + (I - 1))));

each character mapped

Translate

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

Translate

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

Translate

procedure Translate (Source : in out Wide_String; Mapping : Wide_Maps.Wide_Character_Mapping_Function)
  with Global => null,
       Pre => Mapping /= null;

Replace_Slice

function Replace_Slice (Source : Wide_String; Low : Positive; High : Natural; By : Wide_String) return Wide_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)));

the characters Low .. High replaced by By (By inserted before Low when High < Low)

Replace_Slice

procedure Replace_Slice (Source : in out Wide_String; Low : Positive; High : Natural; By : Wide_String;
                         Drop : Truncation := Error; Justify : Alignment := Left; Pad : Wide_Character := Wide_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);

Insert

function Insert (Source : Wide_String; Before : Positive; New_Item : Wide_String) return Wide_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));

New_Item inserted before Before

Insert

procedure Insert (Source : in out Wide_String; Before : Positive; New_Item : Wide_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, Wide_Space),
       Post => Source = Moved (Insert (Source'Old, Before, New_Item), Source'Length, Drop, Left, Wide_Space);

Overwrite

function Overwrite (Source : Wide_String; Position : Positive; New_Item : Wide_String) return Wide_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)));

New_Item written over the characters from Position on, the string extended where it runs past the end

Overwrite

procedure Overwrite (Source : in out Wide_String; Position : Positive; New_Item : Wide_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, Wide_Space),
       Post => Source = Moved (Overwrite (Source'Old, Position, New_Item), Source'Length, Drop, Left, Wide_Space);

Delete

function Delete (Source : Wide_String; From : Positive; Through : Natural) return Wide_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))));

the characters From .. Through removed (none when Through < From)

Delete

procedure Delete (Source : in out Wide_String; From : Positive; Through : Natural; Justify : Alignment := Left; Pad : Wide_Character := Wide_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);

Trim

function Trim (Source : Wide_String; Side : Trim_End) return Wide_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))), "", ""));

Source without its leading spaces (Left), trailing ones (Right) or both

Trim

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

Trim

function Trim (Source : Wide_String; Left, Right : Wide_Maps.Wide_Character_Set) return Wide_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)), "", ""));

Source without its leading characters that are in Left and its trailing ones in Right

Trim

procedure Trim (Source : in out Wide_String; Left, Right : Wide_Maps.Wide_Character_Set; Justify : Alignment := Strings.Left; Pad : Wide_Character := Wide_Space)
  with Global => null,
       Post => Source = Moved (Trim (Source'Old, Left, Right), Source'Length, Error, Justify, Pad);
function Head (Source : Wide_String; Count : Natural; Pad : Wide_Character := Wide_Space) return Wide_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));

the first Count characters of Source, padded with Pad

Head

procedure Head (Source : in out Wide_String; Count : Natural; Justify : Alignment := Left; Pad : Wide_Character := Wide_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);

Tail

function Tail (Source : Wide_String; Count : Natural; Pad : Wide_Character := Wide_Space) return Wide_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));

the last Count characters of Source, padded on the left with Pad

Tail

procedure Tail (Source : in out Wide_String; Count : Natural; Justify : Alignment := Left; Pad : Wide_Character := Wide_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);
function "*" (Left : Natural; Right : Wide_Character) return Wide_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

function "*" (Left : Natural; Right : Wide_String) return Wide_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)));

Left copies of Right: Right's characters first, then each a copy of the one Right'Length before it