module watt.text.format

Structured string formatting.

This module contains the format function, a versatile string formatter. The format function is also used by writef functions and similar to handle their formatting.

The format string syntax is similar to C's printf, but requires less explicit typing. In most cases, %s will suffice to do the 'right thing'.

Examples

format("Hello %s", "your name");  // "Hello your name"
format("%03s", 32);  // "032"

Code Map

//! Structured string formatting.
module watt.text.format;


//! Formats various arguments into a string by interpreting formatString.
fn format(formatString: const(char)[]) string { }
//! Format using the above rules, but into a Sink.
fn format(sink: scope (Sink), formatString: const(char)[]) { }
//! Format using the above rules, but into a Sink, with an explicit list of
//! TypeInfos, and an explicit va_list.
fn formatImpl(sink: scope (Sink), formatString: const(char)[], _typeids: TypeInfo[], vl: va_list) { }
fn format(formatString: const(char)[]) string

Formats various arguments into a string by interpreting formatString.

Format specifiers are specified with the character %.
%(flag)(width)(type)
The flag and width fields are optional.

The flags modify how the printing is formatted.

  • : prefix positive integers with a space.
  • 0: pad the string with zeros, rather than spaces. The width flag pads the output if it is less than the number given. For example,
    "%3s", "ab" = " ab"
    "%03s", "ab" = "0ab"

And the type denotes what type should be printed. They are as follows:

  • %: print a % character.
  • s: perform the default action for the corresponding type.
  • d: print an integer.
  • x: print a lower case hex string.
  • X: print an upper case hex string.
  • b: print a binary string.
  • p: print the value appropriately as a pointer.
  • f: print a floating point value.
  • c: print a single character.
fn format(sink: scope (Sink), formatString: const(char)[])

Format using the above rules, but into a Sink.

fn formatImpl(sink: scope (Sink), formatString: const(char)[], _typeids: TypeInfo[], vl: va_list)

Format using the above rules, but into a Sink, with an explicit list of TypeInfos, and an explicit va_list.

This allows variadic functions to use format to process their input.