Using the C standard libraries for string processing is a terrible option when it comes to programming micro-controllers. The standard C string functions can be unsafe and most C string functions are prohibited by high integrity coding standards. They will also bloat compiled binaries by up to 30Kb.

sprintf, atoi and atof blow out the size of binaries, sometimes by up to 30kB
sprintf, atoi and atof blow out the size of binaries, sometimes by up to 30kB

The Embedded Tool Kit offers a lightweight C++ alternative. ETK contains everything you need to work with strings on micro-controllers in these classes.

  • Rope – a C-string wrapper
  • StaticString – a complete string class that uses static memory
  • Tokeniser – a string splitting utility

Arduino StaticString Library Download

etk::Rope

Rope is a C string wrapper. It does everything that’s needed to use C-string effectively such as concatenation of other strings, floats and integers, converting strings to floats and integers, calculating string length, comparing strings and so on. Rope requires a pointer to a C string (character array) and the length of the array so it can avoid buffer overruns. Rope will then perform operations on this C string.

Let’s look at some examples

Rope creation

Construct and initialise

String concatenation

Set cursor

 

Appending integers with padding

Appending floats with precision parameter

Strings to integers

Strings to floats

String length

Comparing strings

etk::StaticString

StaticString is a string template class that does not secretly allocate memory. The maximum length of a StaticString is specified by the template parameter.

StaticStrings are just a thin wrapper over the Rope class. They essentially are a rope, but contain their own memory.

Examples

StaticString actually does provide a few more functions than Rope. It can insert and remove characters.

It also has a fill function

And finally, StaticString has to_upper and to_lower functions.

String Tokeniser

The tokeniser splits strings and extracts the pieces. Let’s say you want to extract words from a phrase

The tokeniser can use virtually any type of string object. The only limitation is that the input string provided to etk::make_tokeniser MUST be null terminated (i.e. have a null character ‘\0’ at the end). Examples of string types that can be used are C strings (character arrays), StaticStrings, Rope, std::string and pretty much anything that allows the use of subscript operators ( operator [] ).

Here is an example using etk::Arrays – it’s a silly example but it showcases the diversity of the tokeniser

 

Leave a Reply

Your email address will not be published. Required fields are marked *