Format strings

Introduction

Format strings are a very common concept in most programming languages and frameworks.

Manipulating strings to build output can sometimes be tedious, especially in environments like C which do not lend themselves to direct string interpolation.

The idea is simple and effective: passing around strings which contain interpolation markers (also called format specifiers), then passing along with such strings the additional elements that are to replace the interpolation markers and be included into the final result.

In standard ISO/ANSI C for example, the printf family of functions can handle format strings. However these format strings are not the same as in Dooba. We have decided that the ISO format strings are in fact lacking some features that we considered necessary.

Dooba format strings

The table below presents all possible interpolation markers and their associated arguments:

Marker Description Arguments Example
"\%" Literal '%' "Percentage: 56\%"
"%s" Zero-terminated string char *str "Hello, %s", "Paul"
"%t" String with length char *str, uint16_t len "Got message: %t", "hello", 5
"%c" Single character char c "Here is a character: %c", '@'
"%h" 16-bit unsigned integer (hexadecimal representation) uint16_t v "15 in hexadecimal: %h", 15
"%i" 16-bit unsigned integer (decimal representation) uint16_t v "0xff in decimal: %i", 0xff
"%H" 32-bit unsigned integer (hexadecimal representation) uint32_t v "4294967295 in hexadecimal: %H", 4294967295
"%I" 32-bit unsigned integer (decimal representation) uint32_t v "0xffffffff in decimal: %I", 0xffffffff
"%X" 64-bit unsigned integer (hexadecimal representation) uint64_t v "18446744073709551615 in hexadecimal: %X", 18446744073709551615
"%Y" 64-bit unsigned integer (decimal representation) uint64_t v "0xffffffffffffffff in decimal: %I", 0xffffffffffffffff
"%f" Floating point value (double size) double v "Value of PI: %f", 3.14159265359