diff options
author | David Robillard <d@drobilla.net> | 2022-10-01 20:12:13 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 16:27:02 -0500 |
commit | cd3d9986f40fd4e605ac2e8168512065439173e2 (patch) | |
tree | 72d0418cbd8b8b5c7e43880c68831819e12b1982 /include/serd/string_view.h | |
parent | 1f09497ae009daade56c450e73bf6425b27cea24 (diff) | |
download | serd-cd3d9986f40fd4e605ac2e8168512065439173e2.tar.gz serd-cd3d9986f40fd4e605ac2e8168512065439173e2.tar.bz2 serd-cd3d9986f40fd4e605ac2e8168512065439173e2.zip |
Split up public API header
Diffstat (limited to 'include/serd/string_view.h')
-rw-r--r-- | include/serd/string_view.h | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/include/serd/string_view.h b/include/serd/string_view.h new file mode 100644 index 00000000..767cc6b1 --- /dev/null +++ b/include/serd/string_view.h @@ -0,0 +1,79 @@ +// Copyright 2011-2021 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#ifndef SERD_STRING_VIEW_H +#define SERD_STRING_VIEW_H + +#include "serd/attributes.h" + +#include <stddef.h> +#include <string.h> + +SERD_BEGIN_DECLS + +/** + @defgroup serd_string_view String View + @ingroup serd_utilities + @{ +*/ + +/** + An immutable slice of a string. + + This type is used for many string parameters, to allow referring to slices + of strings in-place and to avoid redundant string measurement. +*/ +typedef struct { + const char* SERD_NONNULL buf; ///< Start of string + size_t len; ///< Length of string in bytes +} SerdStringView; + +/// Return a view of an empty string +SERD_ALWAYS_INLINE_FUNC SERD_CONST_FUNC static inline SerdStringView +serd_empty_string(void) +{ + const SerdStringView view = {"", 0U}; + return view; +} + +/** + Return a view of a substring, or a premeasured string. + + This makes either a view of a slice of a string (which may not be null + terminated), or a view of a string that has already been measured. This is + faster than serd_string() for dynamic strings since it does not call + `strlen`, so should be used when the length of the string is already known. + + @param str Pointer to the start of the substring. + + @param len Length of the substring in bytes, not including the trailing null + terminator if present. +*/ +SERD_ALWAYS_INLINE_FUNC SERD_CONST_FUNC static inline SerdStringView +serd_substring(const char* const SERD_NONNULL str, const size_t len) +{ + const SerdStringView view = {str, len}; + return view; +} + +/** + Return a view of an entire string by measuring it. + + This makes a view of the given string by measuring it with `strlen`. + + @param str Pointer to the start of a null-terminated C string, or null. +*/ +SERD_ALWAYS_INLINE_FUNC SERD_PURE_FUNC static inline SerdStringView +// NOLINTNEXTLINE(clang-diagnostic-unused-function) +serd_string(const char* const SERD_NULLABLE str) +{ + return str ? serd_substring(str, strlen(str)) : serd_empty_string(); +} + +/** + @} +*/ + +SERD_END_DECLS + +#endif // SERD_STRING_VIEW_H |