DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Package Verada.Vibe.Formatting

Verada.Vibe.Formatting: Python's format(x, spec) (f-strings' {x:spec}) of an int, a float and a str, repr of a str (!r), and int(s) of a str. The compiler reads a format spec, Python's mini-language [[fill]align][sign][z][#][0][width][grouping][.[precision][grouping]][type], when it compiles the program and passes it as a Spec, so nothing here parses one; Valid_For_* say which specs Python accepts for each type (it raises ValueError for the others). The results are CPython's (3.14's, which also groups a float's fraction: .6_f), character for character; a float's digits are correctly rounded as CPython's are (Verada.Vibe.Decimal_Digits). Only memory limits a width, a precision or a result: a result that memory cannot hold raises Storage_Error (Verada.Storage).

Alignment

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

none, <, >, ^, =

Sign_Option

type Sign_Option is (Minus, Plus, Space);

none or -, +,

Grouping_Option

type Grouping_Option is (None, Comma, Underscore);

none, ,, _

Spec

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;

a format spec. Zero_Pad is the 0 before the width when no fill is given (after an explicit fill, Python reads a 0 as the width's first digit, so the compiler does too): it makes the fill '0' (Fill is then not used) and, for numbers, the alignment = when none is given, as Python's does. Fraction_Grouping is the grouping after the precision (.6_f, or ._f with none), which only a float's types use. Kind is the type letter, ' ' for none.

Well_Formed

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

a spec the compiler makes: a precision of -1 for none, and a fill a text may hold

Is_Float_Kind

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

the float presentation types

Valid_For_Integer

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));

the specs Python accepts for an int: its types ('n' as 'd', the C locale's), and the float types, which format the int converted to a float; no precision for an int's own types, nor z; no grouping with 'n', no , with 'b', 'o', 'x' and 'X'; with 'c' no sign, # or grouping (Python takes and ignores a fraction's grouping, but with 'n')

Valid_For_Float

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));

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

Valid_For_Text

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);

the specs Python accepts for a str: no type or 's'; no =, sign, z, # or grouping (a fraction's grouping it takes and ignores)

Magnitude_Image

function Magnitude_Image (M : Long_Long_Long_Integer) return Wide_Wide_String is
  (if M < 10 then (1 => Wide_Wide_Character'Val (48 + M))
   else Magnitude_Image (M / 10) & Wide_Wide_Character'Val (48 + M mod 10))
  with Pre => M in 0 .. 2 ** 63,
       Post => 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'),
       Subprogram_Variant => (Decreases => M);

the decimal digits of M, the most significant first

Decimal_Image

function Decimal_Image (Value : Integer) return Wide_Wide_String is
  (if Value < 0 then Wide_Wide_Character'Val (45) & Magnitude_Image (-Long_Long_Long_Integer (Value))
   else Magnitude_Image (Long_Long_Long_Integer (Value)))
  with Post => Decimal_Image'Result'First = 1 and then Decimal_Image'Result'Length in 1 .. 20;

Python's str(n) of an int: its decimal digits, after '-' when it is negative

Plain

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');

a spec that formats an int as str does: no width, sign, grouping or type but 'd'

Format

function Format (Value : Integer; Item : Spec) return Text
  with Global => null,
       Pre => Valid_For_Integer (Item) and then (if Item.Kind = 'c' then Value in 0 .. 16#10FFFF#),
       Post => Length (Format'Result) >= Item.Width
               and then (if Plain (Item) then To_Wide_Wide_String (Format'Result) = Decimal_Image (Value));

Python's format(n, spec) of an int

Format

function Format (Value : Long_Float; Item : Spec) return Text
  with Global => null,
       Pre => Valid_For_Float (Item),
       Post => Length (Format'Result) >= Item.Width;

Python's format(x, spec) of a float: with no type, str(x) (repr's digits) when no precision is given, else 'g''s, but with ".0" after a whole number and an exponent from 10 ** (precision - 1) on; precision 6 when none is given for the other types; inf, nan and -0.0 as Python writes them, z writing a value that rounds to zero without its sign

Fill_Of

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

the fill a spec pads with

Kept

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));

how many of Source's characters a str's format keeps: the precision's, when shorter

Padding

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

the fill a str's format adds to reach the width

Left_Padding

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);

how much of it comes first (a str is aligned left by default; ^ puts the extra one on the right)

Format

function Format (Value : Text; Item : Spec) return Text
  with Global => null,
       Pre => Valid_For_Text (Item),
       Post => 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))));

Python's format(s, spec) of a str: its first Kept characters, padded to the width

Is_Printable

function Is_Printable (C : Wide_Wide_Character) return Boolean
  with Global => null,
       Post => (if Wide_Wide_Character'Pos (C) <= 255
                then Is_Printable'Result = (Wide_Wide_Character'Pos (C) in 32 .. 126 | 161 .. 172 | 174 .. 255));

Python's str.isprintable of one character: not a control, format, surrogate, private-use, unassigned, line or paragraph separator, nor a space but ' ' (Unicode 16's categories, CPython 3.14's)

Has

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

whether S holds C

Quote_For

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

the quote repr writes a str between: ' unless the str has a ' and no "

Escape_Length

function Escape_Length (C, Q : Wide_Wide_Character) return Positive is
  (if C = '\' or else C = Q or else Wide_Wide_Character'Pos (C) in 9 | 10 | 13 then 2
   elsif Is_Printable (C) then 1
   elsif Wide_Wide_Character'Pos (C) <= 255 then 4
   elsif Wide_Wide_Character'Pos (C) <= 16#FFFF# then 6
   else 10)
  with Post => Escape_Length'Result <= 10;

how many characters repr writes for C between the quotes Q: two for \, \Q, \t, \n and \r, one for another printable character, \xhh for the other Latin-1 ones, \uhhhh and \Uhhhhhhhh beyond

Escaped_Length

function Escaped_Length (S : Wide_Wide_String; Q : Wide_Wide_Character; Last : Natural) return Long_Long_Long_Integer is
  (if Last = 0 then 0 else Escaped_Length (S, Q, Last - 1) + Long_Long_Long_Integer (Escape_Length (S (Last), Q)))
  with Ghost,
       Pre => (S'Length = 0 or else S'First = 1) and then Last <= S'Length,
       Post => Escaped_Length'Result >= Long_Long_Long_Integer (Last) and then Escaped_Length'Result <= 10 * Long_Long_Long_Integer (Last),
       Subprogram_Variant => (Decreases => Last);

how many characters repr writes for S's first Last between the quotes Q

Repr

function Repr (Value : Text) return Text
  with Global => null,
       Post => 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))
               --  (a str that needs no escapes, character by character)
               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)));

Python's repr(s) of a str (f"{s!r}"): between quotes, each character as it is or escaped (lower-case hexadecimal digits)

Is_Space

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#);

a space to int(): str.isspace's characters but 28 .. 31 (which CPython's int() does not strip)

Is_Digit

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

Leading_Spaces

function Leading_Spaces (S : Wide_Wide_String) return Natural
  with Global => null,
       Pre => S'Length = 0 or else S'First = 1,
       Post => 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)));

how many of S's characters are spaces before the first that is not

Trailing_Spaces

function Trailing_Spaces (S : Wide_Wide_String) return Natural
  with Global => null,
       Pre => S'Length = 0 or else S'First = 1,
       Post => 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)));

how many are spaces after the last that is not (S'Length when all are)

Number_Last

function Number_Last (S : Wide_Wide_String) return Natural is
  (S'Length - Trailing_Spaces (S))
  with Pre => S'Length = 0 or else S'First = 1;

the last character of the number: the last that is not a space

Signed

function Signed (S : Wide_Wide_String) return Boolean is
  (Leading_Spaces (S) < Number_Last (S) and then S (Leading_Spaces (S) + 1) in '+' | '-')
  with Pre => S'Length = 0 or else S'First = 1;

whether the number has a sign: its first character, after the spaces

Digits_First

function Digits_First (S : Wide_Wide_String) return Natural is
  (Leading_Spaces (S) + (if Signed (S) then 1 else 0))
  with Pre => S'Length = 0 or else S'First = 1;

the characters before the digits: the spaces, and the sign

Is_Digit_Run

function Is_Digit_Run (S : Wide_Wide_String; After, Last : Natural) return Boolean is
  (After < Last and then Is_Digit (S (After + 1)) and then Is_Digit (S (Last))
   and then (for all I in After + 1 .. Last =>
               Is_Digit (S (I))
               or else (S (I) = '_' and then I > After + 1 and then I < Last
                        and then Is_Digit (S (I - 1)) and then Is_Digit (S (I + 1)))))
  with Pre => (S'Length = 0 or else S'First = 1) and then Last <= S'Length;

S (After + 1 .. Last) are decimal digits with single underscores between them

Digit_Value

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);

Run_Value

function Run_Value (S : Wide_Wide_String; After, Last : Natural) return Long_Long_Long_Integer is
  (if Last <= After then 0
   elsif S (Last) = '_' or else Run_Value (S, After, Last - 1) > 2 ** 63 then Run_Value (S, After, Last - 1)
   else Run_Value (S, After, Last - 1) * 10 + Digit_Value (S (Last)))
  with Ghost,
       Pre => (S'Length = 0 or else S'First = 1) and then Last <= S'Length,
       Post => Run_Value'Result in 0 .. 2 ** 67,
       Subprogram_Variant => (Decreases => Last);

the value of S (After + 1 .. Last)'s digits; once past 2 ** 63 (out of Integer's range) it counts no further

Integer_Form

function Integer_Form (S : Wide_Wide_String) return Boolean is
  (Is_Digit_Run (S, Digits_First (S), Number_Last (S))
   and then Run_Value (S, Digits_First (S), Number_Last (S))
            <= (if Signed (S) and then S (Leading_Spaces (S) + 1) = '-' then 2 ** 63 else 2 ** 63 - 1))
  with Ghost,
       Pre => S'Length = 0 or else S'First = 1;

the str int() reads: spaces, a sign, a digit run, spaces, and a value in range

Integer_Value

function Integer_Value (S : Wide_Wide_String) return Long_Long_Long_Integer is
  (if Signed (S) and then S (Leading_Spaces (S) + 1) = '-'
   then -Run_Value (S, Digits_First (S), Number_Last (S))
   else Run_Value (S, Digits_First (S), Number_Last (S)))
  with Ghost,
       Pre => S'Length = 0 or else S'First = 1;

the value it reads

Is_Integer_Text

function Is_Integer_Text (Source : Text) return Boolean
  with Global => null,
       Post => Is_Integer_Text'Result = Integer_Form (To_Wide_Wide_String (Source));

whether Python's int(s) reads Source (else it raises ValueError)

To_Integer

function To_Integer (Source : Text) return Integer
  with Global => null,
       Pre => Is_Integer_Text (Source),
       Post => Long_Long_Long_Integer (To_Integer'Result) = Integer_Value (To_Wide_Wide_String (Source));

Python's int(s)