2

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:

  1. Xprintf are bulky functions if one count each cycle (that happens often). Even without floating point
  2. 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:

  1. No dynamic (possible to switch off) memory allocations
  2. Small overhead
  3. Not using C++ std:: (if C++)
  4. Better if especially designed for embedded usage
1

That would seem to suit your needs of building/composing/appending to a string within a fixed buffer. fmt is dependent on the standard library, so you could only use it if you replace some of those dependencies (vectors, sets etc.) - although it's probably the case that the part of the code you're interested in actually depends on a lot less. Maybe you could carve it out. FastFormat seems to have a lot less standard library dependencies, and a lot of the external dependencies it does have seem to restricted to specific sinks, so it might even possible to even build it with no standard library dependence, although I would guess it would need some elbow grease as well.

  • Thank you for the answer! Digging into fmt... if I could carve it out. – MajesticRa Jul 24 '16 at 17:41
0

If you need to use floating point or a number of formats/bases then your best bet is the snprint function which is "safer" for embedded use as you pre-declare the buffer to use and the function is size limited to avoid overflows. As well as flexibility it has the advantage of portability since it is a part of the ANSI standard for C/C++.

If you only have a very limited number of formats, (not including floats), then the itoa function supported by some/most compilers is usually your best bet but it is not always available as it is not in the ANSI standard.

  • Thanks! Unfortunately I can't edit my post to add "Not XYprintf family") snprintf is what I'm using right now. Basically, your 2 paragraphs fit exactly into my "My concerns..." 2 bullets)) Why I'm looking something else than XYprintf and itoa, ftoa (BTW, not easy to get good for embedded), ultoa... – MajesticRa Jul 24 '16 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.