format

package
v0.12.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package format ports cpython/Python/formatter_unicode.c. It implements the format-spec mini-language used by str.format, f-strings, and the format() builtin:

[[fill]align][sign][z][#][0][width][grouping][.precision][type]

All four formatters - string, int, float, and (later) complex - share the same parser. The float formatter delegates digit generation to pystrconv.FormatFloat for IEEE-754 round-trip parity with CPython.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidSpec = errors.New("format: invalid format specifier")

ErrInvalidSpec is returned by ParseSpec when the spec violates the grammar.

Functions

func FormatFloat

func FormatFloat(v float64, spec Spec) (string, error)

FormatFloat renders v under spec. Mirrors format_float_internal.

CPython: Python/formatter_unicode.c:L1290 format_float_internal

func FormatInt

func FormatInt(v *big.Int, spec Spec) (string, error)

FormatInt renders v under spec. Mirrors format_long_internal.

CPython: Python/formatter_unicode.c:L959 format_long_internal

func FormatString

func FormatString(s string, spec Spec) (string, error)

FormatString renders s under spec. Mirrors format_string_internal.

CPython: Python/formatter_unicode.c:L848 format_string_internal

Types

type Spec

type Spec struct {
	Fill      rune
	Align     byte // '<', '>', '=', '^', or 0 if default
	Sign      byte // '+', '-', ' ', or 0
	NoNegZero bool
	Alt       bool
	Zero      bool
	Width     int  // -1 if unspecified
	Thousands byte // ',', '_', or 0
	Precision int  // -1 if unspecified
	Type      byte // 0 if unspecified
}

Spec is the parsed form of a format-spec mini-language string.

CPython: Python/formatter_unicode.c:L18 InternalFormatSpec

func ParseSpec

func ParseSpec(s string) (Spec, error)

ParseSpec mirrors parse_internal_render_format_spec.

CPython: Python/formatter_unicode.c:L150 parse_internal_render_format_spec

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL