strings::template+x86_64 +linux

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 variables 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: "$$". Variables may also be used with braces, i.e. ${variable}, so that they can be placed immediately next to alphanumeric characters; such variables may include non-alphanumeric characters other than '{' and '}'.

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.



type param;

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


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


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;


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.