strings::template +linux +x86_64

This module provides support for formatting of large or complex strings beyond the scope of fmt. A template is compiled using compile, then executed with execute to print formatted text to an io::handle.

The template format is a string with variable substituted using "$". Variable names must be alphanumeric ASCII characters (i.e. for which ascii::isalnum returns true). A literal "$" may be printed by using it twice: "$$".

const src = "Hello, $user! Your balance is $$$balance.\n";
const template = template::compile(src)!;
defer template::finish(&template);
template::execute(&template, os::stdout,
	("user", "ddevault"),
	("balance", 1000),
)!; // "Hello, ddevault! Your balance is $1000.

Index

Types

type param;

// Undocumented types:
type instruction;
type literal;
type template;
type variable;

Functions

fn compile(str) (template | errors::invalid);
fn execute(*template, io::handle, param...) (size | io::error);
fn finish(*template) void;

Types

type param[link]

type param = (str, fmt::formattable);

Parameters to execute with a template, a tuple of a variable name and a formattable object.

type instruction[link]

Show undocumented member
type instruction = (literal | variable);

type literal[link]

Show undocumented member
type literal = str;

type template[link]

Show undocumented member
type template = []instruction;

type variable[link]

Show undocumented member
type variable = str;

Functions

fn compile[link]

fn compile(input: str) (template | errors::invalid);

Compiles a template string. The return value must be freed with finish after use.

fn execute[link]

fn execute(tmpl: *template, out: io::handle, params: param...) (size | io::error);

Executes a template, writing the output to the given io::handle. If the template calls for a parameter which is not provided, an assertion will be fired.

fn finish[link]

fn finish(tmpl: *template) void;

Frees resources associated with a template.