! Warning! Library is in developing and not ready to be used in production code !
- Presentations:
b
,B
,c
,d
,o
,x
,X
,f
,F
,s
,p
- Flags:
-
,+
,0
,#
,<
,^
,>
- Argument position (
{0}
,{1}
,{2:+}
etc) - Field width
float
anddouble
types are supported (-inf
,+inf
andnan
also works)- Wrong type error detection
#
not supported for float typesL
option (locale-specific formatting) not supported- Only
f
presentation for float type is supported. - Format strings like
{{}.{}}
not supported
Read it first. How to write "replacement fields" for output strings: https://fmt.dev/latest/syntax.html#format-specification-mini-language
char my_buffer[64];
mf::format(my_buffer, "{} {} {}", "Printing", "to", "buffer");
1
static bool uart_format_callback(void* data, char character)
{
uart_send_char(character);
return true;
}
mf::format(uart_format_callback, nullptr, "{:.2} {} {:10}", 1.2f, 2, 42U);
2
template <typename ... Args>
size_t print_to_uart(const char* format, const Args& ... args)
{
auto uart_format_callback = [](auto, char character)
{
uart_send_char(character);
return true;
};
return mf::format(uart_format_callback, nullptr, format, args...);
}
...
print_to_uart("Hello world!!!\n");
print_to_uart("Hello {}\n", "world!!!");
print_to_uart("{} {}\n", "Hello", "world!!!");
print_to_uart("{1} {0}\n", "world!!!", "Hello");
print_to_uart("U={:8.2}v, I={:8.2}A\n", 11.2f, 0.1f);
More examples or replacement fields are in test sources: micro_format_tests.cpp
Library contains simple functions for non-format style convertion values to string:
format_dec
- to print integer as decimal numberformat_hex
- to print integer as hexadecimal numberformat_bin
- to print integer as binary numberformat_float
- to print floating point number
Both "print to buffer" and callback versions are presented
mf::format_dec(my_buffer, 42);
mf::format_dec(uart_format_callback, nullptr, 42);
mf::format_float(my_buffer, 1234.5678, 4);
Library doesn't compile with float
and double
types support by default to reduce binary size of firmware. To use float
type you have do define MICRO_FORMAT_FLOAT
macro in you project. To use both float
and double
define MICRO_FORMAT_DOUBLE
- Binary size of compiled library without
float
anddouble
support takes less than 2Kb for my cortex-m0 micrcocontroller - Each new combination of arguments types for
mf::format
takes about 80 bytes - Call
mf::format
for existing combination of types of arguments takes about 40 bytes