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:
Constructs a view of an empty string, for example:
ZixStringView empty = zix_empty_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.
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.