diff options
author | David Robillard <d@drobilla.net> | 2022-10-23 13:41:54 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-10-23 14:57:45 -0400 |
commit | bc4b359747466f760f8861dad6d99d7005ff6a04 (patch) | |
tree | 5ef45925bdcc016629fde220078f625e9dc8c3d5 /doc/string_views.rst | |
parent | 5891e6fb17de98fae6764bdff929bcaa21a1b36c (diff) | |
download | zix-bc4b359747466f760f8861dad6d99d7005ff6a04.tar.gz zix-bc4b359747466f760f8861dad6d99d7005ff6a04.tar.bz2 zix-bc4b359747466f760f8861dad6d99d7005ff6a04.zip |
Build reference documentation
Diffstat (limited to 'doc/string_views.rst')
-rw-r--r-- | doc/string_views.rst | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/doc/string_views.rst b/doc/string_views.rst new file mode 100644 index 0000000..b6e3fd9 --- /dev/null +++ b/doc/string_views.rst @@ -0,0 +1,56 @@ +.. + Copyright 2020-2022 David Robillard <d@drobilla.net> + SPDX-License-Identifier: ISC + +String Views +============ + +.. default-domain:: c +.. highlight:: c + +For performance reasons, +many functions in zix that take a string take a :struct:`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: + +:func:`zix_empty_string` + + Constructs a view of an empty string, for example: + + .. literalinclude:: overview_code.c + :start-after: begin make-empty-string + :end-before: end make-empty-string + :dedent: 2 + +:func:`zix_string` + + Constructs a view of an arbitrary string, for example: + + .. literalinclude:: overview_code.c + :start-after: begin measure-string + :end-before: end measure-string + :dedent: 2 + + This calls ``strlen`` to measure the string. + Modern compilers should optimize this away if the parameter is a literal. + +:func:`zix_substring` + + Constructs a view of a slice of a string with an explicit length, + for example: + + .. literalinclude:: overview_code.c + :start-after: begin make-string-view + :end-before: end make-string-view + :dedent: 2 + + 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 :func:`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. |