4 ms·
I found that instead of adding format specifiers, one can create a construct that uses occurrences of '\0' in strings to insert values: FMT_BEGIN("a + b = \
by camel-cdr 4y ago
I found that instead of adding format specifiers, one can create a construct that uses occurrences of '\0' in strings to insert values:
FMT_BEGIN("a + b = \0 + \0 = \0\n") {
FMT(float, (a));
FMT(float, (b));
FMT(float, (a + b));
} FMT_END;
FMT_BEGIN("x = \0 = 0x\0 = 0b\0\na = \0\nb = \0\n") {
FMT(uint, (x));
FMT(uint_base, ((unsigned)x, 16, "0123456789abcdef"));
FMT(uint_base, ((unsigned)x, 2, "01"));
FMT(Vec3, (a));
FMT(Vec3, (b));
} FMT_END;
The major advantage here is that you don't need to parse a dsl, and you can still have formatting options. A final api would probably use automatic compound literal struct arguments to implement default arguments with names parameters.
Type detection should also be possible, although you would need to register all print functions in a global macro.
This is just a proof of concept: https://github.com/camel-cdr/cauldron/blob/main/ideas/fmt.c https://github.com/camel-cdr/cauldron/blob/main/ideas/fmt.c