DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Verada.Vibe.Formatting

Python's format specs, repr of a str and int() of a str, for Vibe.

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

Format writes an Integer, a Long_Float or a Text by a Spec, the fields of a format spec, which the compiler reads from an f-string's or format's literal spec; nothing here parses a spec. Valid_For_Integer, Valid_For_Float and Valid_For_Text are the preconditions: the specs Python accepts for each type (it raises ValueError for the others). The results are CPython 3.14's character for character, and a float's digits are correctly rounded (Verada.Vibe.Decimal_Digits). Repr is Python's repr of a str; Is_Integer_Text and To_Integer are Python's int() of a str with a value in Integer's range. Each result is a new Text.

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.Vibe.Formatting;
import Verada.Vibe.Formatting;

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

Alignment (declaration) — A spec's alignment: none (Default), < (Left), > (Right), ^ (Center) or = (After_Sign, padding between a number's sign and its digits).

Declared interface:

type Alignment is (Default, Left, Right, Center, After_Sign);

API 002

Sign_Option (declaration) — A spec's sign option: none or - (Minus, a sign for negative numbers only), + (Plus) or a space (Space).

Declared interface:

type Sign_Option is (Minus, Plus, Space);

API 003

Grouping_Option (declaration) — A spec's digit grouping: none, , (Comma) or _ (Underscore).

Declared interface:

type Grouping_Option is (None, Comma, Underscore);

API 004

Spec (declaration) — A format spec's fields, each defaulting to its absence; the compiler fills one in from a literal spec. Zero_Pad is the 0 before the width when no fill is given: it makes the fill '0' (Fill is then not used) and, for a number with no alignment, the alignment =. Negative_Zero is z, Alternate is #, Precision is -1 when none is given, Fraction_Grouping is the grouping after the precision (which only a float's types use), and Kind is the type letter, ' ' for none.

Declared interface:

type Spec is record
   Fill : Wide_Wide_Character := ' ';
   Align : Alignment := Default;
   Sign : Sign_Option := Minus;
   Negative_Zero : Boolean := False;
   Alternate : Boolean := False;
   Zero_Pad : Boolean := False;
   Width : Natural := 0;
   Grouping : Grouping_Option := None;
   Precision : Integer := -1;
   Fraction_Grouping : Grouping_Option := None;
   Kind : Character := ' ';
end record;

Public nested members: Fill, Align, Sign, Negative_Zero, Alternate, Zero_Pad, Width, Grouping, Precision, Fraction_Grouping, Kind.

API 005

Well_Formed (declaration) — Whether a spec is one the compiler makes: a precision of -1 for none, and a fill that is a code point; each Valid_For_* function requires it. Only memory limits a width or a precision.

Declared interface:

function Well_Formed (Item : Spec) return Boolean is
  (Item.Precision >= -1 and then Wide_Wide_Character'Pos (Item.Fill) <= 16#10FFFF#);

API 006

Is_Float_Kind (declaration) — Whether a type letter is a float presentation type: 'e', 'E', 'f', 'F', 'g', 'G' or '%'.

Declared interface:

function Is_Float_Kind (Kind : Character) return Boolean is
  (Kind in 'e' | 'E' | 'f' | 'F' | 'g' | 'G' | '%');

API 007

Valid_For_Integer (declaration) — The specs Python accepts for an int: its own types ('n' as 'd', the C locale's) and the float types, which format the int converted to a float. An int's own types take no precision and no z; 'n' takes no grouping; 'b', 'o', 'x' and 'X' take no ,; and 'c' takes no sign, # or grouping.

Declared interface:

function Valid_For_Integer (Item : Spec) return Boolean is
  (Well_Formed (Item)
   and then (Item.Kind in ' ' | 'd' | 'n' | 'b' | 'o' | 'x' | 'X' | 'c' or else Is_Float_Kind (Item.Kind))
   and then (if not Is_Float_Kind (Item.Kind) then Item.Precision = -1 and then not Item.Negative_Zero)
   and then (if Item.Kind = 'n' then Item.Grouping = None and then Item.Fraction_Grouping = None)
   and then (if Item.Kind in 'b' | 'o' | 'x' | 'X' then Item.Grouping /= Comma)
   and then (if Item.Kind = 'c' then Item.Sign = Minus and then not Item.Alternate and then Item.Grouping = None));

API 008

Valid_For_Float (declaration) — The specs Python accepts for a float: no type, 'n' (as 'g', without grouping) and the float types.

Declared interface:

function Valid_For_Float (Item : Spec) return Boolean is
  (Well_Formed (Item)
   and then (Item.Kind in ' ' | 'n' or else Is_Float_Kind (Item.Kind))
   and then (if Item.Kind = 'n' then Item.Grouping = None and then Item.Fraction_Grouping = None));

API 009

Valid_For_Text (declaration) — The specs Python accepts for a str: no type or 's', and no = alignment, sign, z, # or grouping.

Declared interface:

function Valid_For_Text (Item : Spec) return Boolean is
  (Well_Formed (Item)
   and then Item.Kind in ' ' | 's'
   and then Item.Align /= After_Sign
   and then Item.Sign = Minus
   and then not Item.Negative_Zero
   and then not Item.Alternate
   and then Item.Grouping = None);

API 010

Magnitude_Image (declaration) — The decimal digits of a magnitude M in 0 .. 2 ** 63, most significant first and indexed from 1; the postcondition bounds the length by the magnitude.

Before this operation, establish:

M in 0 .. 2 ** 63

On normal return the declared contract establishes:

Magnitude_Image'Result'First = 1
               and then Magnitude_Image'Result'Length >= 1
               and then (if M < 10 then Magnitude_Image'Result'Length = 1)
               and then (if M < 10 ** 2 then Magnitude_Image'Result'Length <= 2)
               and then (if M < 10 ** 3 then Magnitude_Image'Result'Length <= 3)
               and then (if M < 10 ** 4 then Magnitude_Image'Result'Length <= 4)
               and then (if M < 10 ** 5 then Magnitude_Image'Result'Length <= 5)
               and then (if M < 10 ** 6 then Magnitude_Image'Result'Length <= 6)
               and then (if M < 10 ** 7 then Magnitude_Image'Result'Length <= 7)
               and then (if M < 10 ** 8 then Magnitude_Image'Result'Length <= 8)
               and then (if M < 10 ** 9 then Magnitude_Image'Result'Length <= 9)
               and then (if M < 10 ** 10 then Magnitude_Image'Result'Length <= 10)
               and then (if M < 10 ** 11 then Magnitude_Image'Result'Length <= 11)
               and then (if M < 10 ** 12 then Magnitude_Image'Result'Length <= 12)
               and then (if M < 10 ** 13 then Magnitude_Image'Result'Length <= 13)
               and then (if M < 10 ** 14 then Magnitude_Image'Result'Length <= 14)
               and then (if M < 10 ** 15 then Magnitude_Image'Result'Length <= 15)
               and then (if M < 10 ** 16 then Magnitude_Image'Result'Length <= 16)
               and then (if M < 10 ** 17 then Magnitude_Image'Result'Length <= 17)
               and then (if M < 10 ** 18 then Magnitude_Image'Result'Length <= 18)
               and then Magnitude_Image'Result'Length <= 19
               and then (for all I in Magnitude_Image'Result'Range => Magnitude_Image'Result (I) in '0' .. '9')

API 011

Decimal_Image (declaration) — Python's str(n) of an int: its decimal digits, after '-' when it is negative; at most 20 characters.

On normal return the declared contract establishes:

Decimal_Image'Result'First = 1 and then Decimal_Image'Result'Length in 1 .. 20

API 012

Plain (declaration) — Whether a spec formats an int as str does: no width, sign option or grouping, and no type but 'd' or 'n'.

Declared interface:

function Plain (Item : Spec) return Boolean is
  (Item.Width = 0 and then Item.Sign = Minus and then Item.Grouping = None and then Item.Kind in ' ' | 'd' | 'n');

API 013

Format (declaration) — Python's format(n, spec) of an int. With 'c' the value must be a code point. The result is at least the width, and under a Plain spec it is Decimal_Image.

Before this operation, establish:

Valid_For_Integer (Item) and then (if Item.Kind = 'c' then Value in 0 .. 16#10FFFF#)

On normal return the declared contract establishes:

Length (Format'Result) >= Item.Width
               and then (if Plain (Item) then To_Wide_Wide_String (Format'Result) = Decimal_Image (Value))

API 014

Format (declaration) — Python's format(x, spec) of a float. With no type it is str(x) (repr's digits) when no precision is given, else 'g' with ".0" after a whole number and an exponent from 10 ** (precision - 1) on; the other types take precision 6 when none is given. inf, nan and -0.0 are written as Python writes them, and z drops the sign of a value that rounds to zero. The contract bounds the length only.

Before this operation, establish:

Valid_For_Float (Item)

On normal return the declared contract establishes:

Length (Format'Result) >= Item.Width

API 015

Fill_Of (declaration) — The character a spec pads with: '0' under Zero_Pad, else Fill.

Declared interface:

function Fill_Of (Item : Spec) return Wide_Wide_Character is
  (if Item.Zero_Pad then '0' else Item.Fill);

API 016

Kept (declaration) — How many of a str's characters its format keeps: the precision, when that is shorter than the text.

Declared interface:

function Kept (Source : Text; Item : Spec) return Natural is
  (if Item.Precision >= 0 and then Item.Precision < Length (Source) then Item.Precision else Length (Source));

API 017

Padding (declaration) — How many fill characters a str's format adds to reach the width.

Declared interface:

function Padding (Source : Text; Item : Spec) return Natural is
  (if Item.Width > Kept (Source, Item) then Item.Width - Kept (Source, Item) else 0);

API 018

Left_Padding (declaration) — How much of that padding comes first: all of it for >, half of it rounded down for ^ (which puts the extra one on the right), and none otherwise, since a str is aligned left by default.

Declared interface:

function Left_Padding (Source : Text; Item : Spec) return Natural is
  (case Item.Align is
      when Right => Padding (Source, Item),
      when Center => Padding (Source, Item) / 2,
      when others => 0);

API 019

Format (declaration) — Python's format(s, spec) of a str: its first Kept characters, padded with Fill_Of to the width. The postcondition states every character of the result.

Before this operation, establish:

Valid_For_Text (Item)

On normal return the declared contract establishes:

Length (Format'Result) = Kept (Value, Item) + Padding (Value, Item)
               and then Length (Format'Result) <= Integer'Max (Length (Value), Item.Width)
               and then (for all I in 1 .. Length (Format'Result) =>
                           To_Wide_Wide_String (Format'Result) (I)
                           = (if I <= Left_Padding (Value, Item) or else I > Left_Padding (Value, Item) + Kept (Value, Item)
                              then Fill_Of (Item)
                              else To_Wide_Wide_String (Value) (I - Left_Padding (Value, Item))))

API 020

Is_Printable (declaration) — Python's str.isprintable of one character, by Unicode 16's categories as in CPython 3.14: not a control, format, surrogate, private-use or unassigned character, not a line or paragraph separator, and not a space other than ' '. The postcondition states the Latin-1 cases.

On normal return the declared contract establishes:

(if Wide_Wide_Character'Pos (C) <= 255
                then Is_Printable'Result = (Wide_Wide_Character'Pos (C) in 32 .. 126 | 161 .. 172 | 174 .. 255))

API 021

Has (declaration) — Whether S holds the character C.

Declared interface:

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

API 022

Quote_For (declaration) — The quote repr writes a str between: " when the str has a ' and no ", else '.

Declared interface:

function Quote_For (S : Wide_Wide_String) return Wide_Wide_Character is
  (if Has (S, ''') and then not Has (S, '"') then '"' else ''');

API 023

Escape_Length (declaration) — How many characters repr writes for C between the quotes Q: two for a backslash, Q, tab, newline and carriage return; one for another printable character; four (\xhh) for the other Latin-1 characters; six (\uhhhh) and ten (\Uhhhhhhhh) beyond.

On normal return the declared contract establishes:

Escape_Length'Result <= 10

API 024

Escaped_Length (declaration) — Ghost: how many characters repr writes between the quotes Q for S's first Last characters; Repr's contract states its length with it.

Before this operation, establish:

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

On normal return the declared contract establishes:

Escaped_Length'Result >= Long_Long_Long_Integer (Last) and then Escaped_Length'Result <= 10 * Long_Long_Long_Integer (Last)

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

API 025

Repr (declaration) — Python's repr(s) of a str (f"{s!r}"): the text between quotes, each character as it is or escaped with lower-case hexadecimal digits. The postcondition gives the length and the quotes, and every character when none needs an escape (a result longer than memory holds is Storage_Error).

On normal return the declared contract establishes:

Long_Long_Long_Integer (Length (Repr'Result))
               = Escaped_Length (To_Wide_Wide_String (Value), Quote_For (To_Wide_Wide_String (Value)), Length (Value)) + 2
               and then To_Wide_Wide_String (Repr'Result) (1) = Quote_For (To_Wide_Wide_String (Value))
               and then To_Wide_Wide_String (Repr'Result) (Length (Repr'Result)) = Quote_For (To_Wide_Wide_String (Value))
               and then (if (for all I in 1 .. Length (Value) =>
                               Escape_Length (To_Wide_Wide_String (Value) (I), Quote_For (To_Wide_Wide_String (Value))) = 1)
                         then Length (Repr'Result) = Length (Value) + 2
                              and then (for all I in 1 .. Length (Value) =>
                                          To_Wide_Wide_String (Repr'Result) (I + 1) = To_Wide_Wide_String (Value) (I)))

API 026

Is_Space (declaration) — The characters int() skips around a number: str.isspace's, except 28 .. 31, which CPython's int() does not strip.

Declared interface:

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

API 027

Is_Digit (declaration) — An ASCII decimal digit, the only digits read here (Python's int() also reads other scripts' digits).

Declared interface:

function Is_Digit (C : Wide_Wide_Character) return Boolean is (C in '0' .. '9');

API 028

Leading_Spaces (declaration) — How many of S's characters are spaces before the first that is not.

Before this operation, establish:

S'Length = 0 or else S'First = 1

On normal return the declared contract establishes:

Leading_Spaces'Result <= S'Length
               and then (for all K in 1 .. Leading_Spaces'Result => Is_Space (S (K)))
               and then (Leading_Spaces'Result = S'Length or else not Is_Space (S (Leading_Spaces'Result + 1)))

API 029

Trailing_Spaces (declaration) — How many of S's characters are spaces after the last that is not (S'Length when all are).

Before this operation, establish:

S'Length = 0 or else S'First = 1

On normal return the declared contract establishes:

Trailing_Spaces'Result <= S'Length
               and then (for all K in 1 .. Trailing_Spaces'Result => Is_Space (S (S'Length - Trailing_Spaces'Result + K)))
               and then (Trailing_Spaces'Result = S'Length or else not Is_Space (S (S'Length - Trailing_Spaces'Result)))

API 030

Number_Last (declaration) — The position of the number's last character: the last that is not a space.

Before this operation, establish:

S'Length = 0 or else S'First = 1

API 031

Signed (declaration) — Whether the number has a sign: '+' or '-' as its first character after the spaces.

Before this operation, establish:

S'Length = 0 or else S'First = 1

API 032

Digits_First (declaration) — How many characters come before the digits: the leading spaces and the sign.

Before this operation, establish:

S'Length = 0 or else S'First = 1

API 033

Is_Digit_Run (declaration) — Whether S (After + 1 .. Last) is decimal digits with single underscores between them, beginning and ending with a digit.

Before this operation, establish:

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

API 034

Digit_Value (declaration) — The value of an ASCII digit, and 0 for any other character.

Declared interface:

function Digit_Value (C : Wide_Wide_Character) return Long_Long_Long_Integer is
  (if Is_Digit (C) then Long_Long_Long_Integer (Wide_Wide_Character'Pos (C) - 48) else 0);

API 035

Run_Value (declaration) — Ghost: the value of the digits of S (After + 1 .. Last), underscores skipped; once past 2 ** 63 (outside Integer's range) it counts no further.

Before this operation, establish:

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

On normal return the declared contract establishes:

Run_Value'Result in 0 .. 2 ** 67

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

API 036

Integer_Form (declaration) — Ghost: whether int() reads S: spaces, an optional sign, a digit run and spaces, with a value in Integer's range.

Before this operation, establish:

S'Length = 0 or else S'First = 1

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

API 037

Integer_Value (declaration) — Ghost: the value int() reads from S, negative after '-'.

Before this operation, establish:

S'Length = 0 or else S'First = 1

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

API 038

Is_Integer_Text (declaration) — Whether Python's int(s) reads Source with a value in Integer's range (Python raises ValueError for a text it does not read); To_Integer's precondition.

On normal return the declared contract establishes:

Is_Integer_Text'Result = Integer_Form (To_Wide_Wide_String (Source))

API 039

To_Integer (declaration) — Python's int(s) of a text Is_Integer_Text accepts; its value is Integer_Value of the model.

Before this operation, establish:

Is_Integer_Text (Source)

On normal return the declared contract establishes:

Long_Long_Long_Integer (To_Integer'Result) = Integer_Value (To_Wide_Wide_String (Source))