CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
Formatting

Modules

 Formattable interface
 

Macros

#define strFormat(out, fmt, ...)    _strFormat(out, fmt, count_macro_args(__VA_ARGS__), (stvar[]) { __VA_ARGS__ })
 

Detailed Description

Type-safe string formatter with variable substitution and rich formatting options.

Though the function prototype itself is extremely simple, the usage is not. On a basic level, the formatter works similarly to printf, but is type safe and has many convenience features.

Format syntax

The format string is copied as-is unless the character sequence ${ is encountered. This sequence triggers a variable replacement, and everything until the next } is considered to be part of the variable. If a literal ${ is needed, it can be escaped with a backtick – i.e. `${

Variable substitution follows the following format:

type#(width,fmtopts)extra;default

type is a type name (see below), and the number indicates the n-th instance of that particular type in the format arguments. The type is REQUIRED and the number is OPTIONAL. If the number is omitted, an internal count is maintained, and each time that type name is used, the count is increased.

For example, ${string} indicates the next string in the argument sequence, and ${int3} uses the third integer.

Optional prefixes

An optional prefix may precede the type name. Supported prefixes are:

Format options

fmtopts is a comma-delimited list of formatting options, most of which are specific to particular types – see below for details. A formatting option that is just a number by itself is interpreted to be the field width.

Most types right-justify within the field width if it is present – this can be overridden with the formatting options of left, or center.

Supported types

Extra field modifiers

The 'extra' field may follow one of two forms and acts as a modifier on the type:

Default values

If there is an error such as not being able to find an argument of the appropriate type, the normal behavior is for strFormat to return false and produce no output. However, if the variable contains a ;default section, instead of failing the entire format operation, the literal text of 'default' (anything after the semicolon) will be inserted in place of the variable instead. A } character may be escaped with `} in this section.

Macro Definition Documentation

◆ strFormat

#define strFormat (   out,
  fmt,
  ... 
)     _strFormat(out, fmt, count_macro_args(__VA_ARGS__), (stvar[]) { __VA_ARGS__ })

bool strFormat(string *out, strref fmt, ...)

Type-safe string formatter with variable substitution and rich formatting options

Copies the format string as-is, replacing ${...} sequences with formatted variables. See detailed documentation sections above for complete syntax and formatting options.

Parameters
outPointer to string to receive the formatted result
fmtFormat string with ${type} variable sequences
...Variable arguments, each wrapped with stvar(type, value)
Returns
true on success, false if a required variable is missing or type mismatch occurs

Example:

string s = 0;
strFormat(&s, _S"Hello ${string}, you have ${int} messages",
stvar(string, name), stvar(int32, count));
#define strFormat(out, fmt,...)
Definition format.h:130
#define _S
Creates a static ASCII string from a string literal.
Definition strbase.h:392
#define stvar(typen, val)
Definition stvar.h:153

Definition at line 130 of file format.h.