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.

Index

Types

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

// Undocumented types:
type instruction = (literal | variable);
type literal = str;
type template = []instruction;
type variable = str;

Errors

type invalid = !void;

Functions

fn compile(input: str) (template | invalid);
fn execute(tmpl: *template, out: io::handle, params: param...) (size | io::error);
fn finish(tmpl: *template) void;
fn strerror(err: invalid) str;

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;

Errors

type invalid[link]

type invalid = !void;

The template string has an invalid format.

Functions

fn compile[link]

fn compile(input: str) (template | 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.

fn strerror[link]

fn strerror(err: invalid) str;

Converts an error into a human-friendly string.