DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Verada.Strings.Text_Operations

Python's str methods over copyable texts.

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

Each operation is stated over the texts' models (To_Wide_Wide_String, code points from 1) and written in Ada over them, so the same body serves Verada and GNAT. Positions count from 1, and 0 is "not found" (Python's -1). Case mapping and the character classes are CPython's on Latin-1; beyond Latin-1 no case mapping is done, no character is a digit or a letter, and the spaces are Unicode's. Split gives and Join takes a Verada.Strings.Text_Lists vector. Each result text is new; the arguments are unchanged.

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 Verada.Strings.Text_Operations;
import Verada.Strings.Text_Operations;

A Vibe module imports this unit's names with from Verada.Strings.Text_Operations 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

From_1 (declaration) — Whether a string can be a model: empty or indexed from 1. The other functions' preconditions use it.

Declared interface:

function From_1 (S : Wide_Wide_String) return Boolean is
  (S'Length = 0 or else S'First = 1);

API 002

Occurs_After (declaration) — Whether P occurs in S just after S's first Done characters, at position Done + 1.

Before this operation, establish:

From_1 (S) and then From_1 (P)

On normal return the declared contract establishes:

Occurs_After'Result = (Done <= S'Length - P'Length
                                      and then (for all K in 1 .. P'Length => S (Done + K) = P (K)))

API 003

Count_After (declaration) — Ghost: how many times P occurs in S after its first Done characters, without overlapping and each found from the left; Count, and through it Replace and Split, are stated with it.

Before this operation, establish:

From_1 (S) and then From_1 (P) and then P'Length > 0 and then Done <= S'Length

On normal return the declared contract establishes:

Long_Long_Long_Integer (Count_After'Result) * Long_Long_Long_Integer (P'Length) <= Long_Long_Long_Integer (S'Length - Done)
               and then (Count_After'Result = 0) = (for all D in Done .. S'Length - P'Length => not Occurs_After (S, P, D))

Ghost: for contracts and ghost code, and left out of the program.

API 004

First_After (declaration) — Ghost: where P first occurs in S after its first Done characters, as the number of characters before it (Find's position less 1), or -1 when it does not occur.

Before this operation, establish:

From_1 (S) and then From_1 (P) and then Done <= S'Length

On normal return the declared contract establishes:

First_After'Result in -1 .. S'Length - P'Length
               and then (if First_After'Result = -1
                         then (for all D in Done .. S'Length - P'Length => not Occurs_After (S, P, D))
                         else First_After'Result >= Done and then Occurs_After (S, P, First_After'Result)
                              and then (for all D in Done .. First_After'Result - 1 => not Occurs_After (S, P, D))
                              and then not (for all D in Done .. S'Length - P'Length => not Occurs_After (S, P, D)))

Ghost: for contracts and ghost code, and left out of the program.

API 005

Contains (declaration) — Python's p in s: whether Pattern occurs in Source; the empty text occurs in every text.

On normal return the declared contract establishes:

Contains'Result = (First_After (To_Wide_Wide_String (Source), To_Wide_Wide_String (Pattern), 0) >= 0)

API 006

Find (declaration) — Python's s.find(p) with positions from 1: the first position where Pattern occurs, 0 when it does not; the empty text occurs at 1.

On normal return the declared contract establishes:

Find'Result = First_After (To_Wide_Wide_String (Source), To_Wide_Wide_String (Pattern), 0) + 1

API 007

Index (declaration) — Python's s.index(p): as Find, of a Pattern that occurs.

Before this operation, establish:

Contains (Source, Pattern)

On normal return the declared contract establishes:

Index'Result = Find (Source, Pattern)

API 008

Count (declaration) — Python's s.count(p): the occurrences of Pattern that do not overlap, taken from the left ("aaa".count("aa") is 1); the empty text is counted Length + 1 times.

On normal return the declared contract establishes:

(if Length (Pattern) = 0 then Count'Result = Length (Source) + 1
                else Count'Result = Count_After (To_Wide_Wide_String (Source), To_Wide_Wide_String (Pattern), 0)
                     and then Count'Result <= Length (Source) / Length (Pattern)
                     and then (Count'Result = 0) = not Contains (Source, Pattern))

API 009

Starts_With (declaration) — Python's s.startswith(p).

Declared interface:

function Starts_With (Source, Prefix : Text) return Boolean is
  (Occurs_After (To_Wide_Wide_String (Source), To_Wide_Wide_String (Prefix), 0))
  with Global => null;

API 010

Ends_With (declaration) — Python's s.endswith(p).

Declared interface:

function Ends_With (Source, Suffix : Text) return Boolean is
  (Length (Suffix) <= Length (Source)
   and then Occurs_After (To_Wide_Wide_String (Source), To_Wide_Wide_String (Suffix), Length (Source) - Length (Suffix)))
  with Global => null;

API 011

Is_Digit_Character (declaration) — CPython's c.isdigit() on Latin-1: '0' .. '9' and the superscripts ², ³ and ¹; no character beyond Latin-1 is a digit here.

Declared interface:

function Is_Digit_Character (C : Wide_Wide_Character) return Boolean is
  (Wide_Wide_Character'Pos (C) in 48 .. 57 | 178 .. 179 | 185);

API 012

Is_Alpha_Character (declaration) — CPython's c.isalpha() on Latin-1: the ASCII and Latin-1 letters, and ª, µ and º; no character beyond Latin-1 is a letter here.

Declared interface:

function Is_Alpha_Character (C : Wide_Wide_Character) return Boolean is
  (Wide_Wide_Character'Pos (C) in 65 .. 90 | 97 .. 122 | 170 | 181 | 186 | 192 .. 214 | 216 .. 246 | 248 .. 255);

API 013

Is_Space_Character (declaration) — CPython's c.isspace(): the ASCII white space, the separators \x1c .. \x1f, \x85 and \xa0, and beyond Latin-1 Unicode's spaces.

Declared interface:

function Is_Space_Character (C : Wide_Wide_Character) return Boolean is
  (Wide_Wide_Character'Pos (C) in 9 .. 13 | 28 .. 32 | 133 | 160 | 16#1680# | 16#2000# .. 16#200A#
                                 | 16#2028# .. 16#2029# | 16#202F# | 16#205F# | 16#3000#);

API 014

All_Digits (declaration) — Whether every character of S is Is_Digit_Character (true of the empty string).

Declared interface:

function All_Digits (S : Wide_Wide_String) return Boolean is
  (for all I in S'Range => Is_Digit_Character (S (I)));

API 015

All_Alpha (declaration) — Whether every character of S is Is_Alpha_Character (true of the empty string).

Declared interface:

function All_Alpha (S : Wide_Wide_String) return Boolean is
  (for all I in S'Range => Is_Alpha_Character (S (I)));

API 016

All_Space (declaration) — Whether every character of S is Is_Space_Character (true of the empty string).

Declared interface:

function All_Space (S : Wide_Wide_String) return Boolean is
  (for all I in S'Range => Is_Space_Character (S (I)));

API 017

Is_Digit (declaration) — Python's s.isdigit(): not empty, and every character a digit.

Declared interface:

function Is_Digit (Source : Text) return Boolean is
  (Length (Source) > 0 and then All_Digits (To_Wide_Wide_String (Source)))
  with Global => null;

API 018

Is_Alpha (declaration) — Python's s.isalpha(): not empty, and every character a letter.

Declared interface:

function Is_Alpha (Source : Text) return Boolean is
  (Length (Source) > 0 and then All_Alpha (To_Wide_Wide_String (Source)))
  with Global => null;

API 019

Is_Space (declaration) — Python's s.isspace(): not empty, and every character a space.

Declared interface:

function Is_Space (Source : Text) return Boolean is
  (Length (Source) > 0 and then All_Space (To_Wide_Wide_String (Source)))
  with Global => null;

API 020

Upper_Of (declaration) — A character's upper case, CPython's on Latin-1 ('ÿ' is U+0178 and 'µ' is U+039C); 'ß' is itself here, since its upper case is two characters (Upper writes "SS"). Other characters are kept.

Declared interface:

function Upper_Of (C : Wide_Wide_Character) return Wide_Wide_Character is
  (if Wide_Wide_Character'Pos (C) in 97 .. 122 | 224 .. 246 | 248 .. 254
   then Wide_Wide_Character'Val (Wide_Wide_Character'Pos (C) - 32)
   elsif Wide_Wide_Character'Pos (C) = 181 then Wide_Wide_Character'Val (16#39C#)
   elsif Wide_Wide_Character'Pos (C) = 255 then Wide_Wide_Character'Val (16#178#)
   else C);

API 021

Lower_Of (declaration) — A character's lower case, CPython's on Latin-1; other characters are kept.

Declared interface:

function Lower_Of (C : Wide_Wide_Character) return Wide_Wide_Character is
  (if Wide_Wide_Character'Pos (C) in 65 .. 90 | 192 .. 214 | 216 .. 222
   then Wide_Wide_Character'Val (Wide_Wide_Character'Pos (C) + 32)
   else C);

API 022

Sharp_S (declaration) — The character 'ß', whose upper case is two characters.

Declared interface:

function Sharp_S return Wide_Wide_Character is (Wide_Wide_Character'Val (223));

API 023

Sharps (declaration) — Ghost: how many of S's first Last characters are 'ß'; Upper's length is stated with it.

Before this operation, establish:

From_1 (S) and then Last <= S'Length

On normal return the declared contract establishes:

Sharps'Result <= Last

Ghost: for contracts and ghost code, and left out of the program.

API 024

Upper (declaration) — Python's s.upper(): each character's Upper_Of, and "SS" for each 'ß', so the text grows by one for each. The postcondition gives every character of a text without 'ß'.

On normal return the declared contract establishes:

Length (Upper'Result) = Length (Source) + Sharps (To_Wide_Wide_String (Source), Length (Source))
               and then (if (for all I in 1 .. Length (Source) => To_Wide_Wide_String (Source) (I) /= Sharp_S)
                         then Length (Upper'Result) = Length (Source)
                              and then (for all I in 1 .. Length (Source) =>
                                          To_Wide_Wide_String (Upper'Result) (I) = Upper_Of (To_Wide_Wide_String (Source) (I))))

API 025

Lower (declaration) — Python's s.lower(): each character's Lower_Of, the length unchanged.

On normal return the declared contract establishes:

Length (Lower'Result) = Length (Source)
               and then (for all I in 1 .. Length (Source) =>
                           To_Wide_Wide_String (Lower'Result) (I) = Lower_Of (To_Wide_Wide_String (Source) (I)))

API 026

Is_In (declaration) — Whether C is one of Set's characters.

Declared interface:

function Is_In (C : Wide_Wide_Character; Set : Wide_Wide_String) return Boolean is
  (for some K in Set'Range => Set (K) = C);

API 027

Strip_Left (declaration) — Python's s.lstrip(): Source without the white space at its start.

On normal return the declared contract establishes:

Length (Strip_Left'Result) <= Length (Source)
               and then (for all I in 1 .. Length (Strip_Left'Result) =>
                           To_Wide_Wide_String (Strip_Left'Result) (I)
                           = To_Wide_Wide_String (Source) (I + (Length (Source) - Length (Strip_Left'Result))))
               and then (for all I in 1 .. Length (Source) - Length (Strip_Left'Result) =>
                           Is_Space_Character (To_Wide_Wide_String (Source) (I)))
               and then (Length (Strip_Left'Result) = 0
                         or else not Is_Space_Character (To_Wide_Wide_String (Strip_Left'Result) (1)))

API 028

Strip_Right (declaration) — Python's s.rstrip(): Source without the white space at its end.

On normal return the declared contract establishes:

Length (Strip_Right'Result) <= Length (Source)
               and then (for all I in 1 .. Length (Strip_Right'Result) =>
                           To_Wide_Wide_String (Strip_Right'Result) (I) = To_Wide_Wide_String (Source) (I))
               and then (for all I in 1 .. Length (Source) =>
                           (if I > Length (Strip_Right'Result) then Is_Space_Character (To_Wide_Wide_String (Source) (I))))
               and then (Length (Strip_Right'Result) = 0
                         or else not Is_Space_Character (To_Wide_Wide_String (Strip_Right'Result) (Length (Strip_Right'Result))))

API 029

Strip_Left (declaration) — Python's s.lstrip(): Source without the white space at its start.

On normal return the declared contract establishes:

Length (Strip_Left'Result) <= Length (Source)
               and then (for all I in 1 .. Length (Strip_Left'Result) =>
                           To_Wide_Wide_String (Strip_Left'Result) (I)
                           = To_Wide_Wide_String (Source) (I + (Length (Source) - Length (Strip_Left'Result))))
               and then (for all I in 1 .. Length (Source) - Length (Strip_Left'Result) =>
                           Is_Space_Character (To_Wide_Wide_String (Source) (I)))
               and then (Length (Strip_Left'Result) = 0
                         or else not Is_Space_Character (To_Wide_Wide_String (Strip_Left'Result) (1)))

API 030

Strip_Right (declaration) — Python's s.rstrip(): Source without the white space at its end.

On normal return the declared contract establishes:

Length (Strip_Right'Result) <= Length (Source)
               and then (for all I in 1 .. Length (Strip_Right'Result) =>
                           To_Wide_Wide_String (Strip_Right'Result) (I) = To_Wide_Wide_String (Source) (I))
               and then (for all I in 1 .. Length (Source) =>
                           (if I > Length (Strip_Right'Result) then Is_Space_Character (To_Wide_Wide_String (Source) (I))))
               and then (Length (Strip_Right'Result) = 0
                         or else not Is_Space_Character (To_Wide_Wide_String (Strip_Right'Result) (Length (Strip_Right'Result))))

API 031

Strip (declaration) — Python's s.strip(): Source without the white space at either end, its characters from the first kept one to the last kept one.

On normal return the declared contract establishes:

Length (Strip'Result) <= Length (Source)
               and then (for some Removed in 0 .. Length (Source) - Length (Strip'Result) =>
                           (for all I in 1 .. Length (Strip'Result) =>
                              To_Wide_Wide_String (Strip'Result) (I) = To_Wide_Wide_String (Source) (Removed + I))
                           and then (for all I in 1 .. Removed => Is_Space_Character (To_Wide_Wide_String (Source) (I)))
                           and then (for all I in 1 .. Length (Source) =>
                                       (if I > Removed + Length (Strip'Result) then Is_Space_Character (To_Wide_Wide_String (Source) (I)))))
               and then (Length (Strip'Result) = 0
                         or else (not Is_Space_Character (To_Wide_Wide_String (Strip'Result) (1))
                                  and then not Is_Space_Character (To_Wide_Wide_String (Strip'Result) (Length (Strip'Result)))))

API 032

Strip (declaration) — Python's s.strip(): Source without the white space at either end, its characters from the first kept one to the last kept one.

On normal return the declared contract establishes:

Length (Strip'Result) <= Length (Source)
               and then (for some Removed in 0 .. Length (Source) - Length (Strip'Result) =>
                           (for all I in 1 .. Length (Strip'Result) =>
                              To_Wide_Wide_String (Strip'Result) (I) = To_Wide_Wide_String (Source) (Removed + I))
                           and then (for all I in 1 .. Removed => Is_Space_Character (To_Wide_Wide_String (Source) (I)))
                           and then (for all I in 1 .. Length (Source) =>
                                       (if I > Removed + Length (Strip'Result) then Is_Space_Character (To_Wide_Wide_String (Source) (I)))))
               and then (Length (Strip'Result) = 0
                         or else (not Is_Space_Character (To_Wide_Wide_String (Strip'Result) (1))
                                  and then not Is_Space_Character (To_Wide_Wide_String (Strip'Result) (Length (Strip'Result)))))

API 033

Replace (declaration) — Python's s.replace(old, new): each occurrence of Old, without overlapping and taken from the left as Count counts them, made New_Text; an empty Old has New_Text put before each character and at the end ("ab".replace("", "-") is "-a-b-"). A result longer than memory holds is Storage_Error, as making any such text is; the postcondition gives the result's length.

On normal return the declared contract establishes:

Length (Replace'Result)
               = Length (Source) - Count (Source, Old) * Length (Old) + Count (Source, Old) * Length (New_Text)
               and then (if Length (Old) = Length (New_Text) then Length (Replace'Result) = Length (Source))
               and then (if not Contains (Source, Old) then Replace'Result = Source)

API 034

Joined_Length (declaration) — The length of Items' first Last texts joined with Sep_Length characters between each two.

Before this operation, establish:

Last <= Items'Length and then (Last = 0 or else Items'First = 1)

On normal return the declared contract establishes:

Joined_Length'Result >= 0
               and then Joined_Length'Result <= Long_Long_Long_Integer (Last) * (2 ** 63 + Verada.Storage.Length_Bound)

API 035

Joined_Length_Of (declaration) — The length of all of Items' texts joined with Sep_Length characters between each two: Joined_Length of the whole vector, named by the vector, so that a client's facts about one vector are its facts about the same vector elsewhere. Split's postcondition uses it to say that the pieces and separators make up the source.

On normal return the declared contract establishes:

Joined_Length_Of'Result
               = Joined_Length (Verada.Strings.Text_Lists.Model (Items), Sep_Length, Verada.Strings.Text_Lists.Length (Items))

API 036

Split (declaration) — Python's s.split(sep): the pieces of Source between the occurrences of Separator, as Count counts them, the empty ones too ("a,,b".split(",") is ['a', '', 'b']). The separator must not be empty.

Before this operation, establish:

Length (Separator) > 0

On normal return the declared contract establishes:

Verada.Strings.Text_Lists.Length (Split'Result) = Count (Source, Separator) + 1
               and then (for all K in 1 .. Verada.Strings.Text_Lists.Length (Split'Result) =>
                           Length (Verada.Strings.Text_Lists.Element (Split'Result, K)) <= Length (Source))
               and then Joined_Length_Of (Split'Result, Length (Separator)) = Long_Long_Long_Integer (Length (Source))

API 037

Split (declaration) — Python's s.split(sep): the pieces of Source between the occurrences of Separator, as Count counts them, the empty ones too ("a,,b".split(",") is ['a', '', 'b']). The separator must not be empty.

Before this operation, establish:

Length (Separator) > 0

On normal return the declared contract establishes:

Verada.Strings.Text_Lists.Length (Split'Result) = Count (Source, Separator) + 1
               and then (for all K in 1 .. Verada.Strings.Text_Lists.Length (Split'Result) =>
                           Length (Verada.Strings.Text_Lists.Element (Split'Result, K)) <= Length (Source))
               and then Joined_Length_Of (Split'Result, Length (Separator)) = Long_Long_Long_Integer (Length (Source))

API 038

Join (declaration) — Python's sep.join(xs): Items' texts with Separator between each two; empty for no items, and the item itself for one. A result longer than memory holds is Storage_Error, as making any such text is.

On normal return the declared contract establishes:

Long_Long_Long_Integer (Length (Join'Result)) = Joined_Length_Of (Items, Length (Separator))
               and then (if Verada.Strings.Text_Lists.Length (Items) = 0 then Length (Join'Result) = 0)
               and then (if Verada.Strings.Text_Lists.Length (Items) = 1
                         then Join'Result = Verada.Strings.Text_Lists.Element (Items, 1))

API 039

Swap_Case_Of (declaration) — A character with its case swapped: lower when it has a lower case, else upper.

Declared interface:

function Swap_Case_Of (C : Wide_Wide_Character) return Wide_Wide_Character is
  (if Lower_Of (C) /= C then Lower_Of (C) else Upper_Of (C));

API 040

Swap_Case (declaration) — Python's s.swapcase(): each character's case swapped; the length is kept.

Declared interface:

function Swap_Case (Source : Text) return Text
  with Global => null,
       Post => Length (Swap_Case'Result) = Length (Source);

API 041

Capitalize (declaration) — Python's s.capitalize(): the first character upper case, the others lower.

Declared interface:

function Capitalize (Source : Text) return Text
  with Global => null,
       Post => Length (Capitalize'Result) = Length (Source);

API 042

Title (declaration) — Python's s.title(): each word's first letter upper case and its others lower, a word being a run of letters.

Declared interface:

function Title (Source : Text) return Text
  with Global => null,
       Post => Length (Title'Result) = Length (Source);

API 043

Left_Justify (declaration) — Python's s.ljust(width, fill): padded on the right with Fill's one character to Width.

Declared interface:

function Left_Justify (Source : Text; Width : Integer; Fill : Text) return Text
  with Global => null,
       Pre => Length (Fill) = 1,
       Post => Length (Left_Justify'Result) = Integer'Max (Width, Length (Source));

API 044

Right_Justify (declaration) — Python's s.rjust(width, fill): padded on the left with Fill's one character to Width.

Declared interface:

function Right_Justify (Source : Text; Width : Integer; Fill : Text) return Text
  with Global => null,
       Pre => Length (Fill) = 1,
       Post => Length (Right_Justify'Result) = Integer'Max (Width, Length (Source));

API 045

Center (declaration) — Python's s.center(width, fill): padded on both sides to Width, the extra character placed as CPython places it.

Declared interface:

function Center (Source : Text; Width : Integer; Fill : Text) return Text
  with Global => null,
       Pre => Length (Fill) = 1,
       Post => Length (Center'Result) = Integer'Max (Width, Length (Source));

API 046

Zero_Fill (declaration) — Python's s.zfill(width): '0's after a leading sign up to Width.

Declared interface:

function Zero_Fill (Source : Text; Width : Integer) return Text
  with Global => null,
       Post => Length (Zero_Fill'Result) = Integer'Max (Width, Length (Source));

API 047

Ord (declaration) — Python's ord(c): the code point of a one-character text.

Declared interface:

function Ord (Source : Text) return Natural
  with Global => null,
       Pre => Length (Source) = 1,
       Post => Ord'Result = Wide_Wide_Character'Pos (To_Wide_Wide_String (Source) (1))
               and then Ord'Result <= 16#10FFFF#;

API 048

Chr (declaration) — Python's chr(n): the text of the one character at a code point.

Declared interface:

function Chr (Code : Integer) return Text
  with Global => null,
       Pre => Code in 0 .. 16#10FFFF#,
       Post => Length (Chr'Result) = 1
               and then Wide_Wide_Character'Pos (To_Wide_Wide_String (Chr'Result) (1)) = Code;