DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Characters, classification and character tables

Verada Character values are the Latin-1 positions 0 .. 255, whatever the source file's UTF-8 encoding. These libraries are fixed tables and definitions over those values; they do no locale-dependent processing or Unicode normalization. Ada.Wide_Wide_Characters.Handling classifies and converts characters of all of Unicode.

Ada.Characters and Ada.Characters.Latin_1

Ada.Characters is a namespace parent with no operations of its own. The Latin_1 unit supplies every name RM A.3.3 gives, 228 Character constants by position: the control characters (NUL .. US and DEL, and ISO 6429's Reserved_128 .. APC with IS4 .. IS1), the punctuation, LC_A through LC_Z, and the upper half's symbols and letters (UC_A_Grave .. LC_Y_Diaeresis). Aliases such as NBSP/No_Break_Space, Minus_Sign/Hyphen and Ring_Above/Degree_Sign share a position.

Vada imports with with Ada.Characters.Latin_1;; Varc uses import Ada.Characters.Latin_1; and preserves member spelling. Vibe's str is Text, not an array of Character.

Ada.Characters.Handling

The handling API defines classifiers as visible expression functions, so exact sets are part of what the prover can see:

Scalar To_Upper subtracts 32 in its supported lowercase ranges, leaving values such as ß and ÿ unchanged when the corresponding uppercase cannot be represented by that simple Latin-1 rule. To_Lower maps supported uppercase letters by adding 32. To_Basic takes a letter with a diacritical mark to its basic letter ('é' to 'e') and leaves every other character as it is. To_ISO_646 gives Substitute (a space unless given) for a character above 127. The string overloads apply the character conversion to each character; as in Ada, the result is indexed from 1, whatever the argument's bounds. There is no locale, case-fold expansion or Unicode case mapping here.

Ada.Characters.Conversions

Conversions moves characters and strings between Character, Wide_Character and Wide_Wide_Character by code point. Widening always succeeds. Narrowing gives Substitute for a code point the narrower type does not hold, and Is_Character, Is_Wide_Character, Is_String and Is_Wide_String say beforehand whether it would be needed. Every function is an expression function, and each string result is indexed from 1 with the argument's length, which the postconditions state.

with Ada.Characters.Conversions;
with Ada.Characters.Handling;
with Ada.Strings.Maps.Constants;
with Ada.Text_IO;

procedure Main is
   use Ada.Characters;
   Wide : constant Wide_String := Conversions.To_Wide_String ("caf" & Character'Val (233));
   Plain : constant String := Handling.To_Basic (Conversions.To_String (Wide));
begin
   pragma Assert (Plain'First = 1 and then Plain'Length = 4);
   Ada.Text_IO.Put_Line (Plain);
   Ada.Text_IO.Put_Line (Handling.To_Upper (Plain (2 .. 4)));
   Ada.Text_IO.Put_Line (Boolean'Image (Handling.Is_ISO_646 (Plain)));
   Ada.Text_IO.Put_Line (Boolean'Image (Ada.Strings.Maps.Is_In ('7', Ada.Strings.Maps.Constants.Decimal_Digit_Set)));
   Ada.Text_IO.Put_Line (Conversions.To_String (Conversions.To_Wide_Wide_String (String'("x")) & Wide_Wide_Character'Val (16#1F600#), Substitute => '?'));
end Main;

It writes cafe, AFE, TRUE, TRUE and x?. The assertion is proved from To_Basic's postcondition.

The scalar classifiers and conversions take constant time; the string overloads traverse the string.

Ada.Characters.Handling's conversions to and from the wide types, which Ada's Annex J.14 declares there as renamings of these, are taken only by their modern names: a program that writes Ada.Characters.Handling.To_Wide_String (or names it through a use clause of Handling) is told the name is obsolescent (B0903), with the fix that writes Ada.Characters.Conversions.To_Wide_String, as Verada does for Annex J's other forms.

Ada.Wide_Wide_Characters.Handling

Wide_Wide_Characters.Handling has the classifications and conversions of Ada.Characters.Handling for Wide_Wide_Character, over Unicode 16.0.0 (Character_Set_Version says which), by each character's general category: Is_Control is Cc; Is_Letter the letters (Lu, Ll, Lt, Lm, Lo) and letter numbers (Nl); Is_Upper Lu and Is_Lower Ll; Is_Digit and Is_Decimal_Digit a decimal digit of any script (Nd); Is_Hexadecimal_Digit adds A..F and a..f; Is_Mark the combining marks (Mn, Mc); Is_Other_Format Cf; Is_Punctuation_Connector Pc; Is_Space the space separators (Zs); Is_Line_Terminator Zl, Zp, LF, VT, FF, CR and NEL. Is_Graphic holds of every character but controls, private-use and surrogate code points, the line and paragraph separators and the last two positions of each plane; Is_Special is graphic and not alphanumeric. Verada adds Category, a character's general category (Verada.Strings.Unicode.General_Category).

To_Upper and To_Lower are Unicode's simple case mappings, one character for one: 'ß' has no one-character upper case and stays, 'ÿ' becomes 'Ÿ' (U+0178) and 'µ' the Greek 'Μ', which Character could not hold. To_Basic takes a letter that decomposes into a letter and combining marks to that letter, all its marks removed ('Ǻ' to 'A'); Is_Basic holds of a letter To_Basic leaves as it is and that has no compatibility decomposition (so not 'ª' or the ligature 'fi'). 'Ø' has no decomposition, so it is basic here, as Ada's Wide_Characters rule has it, where Ada.Characters.Handling takes it to 'O'. Is_NFKC holds of a character that normalization form KC leaves as it is. The string forms convert each character, the result indexed from 1.

Every function is an expression function. Of a Latin-1 character the prover sees the exact answer, which Verada.Strings.Unicode writes out; of any other it sees a call of the run time's tables, the same at every call.

with Ada.Strings.Wide_Wide_Fixed;
with Ada.Wide_Wide_Characters.Handling;
with Ada.Wide_Wide_Text_IO;

procedure Main is
   use Ada.Wide_Wide_Characters.Handling;
   Word : constant Wide_Wide_String := "  Ǻngström σοφία  ";
   Plain : constant Wide_Wide_String := Ada.Strings.Wide_Wide_Fixed.Trim (To_Basic (Word), Ada.Strings.Both);
begin
   pragma Assert (To_Upper ('ÿ') = Wide_Wide_Character'Val (16#178#));
   Ada.Wide_Wide_Text_IO.Put_Line (Plain);
   Ada.Wide_Wide_Text_IO.Put_Line (To_Upper (Plain));
   Ada.Wide_Wide_Text_IO.Put_Line (Boolean'Wide_Wide_Image (Is_Letter ('σ') and then Is_Digit ('٣')));
end Main;

It writes Angstrom σοφια, ANGSTROM ΣΟΦΙΑ and TRUE. The assertion is proved: 'ÿ' is Latin-1, whose mappings the prover sees.

Ada.Wide_Characters.Handling

Ada.Wide_Characters.Handling (A.3.5) is Ada.Wide_Wide_Characters.Handling for Wide_Character, the Basic Multilingual Plane: the same classifications by Unicode's general categories and the same simple case mappings and basic letters, each an expression function. A mapping that would leave the plane (none does in Unicode 16.0.0's simple mappings, but the tables are the run time's) leaves the character as it is. Its parent Ada.Wide_Characters declares nothing.

Ada.Strings.Maps

Character sets and mappings are value types implemented as tables over Character, with every operation of RM A.4.2. Null_Set contains nothing. Character_Range has Low/High fields, and Character_Ranges is an array of them; To_Set accepts a range, ranges, a singleton or a Character_Sequence (a String) and states membership equivalence. A reversed range is empty. Is_In tests membership. not complements across all Latin-1 values; and, or, xor and - are intersection, union, symmetric difference and difference. Is_Subset (and <=, its other name) says every member of Elements belongs to Set. To_Ranges gives a set as the fewest ranges in ascending order, none adjacent to the next, and To_Sequence its characters in ascending order.

Character_Mapping maps one Character to another. Identity maps every character to itself, which the ghost procedure Lemma_Identity states for a proof that needs it. To_Mapping requires From and To to have the same length, and From to name each character once (where the RM raises Translation_Error); its result maps each character of From to the character of To at the same offset and every other character to itself, as its postcondition says. To_Domain gives the characters a mapping moves, in ascending order, and To_Range where it takes them, in the same order.

Sets and mappings are 256-entry tables and ordinary copyable values.

Ada.Strings.Maps.Constants

Maps.Constants holds Handling's classes as sets (Control_Set, Graphic_Set, Letter_Set, Lower_Set, Upper_Set, Basic_Set, Decimal_Digit_Set, Hexadecimal_Digit_Set, Alphanumeric_Set, Special_Set and ISO_646_Set) and its conversions as mappings (Lower_Case_Map, Upper_Case_Map and Basic_Map). Each is a constant table built from the Handling function of the same name, for Is_In, Value and the set operations of Ada.Strings.Maps. As with Maps' own sets, a proof does not see which characters a set holds.