[go: up one dir, main page]

Module format

Module format 

Source
Expand description

printf-style formatting

Rust has excellent formatting capabilities, but the coreutils require very specific formatting that needs to work exactly like the GNU utilities. Naturally, the GNU behavior is based on the C printf functionality.

Additionally, we need support for escape sequences for the printf utility.

The printf and sprintf functions closely match the behavior of the corresponding C functions: the former renders a formatted string to stdout, the latter renders to a new String object.

There are three kinds of parsing that we might want to do:

  1. Parse only printf directives (for e.g. seq, dd)
  2. Parse only escape sequences (for e.g. echo)
  3. Parse both printf specifiers and escape sequences (for e.g. printf)

This module aims to combine all three use cases. An iterator parsing each of these cases is provided by parse_spec_only, parse_escape_only and parse_spec_and_escape, respectively.

There is a special Format type, which can be used to parse a format string containing exactly one directive and does not use any * in that directive. This format can be printed in a type-safe manner without failing (modulo IO errors).

Modules§

human
human-size formatting
num_format
Utilities for formatting numbers in various formats

Structs§

Format
A format for a single numerical value of type T
FormatArguments
A struct that holds a slice of format arguments and provides methods to access them

Enums§

EscapedChar
FormatArgument
An argument for formatting
FormatError
FormatItem
A single item to format
OctalParsing
Spec
A parsed specification for formatting a value

Traits§

FormatChar

Functions§

parse_escape_only
Parse a format string containing escape sequences
parse_spec_and_escape
Parse a format string containing % directives and escape sequences
parse_spec_only
Parse a format string containing % directives
printf
Write a formatted string to stdout.
sprintf
Create a new formatted string.