String format macros for ZZ
Add this to your zz.toml
file:
[dependencies]
format = "*"
[repos]
format = "git://github.com/zzmodules/format"
using format
Creates a formatted string from a variety of special tokens with traditional
printf
style specifiers supported.
{}
- Treats argument like a struct, calling.display()
for achar *
{*}
- Treats argument like a pointer to a struct, calling->display()
for achar *
{0..9}
- References argument by position. Treatment is the same as{}
{*0..9}
- References pointer argument by position. Treatment is same as{*}
{$0..9}
- References argument identifier name by position and uses it inline{0..9%X}
- References argument by position with format. Treatment is like a limitedprintf
The @format::with()
macro will treat struct and struct pointers specified
with {}
, {*}
, and its variant as special input that should implement a
displayable interface. Structs should simply implement a
display() -> char *
method like in the example below:
using format
using log
using string
struct Vector {
float x;
float y;
}
fn display(Vector *self) -> char * {
new+64000 out = string::make();
out.format("Vector { x: %0.2f, y: %0.2f }", self->x, self->y);
return out.cstr();
}
fn main() -> {
let vec = Vector { x: 1.23, y: 2.46 };
log::info("%s", @format::with("{}", vec)); // Vector { x: 1.23, y: 2.46 }
return 0;
}
MIT