On this page
- Ada.Strings and fixed strings
- Ada.Strings.Bounded.Generic_Bounded_Length
- Ada.Strings.Unbounded
- Ada.Strings.Wide_Maps and the hash functions
- Verada.Strings.Unbounded
- Verada.Strings.Texts
- Verada.Strings.Text_Operations and Text_Lists
- Verada.Vibe.Formatting and Verada.Vibe.Integers
- Verada.Scanning: values read from strings
Strings and text
Verada has five kinds of text, and they are not interchangeable: Ada's String is an array, a bounded string is a value with a fixed capacity, Ada's unbounded string is a copyable value of any length, Verada's unbounded string is a limited owner of heap storage, and Text is a copyable value whose characters are shared and immutable.
Ada.Strings and fixed strings
Ada.Strings supplies Space and the Alignment, Truncation, Membership, Direction and Trim_End enumerations, which the operations below take. Verada.Strings is the namespace parent of the unbounded and Text families.
Ada.Strings.Fixed operates on String arrays, with every operation of RM A.4.3. Index searches for a nonempty Pattern, forward or backward, from the start or from a position, each of Source's characters mapped by a Character_Mapping (or a mapping function); or for a character in a Character_Set (or not in it). Index_Non_Blank searches for a non-space character. Their postconditions say where the result is: zero only when there is no match, otherwise the first (or last) place that matches (the mapped searches through Occurs_After). Count counts the non-overlapping occurrences of a pattern (Count_After) or the characters of a set (Count_In); Find_Token finds the first run of characters in a set, or not in it. Positions are Source's own.
Translate maps every character; Replace_Slice, Insert, Overwrite and Delete return the string changed, stated by Joined, the three parts joined from 1; Trim removes leading and trailing spaces, or characters of two sets; Head and Tail take or pad to Count characters; N * C and N * S repeat. Every string result is indexed from one, whatever the input's lower bound.
Each function has a procedure form that changes a String in place, fitted to its length by Move: a longer result loses the characters Drop says (or, when Drop is Error, Justify says), a shorter one is padded with Pad on the side Justify leaves (both sides, the extra on the right, for Center). Moved states the result, and Fits is Move's precondition: where Ada raises Length_Error, because characters Drop = Error would lose are not all Pad, the precondition requires that they are.
Ada.Strings.Wide_Fixed has all of these, with the same contracts, for Wide_String and Ada.Strings.Wide_Maps, padding with Wide_Space; the two are written from one text (tools/library/fixed.vads.in). Ada.Strings.Wide_Wide_Fixed has, for Wide_Wide_String, the operations that need no character sets or mappings (Index, Index_Non_Blank, Trim, Head, Tail and "*"): Ada.Strings.Wide_Wide_Maps is not in Verada's library, so neither are the operations that take one. Its classifications and case conversions are Ada.Wide_Wide_Characters.Handling's.
Ada.Strings.Bounded.Generic_Bounded_Length
Ada.Strings.Bounded contains a generic package parameterized by positive Max. Instantiate the nested generic, not its namespace parent:
with Ada.Strings.Bounded;
package Names is new Ada.Strings.Bounded.Generic_Bounded_Length (32);Varc uses package Names = new Ada.Strings.Bounded.Generic_Bounded_Length<32>; after import. Vibe's str is Text, not this type.
Max_Length exposes Max; Length_Range is 0..Max. Bounded_String is a private value with inline storage, and every operation of RM A.4.4 is there. Each is stated by its model, To_String (the characters in use, from 1), and Ada.Strings.Fixed's operation on it: To_String (Insert (S, 3, "ab")) = Fixed.Insert (To_String (S), 3, "ab"), so what is proved of a fixed string carries over. There is no Drop parameter and no truncation: a precondition requires that the result fits (New_Item'Length <= Max_Length - Length (Source) for Insert), instead of Ada's Length_Error. Equality and the comparisons compare the characters in use. Ada.Strings.Wide_Bounded is the same for Wide_String (Bounded_Wide_String, To_Bounded_Wide_String, To_Wide_String), from the same text.
A bounded string stores Max characters and a length, and copies as an ordinary value. A default-initialized one is empty, as its Default_Initial_Condition says.
Ada.Strings.Unbounded
Ada.Strings.Unbounded is RM A.4.5: an Unbounded_String is a value of any length, copied by assignment as in Ada. It holds a Text, so a copy shares the characters and the last holder frees them; nothing is copied until an operation makes a new value. Each operation's contract states its result in terms of To_String, the value's String from 1: Append and & join, Slice and Unbounded_Slice take characters by position, Index and Count search with an optional mapping (a Character_Mapping, or a function), Index of a set finds a character in or out of it, Find_Token a run of them, Translate maps each character, and Replace_Slice, Insert, Overwrite, Delete, Trim, Head, Tail and * build the strings the RM describes (the first three state their results with Joined, three strings joined from 1). The procedure forms change their argument to what the function forms return.
Where the RM raises Index_Error or Pattern_Error (a position outside the string, an empty pattern), the operation's precondition requires what avoids it, so a proved program never raises them. The operations that take a mapping function state only the result's length or range: what the function returns is not known to the proof. String_Access is a pool-specific access type, so that Free releases only what an allocator made.
with Ada.Strings.Maps.Constants;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO;
procedure Main is
Line : Unbounded_String := To_Unbounded_String ("the quick fox");
Copy : Unbounded_String;
begin
Copy := Line;
Append (Line, " jumps");
pragma Assert (To_String (Copy) = "the quick fox");
Insert (Line, 11, "brown ");
Ada.Text_IO.Put_Line (To_String (Line));
Ada.Text_IO.Put_Line (Natural'Image (Index (Line, "fox")) & Natural'Image (Count (Line, "o")));
Ada.Text_IO.Put_Line (To_String (Translate (Copy, Ada.Strings.Maps.Constants.Upper_Case_Map)));
Ada.Text_IO.Put_Line (Slice (Line, 5, 9) & "|" & To_String (Head (Copy, 5, '.')) & To_String (Tail (Copy, 15, '.')));
Ada.Text_IO.Put_Line ("[" & To_String (Trim (To_Unbounded_String (" padded "), Ada.Strings.Both)) & "]");
end Main;It writes the quick brown fox jumps, 17 2, THE QUICK FOX and quick|the q..the quick fox. The assertion is proved: Copy kept its value when Line changed.
Ada.Strings.Wide_Unbounded is RM A.4.7's Unbounded_Wide_String, over a Text too: each operation is stated by its model, To_Wide_String, and Ada.Strings.Wide_Fixed's operation on it, as Wide_Bounded's are (Code, a ghost function, gives a character's position for those contracts). Wide_Wide_Unbounded and Wide_Wide_Bounded wait, as Wide_Wide_Fixed's mapped operations do, on Wide_Wide_Maps.
Ada.Strings.Wide_Maps and the hash functions
Ada.Strings.Wide_Maps is RM A.4.7: sets of wide characters and mappings between them, as Ada.Strings.Maps has for Character, with the same contracts: To_Set's results (of a range, ranges, a character or a sequence) and not, and, or, xor and - say which characters each set holds, To_Ranges, To_Sequence, To_Domain and To_Range give them in ascending order, and To_Mapping where each character goes (From names each character at most once, a precondition where the RM raises Translation_Error). A set or a mapping is a table of the 65,536 wide characters held in a Verada.Strings.Texts.Text, so a copy shares it, as a text's copy does, and the empty set and the identity hold none; = of two sets compares the characters they hold. Wide_Maps.Wide_Constants (A.4.8) has Maps.Constants' sets and mappings as wide ones, of the Latin-1 characters only, and Character_Set, the Latin-1 characters themselves.
Ada.Strings.Hash (A.4.9) hashes a string for the hashed containers: strings with the same characters have the same hash, whatever their bounds. Hash_Case_Insensitive hashes the string with its letters in lower case (its postcondition says so), and Equal_Case_Insensitive and Less_Case_Insensitive (A.4.10) compare strings so. Each has the forms of Ada.Strings.Fixed, Bounded (a generic function given the Generic_Bounded_Length instance) and Unbounded, whose postconditions are the String form's of their characters, and Wide_Hash and Wide_Wide_Hash hash the wide strings.
with Ada.Containers;
with Ada.Strings.Equal_Case_Insensitive;
with Ada.Strings.Hash_Case_Insensitive;
with Ada.Strings.Less_Case_Insensitive;
with Ada.Strings.Wide_Maps; use Ada.Strings.Wide_Maps;
with Ada.Strings.Wide_Maps.Wide_Constants;
with Ada.Text_IO;
procedure Main is
use type Ada.Containers.Hash_Type;
Greek : constant Wide_Character_Set := To_Set (Wide_Character_Range'(Low => 'α', High => 'ω'));
Vowels : constant Wide_Character_Set := To_Set ("aeiouαεηιοωυ");
Shout : constant Wide_Character_Mapping := To_Mapping ("αβ", "ΑΒ");
begin
pragma Assert (Is_In ('ε', Greek and Vowels) and then not Is_In ('e', Greek));
Ada.Text_IO.Put_Line (Boolean'Image (Is_In ('e', Vowels - Greek))
& Boolean'Image (Is_In ('é', Ada.Strings.Wide_Maps.Wide_Constants.Letter_Set))
& Boolean'Image (Value (Shout, 'β') = 'Β'));
Ada.Text_IO.Put_Line (Boolean'Image (Ada.Strings.Equal_Case_Insensitive ("Verada", "VERADA"))
& Boolean'Image (Ada.Strings.Less_Case_Insensitive ("ada", "Verada"))
& Boolean'Image (Ada.Strings.Hash_Case_Insensitive ("Ada") = Ada.Strings.Hash_Case_Insensitive ("ADA")));
end Main;It writes TRUETRUETRUE twice. The assertion is proved from the sets' contracts.
Verada.Strings.Unbounded
The Unbounded_String type is limited and reclaimed. Default initialization is empty. Length/Element/To_String read the current contents; Element needs a present one-based index, and nonempty To_String results start at one. Set replaces the contents. Append overloads accept String or Character with finite-length preconditions. Move transfers From into To and empties From. Free empties/releases the owner, and is also the finalizer.
An Unbounded_String cannot be assigned as a whole; use Text when you need a copyable value. Set and Append allocate new storage for the whole result and free the old, so building a string one character at a time costs time proportional to the square of its length. To_String returns a copy of the characters.
Verada.Strings.Texts
Text is a nonlimited reclaimed value of Unicode code points. Default is empty. Its model is To_Wide_Wide_String, the code points from one; two texts are equal when their models are. To_Text makes a Latin-1 text from a String and To_Text_Of one from code points; To_String gives a Latin-1 text's String. Length, Item, Element, equality, the orderings "<", "<=", ">", ">=" (by code point, as the models compare), concatenation and repetition ("*", either order, empty for a count of zero or less) have content and length contracts. Only memory limits a text: Length is stated below Verada.Storage.Length_Bound (2⁶⁰), as no memory holds more, so the length of two texts joined is a Natural; an operation that would make a longer text raises Storage_Error, as an allocation the heap cannot make does. No operation has a precondition on lengths.
Copies share the characters through a reference count kept by the run-time. Share is the type's Adjust operation: a copy adds a holder, and is the same text (Identical, the ghost function that says so, lets a proof take an array of texts copied to be the array it was). Release drops a holder and empties that object; the last holder frees the characters. Assignment and scope exit call them; a program does not call Share or Release itself.
Copying a Text takes constant time; creating, concatenating and converting one allocates and copies the characters. Vibe's str is this Text; the Vibe reference lists the string methods it has.
Verada.Strings.Text_Operations and Text_Lists
Text_Operations holds Python's string methods over texts, written over the model: Contains, Find (0 for none), Index, Count (non-overlapping), Starts_With, Ends_With, Replace, Upper and Lower (CPython's mappings on Latin-1, other code points kept), Is_Digit, Is_Alpha and Is_Space (CPython's classes on Latin-1), Strip, Strip_Left and Strip_Right (of whitespace, or of given characters), Split (by a separator, or by whitespace) and Join. Positions count from one; Vibe gives Python's from zero. Occurs_After, First_After and Count_After are the ghost functions the contracts state occurrences with, and Joined_Length_Of the length of a join: a split's pieces and separators make up its source, so Join (Sep, Split (S, Sep)) has S's length. None has a precondition on lengths: a result longer than memory holds is Storage_Error (Reserve, which Replace and Join call first, raises it).
Text_Lists is the unbounded vector of texts, Vibe's list[str], which Split gives and Join takes.
Verada.Vibe.Formatting and Verada.Vibe.Integers
Formatting is Python's format mini-language: Format of an Integer, a Long_Float or a Text by a Spec, the record of a spec's fields (fill, alignment, sign, z, #, 0, width, grouping, precision, type) that the compiler reads from the literal spec. Valid_For_Integer, Valid_For_Float and Valid_For_Text are the preconditions: the specs Python accepts for each type. The results are CPython's; a float's digits come correctly rounded from the run-time. Repr is Python's repr of a str. Is_Integer_Text says whether Python's int() reads a text within Integer's range, and To_Integer reads it, its value stated over the digits.
Integers has Shift_Left and Shift_Right, Python's << and >> on Integer: a product by a power of two, overflow a precondition, and a division rounding down; and Bit_And, Bit_Or and Bit_Xor, Python's &, | and ^ of ints in two's complement. Floats has Python's %, //, round and ** of floats, as CPython computes them. Unicode has CPython's character classes and case mappings of all of Unicode, which the str methods use.
Verada.Scanning: values read from strings
Verada.Scanning reads the values that T'Value and Text_IO's numeric Get read, and its proofs are part of the library's. Each kind of value has a query and a value function. The value function's precondition is the query, and its postcondition gives the value. A program normally reaches them through the attributes: T'Valid_Value (S) asks the query for the subtype T, and T'Value (S) calls the value function, whose precondition proof then discharges. They can also be called directly.
| Kind | Query | Value | What it reads |
|---|---|---|---|
| Integers | Is_Integer (S, First, Last, Modular) |
Integer_Value |
Spaces, a sign (no minus for a modular type), a decimal or based literal with single underscores and an exponent without a minus, and a value in First .. Last |
| Reals | Is_Real (S, First, Last, Single) |
Real_Value |
As for integers, with a point between two numerals and a signed exponent. The value is the literal's exact value correctly rounded to single or double precision, to nearest with a half to even |
| Fixed point | Is_Fixed (S, First, Last, Num, Den) |
Fixed_Value |
As for reals. The value is a count of the type's small, Num / Den, between First and Last: the literal's exact value divided by the small, truncated toward zero as a conversion to a fixed-point type is |
| Enumeration values | Is_Enumeration (S, Names, Ends, First, Last) |
Enumeration_Value |
One of the literals in a table of images, an identifier in any case or a character literal as written. The result is its position |
| Characters | Is_Character (S, First, Last) |
Character_Value |
A character literal, a control character's name as Character'Image writes it (NUL), or a code point in hexadecimal (["03B1"]). The result is its position |
Each takes a String, a Wide_String or a Wide_Wide_String, widened character for character (Widened). Integer values are counted in Long_Long_Long_Integer, which holds every integer and modular type's values, so no type's value is out of reach. A value's ghost model (Integer_Model, Run, Scaled) is in the specification, for proofs that need more than the value's range.
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
subtype Percent is Integer range 0 .. 100;
procedure Show (Text : String) is
begin
if Percent'Valid_Value (Text) then
Put_Line (Percent'Image (Percent'Value (Text)));
else
Put_Line (Text & " is not a percentage");
end if;
end Show;
begin
Show (" 42 ");
Show ("16#64#");
Show ("101");
Show ("4.5");
end Main;