DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Ada.Strings.Fixed

Search, transformation and fitting operations on String (RM A.4.3).

Read the family guide for representation, selection and resource rules. The complete declaration page retains exact signatures/contracts; the source specification is the implementation baseline.

Availability and ownership

Every operation of RM A.4.3, generated with Ada.Strings.Wide_Fixed from one text (tools/library). Index and Find_Token give positions in Source; every string result is indexed from 1. Where the RM raises Pattern_Error, Index_Error or Length_Error, a precondition requires what avoids it (a nonempty pattern, a position in Source, a Move that Drop or Justify can fit, by Fits). The searches with a Character_Mapping state where the mapped pattern occurs (Occurs_After); those with a mapping function only the result's range. Its bodies are proved (test/programs/prove/library-ada-strings-fixed).

A client proof uses these contracts without proving the body again (see library assurance). Global => null excludes package-global effects, not changes made through an in out argument.

Import and instantiate

with Ada.Strings.Fixed;
import Ada.Strings.Fixed;

A Vibe module imports this unit's names with from Ada.Strings.Fixed import Name; see library use across syntaxes. In Varc, keep each name's declared spelling and write & on arguments the call changes.

Contract and lifecycle reading

A Pre condition is the caller's obligation; a Post is what a normal return guarantees. What a postcondition does not state, a client cannot assume. Ghost functions are for contracts and ghost code. The family guide describes copying, lifetime, order and cost.

API 001

Occurs_After (declaration) — 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)

Declared interface:

function Occurs_After (Source, Pattern : String; Done : Natural; Mapping : Maps.Character_Mapping) return Boolean
  with Ghost, Global => null;

API 002

Declared interface:

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

API 003

Declared interface:

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

API 004

Declared interface:

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;

API 005

Declared interface:

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;

API 006

Declared interface:

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;

API 007

Move (declaration) — Source into Target, fitted to Target's length.

Declared interface:

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

API 008

Index (declaration) — 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.

Declared interface:

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

API 009

Index (declaration) — 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.

Declared interface:

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

API 010

Index (declaration) — The first (or last) place in Source where Pattern occurs, each character of Source mapped, or 0.

Declared interface:

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

API 011

Index (declaration) — The first (or last) place in Source where Pattern occurs, each character of Source mapped, or 0.

Declared interface:

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

API 012

Index (declaration) — 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.

Declared interface:

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

API 013

Declared interface:

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

API 014

Index_Non_Blank (declaration) — The first (or last) character, from From on or up to From, that is not a space; or 0.

Declared interface:

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

API 015

Index_Non_Blank (declaration) — The first (or last) character of Source that is not a space, or 0.

Declared interface:

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

API 016

Count (declaration) — How many times Pattern occurs, without overlapping, each character of Source mapped.

Declared interface:

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

API 017

Declared interface:

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;

API 018

Count (declaration) — How many characters are in Set.

Declared interface:

function Count (Source : String; Set : Maps.Character_Set) return Natural
  with Global => null,
       Post => Count'Result = Count_In (Source, Set, 0);

API 019

Find_Token (declaration) — 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.

Declared interface:

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

API 020

Find_Token (declaration) — As Find_Token from Source'First (First = Source'First and Last = 0 when there is none)

Declared interface:

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

API 021

Translate (declaration) — Each character mapped.

Declared interface:

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

API 022

Declared interface:

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

API 023

Declared interface:

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;

API 024

Declared interface:

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

API 025

Replace_Slice (declaration) — The characters Low .. High replaced by By (By inserted before Low when High < Low)

Declared interface:

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

API 026

Declared interface:

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

API 027

Insert (declaration) — New_Item inserted before Before.

Declared interface:

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

API 028

Declared interface:

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

API 029

Overwrite (declaration) — New_Item written over the characters from Position on, the string extended where it runs past the end.

Declared interface:

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

API 030

Declared interface:

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

API 031

Delete (declaration) — The characters From .. Through removed (none when Through < From)

Declared interface:

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

API 032

Declared interface:

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

API 033

Trim (declaration) — Source without its leading spaces (Left), trailing ones (Right) or both.

Declared interface:

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

API 034

Declared interface:

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

API 035

Trim (declaration) — Source without its leading characters that are in Left and its trailing ones in Right.

Declared interface:

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

API 036

Declared interface:

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

API 037

Head (declaration) — The first Count characters of Source, padded with Pad.

Declared interface:

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

API 038

Declared interface:

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

API 039

Tail (declaration) — The last Count characters of Source, padded on the left with Pad.

Declared interface:

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

API 040

Declared interface:

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

API 041

"*" (declaration) — Left copies of Right.

Declared interface:

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

API 042

"*" (declaration) — Left copies of Right: Right's characters first, then each a copy of the one Right'Length before it.

Declared interface:

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