On this page
- From_1
- Occurs_After
- Count_After
- First_After
- Contains
- Find
- Index
- Count
- Starts_With
- Ends_With
- Is_Digit_Character
- Is_Alpha_Character
- Is_Space_Character
- All_Digits
- All_Alpha
- All_Space
- Is_Digit
- Is_Alpha
- Is_Space
- Upper_Of
- Lower_Of
- Sharp_S
- Sharps
- Upper
- Lower
- Is_In
- Strip_Left
- Strip_Right
- Strip_Left
- Strip_Right
- Strip
- Strip
- Replace
- Joined_Length
- Joined_Length_Of
- Split
- Split
- Join
- Swap_Case_Of
- Swap_Case
- Capitalize
- Title
- Left_Justify
- Right_Justify
- Center
- Zero_Fill
- Ord
- Chr
Package Verada.Strings.Text_Operations
Verada.Strings.Text_Operations: Python's str methods on texts (s.find(p), s.split(sep), s.upper() and the rest). Each is stated on the texts' models, their code points from 1 (To_Wide_Wide_String), and written in Ada over them, so that the same body serves Verada and GNAT. Positions are from 1, and 0 is "not found" (Python's -1). Case mapping and the character classes are CPython's on Latin-1 (code points 0 .. 255); beyond Latin-1 no case mapping is done, no character is a digit or a letter, and the spaces are Unicode's.
From_1
function From_1 (S : Wide_Wide_String) return Boolean is
(S'Length = 0 or else S'First = 1);whether the string is a model: empty or from 1
Occurs_After
function Occurs_After (S, P : Wide_Wide_String; Done : Natural) return Boolean
with Global => null,
Pre => From_1 (S) and then From_1 (P),
Post => Occurs_After'Result = (Done <= S'Length - P'Length
and then (for all K in 1 .. P'Length => S (Done + K) = P (K)));P occurs in S after S's first Done characters (at position Done + 1)
Count_After
function Count_After (S, P : Wide_Wide_String; Done : Natural) return Natural is
(if Done > S'Length - P'Length then 0
elsif Occurs_After (S, P, Done) then 1 + Count_After (S, P, Done + P'Length)
else Count_After (S, P, Done + 1))
with Ghost,
Pre => From_1 (S) and then From_1 (P) and then P'Length > 0 and then Done <= S'Length,
Post => 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)),
Subprogram_Variant => (Decreases => S'Length - Done);how many times P occurs in S after its first Done characters, without overlapping, each found from the left (what s.count(p) counts, and s.replace and s.split take)
First_After
function First_After (S, P : Wide_Wide_String; Done : Natural) return Integer is
(if Done > S'Length - P'Length then -1
elsif Occurs_After (S, P, Done) then Done
else First_After (S, P, Done + 1))
with Ghost,
Pre => From_1 (S) and then From_1 (P) and then Done <= S'Length,
-- (as one case split: none from Done on, or the first from Done on; each quantifier's
-- range then a plain one)
Post => 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))),
Subprogram_Variant => (Decreases => S'Length - Done);where P first occurs in S after its first Done characters, as the characters before it (Find's position less 1), or -1 when it does not
Contains
function Contains (Source, Pattern : Text) return Boolean
with Global => null,
Post => Contains'Result = (First_After (To_Wide_Wide_String (Source), To_Wide_Wide_String (Pattern), 0) >= 0);Python's p in s: whether Pattern occurs in Source (the empty text occurs in every one); where it occurs first is First_After's
Find
function Find (Source, Pattern : Text) return Natural
with Global => null,
Post => Find'Result = First_After (To_Wide_Wide_String (Source), To_Wide_Wide_String (Pattern), 0) + 1;Python's s.find(p): the first position where Pattern occurs, 0 when it does not (the empty text occurs at 1)
Index
function Index (Source, Pattern : Text) return Positive
with Global => null,
Pre => Contains (Source, Pattern),
Post => Index'Result = Find (Source, Pattern);Python's s.index(p): as Find, of a Pattern that occurs
Count
function Count (Source, Pattern : Text) return Natural
with Global => null,
Post => (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));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
Starts_With
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;Python's s.startswith(p)
Ends_With
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;Python's s.endswith(p)
Is_Digit_Character
function Is_Digit_Character (C : Wide_Wide_Character) return Boolean is
(Verada.Strings.Unicode.Digit_Code (Wide_Wide_Character'Pos (C)) = 1);the character classes, CPython's (c.isdigit(), c.isalpha(), c.isspace()), by Verada.Strings.Unicode
Is_Alpha_Character
function Is_Alpha_Character (C : Wide_Wide_Character) return Boolean is
(Verada.Strings.Unicode.Alpha_Code (Wide_Wide_Character'Pos (C)) = 1);Is_Space_Character
function Is_Space_Character (C : Wide_Wide_Character) return Boolean is
(Verada.Strings.Unicode.Space_Code (Wide_Wide_Character'Pos (C)) = 1);All_Digits
function All_Digits (S : Wide_Wide_String) return Boolean is
(for all I in S'Range => Is_Digit_Character (S (I)));All_Alpha
function All_Alpha (S : Wide_Wide_String) return Boolean is
(for all I in S'Range => Is_Alpha_Character (S (I)));All_Space
function All_Space (S : Wide_Wide_String) return Boolean is
(for all I in S'Range => Is_Space_Character (S (I)));Is_Digit
function Is_Digit (Source : Text) return Boolean is
(Length (Source) > 0 and then All_Digits (To_Wide_Wide_String (Source)))
with Global => null;Python's s.isdigit(): not empty, and every character a digit
Is_Alpha
function Is_Alpha (Source : Text) return Boolean is
(Length (Source) > 0 and then All_Alpha (To_Wide_Wide_String (Source)))
with Global => null;Python's s.isalpha()
Is_Space
function Is_Space (Source : Text) return Boolean is
(Length (Source) > 0 and then All_Space (To_Wide_Wide_String (Source)))
with Global => null;Python's s.isspace()
Upper_Of
function Upper_Of (C : Wide_Wide_Character) return Wide_Wide_Character is
(if Wide_Wide_Character'Pos (C) <= 16#10FFFF# then Wide_Wide_Character'Val (Verada.Strings.Unicode.Upper_Code (Wide_Wide_Character'Pos (C))) else C);a character's upper and lower case, CPython's where it is one character (Verada.Strings.Unicode; 'ß', whose upper case is "SS", is itself here, and Upper writes its two)
Lower_Of
function Lower_Of (C : Wide_Wide_Character) return Wide_Wide_Character is
(if Wide_Wide_Character'Pos (C) <= 16#10FFFF# then Wide_Wide_Character'Val (Verada.Strings.Unicode.Lower_Code (Wide_Wide_Character'Pos (C))) else C);Sharp_S
function Sharp_S return Wide_Wide_Character is (Wide_Wide_Character'Val (223));'ß', whose upper case is two characters
Sharps
function Sharps (S : Wide_Wide_String; Last : Natural) return Natural is
(if Last = 0 then 0 else Sharps (S, Last - 1) + (if S (Last) = Sharp_S then 1 else 0))
with Ghost,
Pre => From_1 (S) and then Last <= S'Length,
Post => Sharps'Result <= Last,
Subprogram_Variant => (Decreases => Last);how many of S's first Last characters are 'ß'
Upper
function Upper (Source : Text) return Text
with Global => null,
Post => Length (Upper'Result) = Length (Source) + Sharps (To_Wide_Wide_String (Source), Length (Source))
-- (a text without 'ß', character by character)
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))));Python's s.upper(): each character's Upper_Of, and "SS" for each 'ß' (so the text grows by one for each); beyond Latin-1 the characters are kept
Lower
function Lower (Source : Text) return Text
with Global => null,
Post => 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)));Python's s.lower(): each character's Lower_Of
Is_In
function Is_In (C : Wide_Wide_Character; Set : Wide_Wide_String) return Boolean is
(for some K in Set'Range => Set (K) = C);whether C is one of Set's characters
Strip_Left
function Strip_Left (Source : Text) return Text
with Global => null,
Post => 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)));Python's s.lstrip(): Source without the spaces at its start
Strip_Right
function Strip_Right (Source : Text) return Text
with Global => null,
Post => 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))));Python's s.rstrip(): Source without the spaces at its end
Strip_Left
function Strip_Left (Source, Chars : Text) return Text
with Global => null,
Post => 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_In (To_Wide_Wide_String (Source) (I), To_Wide_Wide_String (Chars)))
and then (Length (Strip_Left'Result) = 0
or else not Is_In (To_Wide_Wide_String (Strip_Left'Result) (1), To_Wide_Wide_String (Chars)));Python's s.lstrip(chars): Source without the characters of Chars at its start
Strip_Right
function Strip_Right (Source, Chars : Text) return Text
with Global => null,
Post => 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_In (To_Wide_Wide_String (Source) (I), To_Wide_Wide_String (Chars))))
and then (Length (Strip_Right'Result) = 0
or else not Is_In (To_Wide_Wide_String (Strip_Right'Result) (Length (Strip_Right'Result)),
To_Wide_Wide_String (Chars)));Python's s.rstrip(chars)
Strip
function Strip (Source : Text) return Text
with Global => null,
Post => 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)))));Python's s.strip(): Source without the spaces at either end, the characters from its first kept one (after Removed of them) to its last kept one
Strip
function Strip (Source, Chars : Text) return Text
with Global => null,
Post => 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_In (To_Wide_Wide_String (Source) (I), To_Wide_Wide_String (Chars)))
and then (for all I in 1 .. Length (Source) =>
(if I > Removed + Length (Strip'Result) then Is_In (To_Wide_Wide_String (Source) (I), To_Wide_Wide_String (Chars)))))
and then (Length (Strip'Result) = 0
or else (not Is_In (To_Wide_Wide_String (Strip'Result) (1), To_Wide_Wide_String (Chars))
and then not Is_In (To_Wide_Wide_String (Strip'Result) (Length (Strip'Result)),
To_Wide_Wide_String (Chars))));Python's s.strip(chars)
Replace
function Replace (Source, Old, New_Text : Text) return Text
with Global => null,
Post => 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);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)
Joined_Length
function Joined_Length (Items : Verada.Strings.Text_Lists.Element_Array; Sep_Length, Last : Natural)
return Long_Long_Long_Integer is
(if Last = 0 then 0
else Joined_Length (Items, Sep_Length, Last - 1) + Long_Long_Long_Integer (Length (Items (Last)))
+ (if Last > 1 then Long_Long_Long_Integer (Sep_Length) else 0))
with Global => null,
Pre => Last <= Items'Length and then (Last = 0 or else Items'First = 1),
Post => Joined_Length'Result >= 0
and then Joined_Length'Result <= Long_Long_Long_Integer (Last) * (2 ** 63 + Verada.Storage.Length_Bound),
Subprogram_Variant => (Decreases => Last);the length of Items' first Last texts joined, with Sep_Length characters between each two
Joined_Length_Of
function Joined_Length_Of (Items : Verada.Strings.Text_Lists.Vector; Sep_Length : Natural) return Long_Long_Long_Integer
with Global => null,
Post => Joined_Length_Of'Result
= Joined_Length (Verada.Strings.Text_Lists.Model (Items), Sep_Length, Verada.Strings.Text_Lists.Length (Items));the length of Items' texts joined, with Sep_Length characters between each two (Joined_Length of them all, named by the vector, so that a client's facts about one vector are its facts about the same vector elsewhere)
Split
function Split (Source, Separator : Text) return Verada.Strings.Text_Lists.Vector
with Global => null,
Pre => Length (Separator) > 0,
Post => 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))
-- (the pieces and the separators between them make up Source)
and then Joined_Length_Of (Split'Result, Length (Separator)) = Long_Long_Long_Integer (Length (Source));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'])
Split
function Split (Source : Text) return Verada.Strings.Text_Lists.Vector
with Global => null,
Post => Verada.Strings.Text_Lists.Length (Split'Result) <= Length (Source)
-- (the pieces are apart in Source, a character at least between each two)
and then Joined_Length_Of (Split'Result, 1) <= Long_Long_Long_Integer (Length (Source))
and then (for all K in 1 .. Verada.Strings.Text_Lists.Length (Split'Result) =>
Length (Verada.Strings.Text_Lists.Element (Split'Result, K)) > 0
and then (for all I in 1 .. Length (Verada.Strings.Text_Lists.Element (Split'Result, K)) =>
not Is_Space_Character
(To_Wide_Wide_String (Verada.Strings.Text_Lists.Element (Split'Result, K)) (I))));Python's s.split(): the runs of characters that are not spaces (" a b ".split() is ['a', 'b'], "".split() is [])
Join
function Join (Separator : Text; Items : Verada.Strings.Text_Lists.Vector) return Text
with Global => null,
Post => 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));Python's sep.join(xs): Items' texts, with Separator between each two (a result longer than memory holds is Storage_Error, as making any such text is)
Swap_Case_Of
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));Python's s.swapcase(): each upper-case character lower, each lower-case one upper
Swap_Case
function Swap_Case (Source : Text) return Text
with Global => null,
Post => Length (Swap_Case'Result) = Length (Source);Capitalize
function Capitalize (Source : Text) return Text
with Global => null,
Post => Length (Capitalize'Result) = Length (Source);Python's s.capitalize(): the first character upper case, the others lower
Title
function Title (Source : Text) return Text
with Global => null,
Post => Length (Title'Result) = Length (Source);Python's s.title(): each word's first letter upper case, its others lower (a word is a run of letters)
Left_Justify
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));Python's s.ljust(width, fill), s.rjust(width, fill) and s.center(width, fill): the text padded with Fill (one character) to Width, on the right, the left, or both
Right_Justify
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));Center
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));Zero_Fill
function Zero_Fill (Source : Text; Width : Integer) return Text
with Global => null,
Post => Length (Zero_Fill'Result) = Integer'Max (Width, Length (Source));Python's s.zfill(width): '0's after a leading sign up to Width
Ord
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#;Python's ord(c): the code point of a text of one character
Chr
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;Python's chr(n): the text of the one character at code point N