Documentation
¶
Index ¶
- func ApplyGenerics(body string, s FormatSpec, defaultAlign Align) string
- func GroupDigits(digits string, sep byte, groupSize int) string
- func RepeatRune(r rune, n int) string
- func SignAwareSplit(body string) int
- func SignPrefix(sign Sign, negative bool) string
- type Align
- type FormatSpec
- type Sign
- type Template
- type TemplateMode
- type TemplateSegment
- type TemplateSegmentKind
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyGenerics ¶
func ApplyGenerics(body string, s FormatSpec, defaultAlign Align) string
ApplyGenerics pads and aligns an already-rendered body to satisfy the purely generic Width / Fill / Align fields of spec. It is the second stage of the format pipeline: a type's Format method first renders the value (handling Sign, Grouping, Precision, CoerceZero, Bare, Verb, Tail and any prefix such as "0x") and then calls ApplyGenerics on the result to obtain the final string.
defaultAlign supplies the type-default alignment when spec.Align is unset (per spec: AlignLeft for non-numeric types, AlignRight for numeric types).
Width is measured in runes; if the body already meets or exceeds spec.Width, the body is returned unchanged. The fill character defaults to ' ' (or '0' when the parser set the ZeroPad shortcut, which already lands as Fill='0' + Align='=').
For Align == AlignSign the helper inserts fill *between* any leading sign character ('+', '-', ' ') and any conventional integer prefix ("0b", "0o", "0x", "0X") and the rest of the body, e.g. "+0000123" or "0x0000002A". On bodies that have neither, AlignSign degenerates to right-alignment.
func GroupDigits ¶
GroupDigits inserts sep every groupSize digits (counted from the right) into the digit string. groupSize must be > 0; digits must consist only of digit/letter characters (no leading sign or prefix). Returns digits unchanged when sep == 0 or groupSize <= 0.
func RepeatRune ¶
RepeatRune returns a new string consisting of n copies of r.
func SignAwareSplit ¶
SignAwareSplit returns the byte index at which fill should be inserted for AlignSign: just after an optional leading sign character and an optional conventional integer prefix ("0b", "0o", "0x", "0X").
func SignPrefix ¶
SignPrefix returns the sign character that should precede a non-negative numeric body, given the requested Sign mode. For negative values, callers should emit the leading '-' from the value itself and pass an empty prefix (this helper is a no-op via the SignMinus / SignDefault branches when negative=true).
Types ¶
type FormatSpec ¶
type FormatSpec struct {
// generic
Fill rune // 0 = unset
Align Align // 0 = type default
Width int16
HasWidth bool
ZeroPad bool // leading-0 shortcut
// numeric / shared
Sign Sign
Grouping byte // 0, ',' or '_'
Precision int16
HasPrec bool
CoerceZero bool // '~' — for float / decimal: coerce -0 to +0 after rounding
Bare bool // '!' — suppress conventional prefix ("0b", "0o", "0x", "0X") on integer prefix-emitting verbs
// discriminator
Verb byte // 0 = default; one ASCII letter; or '#' when a tail is present
Tail string // anything after '#'; "" if absent
}
FormatSpec is the fully parsed format spec.
func Parse ¶
func Parse(text string) (FormatSpec, error)
Parse parses the format mini-language expression (see docs/format-mini-language.md).
func (FormatSpec) Equal ¶ added in v0.4.1
func (s FormatSpec) Equal(other FormatSpec) bool
func (FormatSpec) HasUnconsumedTail ¶ added in v0.1.4
func (s FormatSpec) HasUnconsumedTail() bool
HasUnconsumedTail reports whether the spec carries a tail that was not consumed via Verb='#'. Type Format methods that don't accept a tail alongside a generic verb should reject when this is true. Verbs 'v' and 'T' are universal "ignore everything else" verbs and therefore exempted.
type Template ¶ added in v0.1.4
type Template struct {
Mode TemplateMode
Segments []TemplateSegment
}
Template is a parsed runtime format template ready to be rendered against a runtime args container (array for indexed mode, dict/record for named mode).
func ParseTemplate ¶ added in v0.1.4
ParseTemplate parses a runtime format template and returns the segment list.
Grammar (informal):
template := { text_char | '{{' | '}}' | placeholder }
placeholder := '{' name_or_index [ ':' spec_body ] '}'
spec_body := { spec_char | '{' name_or_index '}' }
name_or_index := identifier | non-negative-integer
A bare '}' is an error. Empty '{}' is an error. Mixing named and indexed placeholders in the same template is an error. Inside the spec body the only `{...}` form allowed is a single name_or_index (no nesting, no expressions). A '#'-tail inside the literal portion of a spec body works as in fspec.Parse — no separate handling is needed here because the literal spec text is forwarded to fspec.Parse verbatim.
type TemplateMode ¶ added in v0.1.4
type TemplateMode byte
TemplateMode indicates whether a template's placeholders use named or indexed look-ups. The mode is locked by the first non-empty placeholder and every subsequent placeholder must agree.
const ( TemplateModeUnset TemplateMode = 0 TemplateModeIndexed TemplateMode = 1 // {0}, {1}, ... TemplateModeNamed TemplateMode = 2 // {name}, {x}, ... )
type TemplateSegment ¶ added in v0.1.4
type TemplateSegment struct {
Kind TemplateSegmentKind
// Literal segment payload.
Literal string
// Placeholder fields.
Name string // for named mode
Index int // for indexed mode
HasSpec bool
// When HasSpec && !SpecIsRef the literal spec text was parsed at template parse time and stored in Spec.
Spec FormatSpec
// When HasSpec && SpecIsRef the spec is provided at runtime via a single {ref} placeholder inside the spec body.
SpecIsRef bool
SpecRefName string
SpecRefIndex int
}
TemplateSegment is one piece of a parsed template: either a literal run of text or a placeholder that references a value (and optionally a format spec) from the args supplied at runtime.
type TemplateSegmentKind ¶ added in v0.1.4
type TemplateSegmentKind byte
TemplateSegmentKind discriminates literal segments from interpolation placeholders inside a parsed template.
const ( TemplateLiteral TemplateSegmentKind = 0 TemplatePlaceholder TemplateSegmentKind = 1 )