Using C++ std::
we can format strings like this:
cout << "The x=" << x << endl;
In C# and Java we can build strings like:
var sb = new StringBuilder(1000);
sb.Append("The x=");
sb.AppendLine(x);
For C (or C++ without std::
) in embedded world (especially smaller 8 bit controllers) we have: Xprintf family functions or string.h functions. My concerns about them:
- Xprintf are bulky functions if one count each cycle (that happens often). Even without floating point
- String.h functions are tedious to use
What I would like to find is some library that can do something like:
char buffer[100];
StringBuilder sb(buffer, 100);
sb.append("The x=");
sb.append(x);
Or in C notation:
char buffer[100];
sb_struct_t sb = string_builder_init(buffer, 100);
sb_append_pchar(sb, "The x=");
sb_append_uint16(sb, x);
So it is pretty thin and simple wrapper around standard lib functions. It looks like a wheel, that I would reinvent if I write it myself.
At the same time I have found many string formatting and streaming libraries, but non that could be use for embedded usages (most of them have a lot of internal memory allocations for example)
So the question:
Do you know any ready to use C/C++ libraries that could fit for embedded development:
- No dynamic (possible to switch off) memory allocations
- Small overhead
- Not using C++ std:: (if C++)
- Better if especially designed for embedded usage