1.2 String Views — Zix 0.8.2 documentation

# 1.2 String Views

For performance reasons, many functions in zix that take a string take a `ZixStringView`, rather than a bare pointer. This forces code to be explicit about string measurement, which discourages common patterns of repeated measurement of the same string. For convenience, several macros and functions are provided for constructing string views:

`ZIX_STATIC_STRING`

> Initializes a string view from a string literal. Note that this may only be used with inlined string literals, passing a pointer to an arbitrary string is undefined behaviour, for example:

>     static const ZixStringView hello = ZIX_STATIC_STRING("hello");

`zix_empty_string()`

> Constructs a view of an empty string, for example:

>     ZixStringView empty = zix_empty_string();

`zix_string()`

> Constructs a view of an arbitrary string, for example:

>     ZixStringView view = zix_string(string_pointer);

> This calls `strlen` to measure the string. Modern compilers should optimize this away if the parameter is a literal.

`zix_substring()`

> Constructs a view of a slice of a string with an explicit length, for example:

>     ZixStringView slice = zix_substring(string_pointer, 4);

> This can also be used to create a view of a pre-measured string. If the length a dynamic string is already known, this is faster than `zix_string()`, since it avoids redundant measurement.

These constructors can be used inline when passing parameters, but if the same dynamic string is used several times, it is better to make a string view variable to avoid redundant measurement.