String processing

Basic string operations

There exists a small set of string-manipulation operations as global functions in the Genode namespace.

repos/base/include/util/string.h

To cover the common case of embedding a string buffer as a member variable in a class, there exists the String class template, which alleviates the need for C-style arrays in such situations.

The String constructor takes any number of arguments, which will appear concatenated in the constructed String. Each argument must be printable as explained in Section Diagnostic output.

Genode::String

There exist a number of printable helper classes that cover typical use cases for producing formatted text output.

Number_of_bytes

wraps an integer value and produces an output suffixed with K, M, or G whenever the value is a multiple of a kilobyte, megabyte, or gigabyte.

Cstring

wraps a plain C character array to make it printable. There exist two constructors. The constructor with one argument expects a null-terminated character array. The other constructor takes the number of to-be-printed characters as arguments.

Hex

wraps an integer value and produces hexadecimal output.

Char

produces a character corresponding to the ASCII value the wrapped integer argument.

Tokenizing

For parsing structured text such as argument strings or XML, simple tokenizing support is provided via the Token class.

Genode::Token

Diagnostic output

To enable components to produce diagnostic output like errors, warnings, and log messages, Genode offers a simple Output interface for sequentially writing single characters or character sequences.

Genode::Output

Functions for generating output for different types are named print and take an Output & as first argument. The second argument is a const & to the value to print. Overloads of the print function for commonly used basic types are provided. Furthermore, there is a function template that is used if none of the type-specific overloads match. This function template expects the argument to be an object with a print method. In contrast to a plain print function overload, such a method is able to incorporate private state into the output.

repos/base/include/base/output.h

The component's execution environment provides an implementation of the Output interface that targets a LOG session. This output back end is offered to the component in the form of the log, warning, and error functions that accept an arbitrary number of arguments that are printed in a concatenated fashion. Each messages is implicitly finalized with a newline character.

repos/base/include/base/log.h

Unicode handling

The string-handling utilities described in Section Basic string operations operate on ASCII-encoded character strings where each character is encoded as one byte. It goes without saying that ASCII is unsuitable for user-facing components that are ultimately expected to support the display of international characters. The Utf8_ptr utility accommodates such components with an easy way to extract a sequence of Unicode codepoints from an UTF-8-encoded string.

Genode::Utf8_ptr