From 830b02f5bb3baa455fe19817bd068da693b89096 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 24 Feb 2021 15:44:27 -0500 Subject: Add string view construction macros --- include/serd/serd.h | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) (limited to 'include/serd/serd.h') diff --git a/include/serd/serd.h b/include/serd/serd.h index 3fe49c35..6120df16 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -21,9 +21,9 @@ #include #include -#include #include #include +#include // IWYU pragma: keep #if defined(_WIN32) && !defined(SERD_STATIC) && defined(SERD_INTERNAL) # define SERD_API __declspec(dllexport) @@ -91,6 +91,11 @@ typedef enum { /// Bitwise OR of SerdNodeFlag values typedef uint32_t SerdNodeFlags; +/** + @defgroup serd_string_view String View + @{ +*/ + /** An immutable slice of a string. @@ -102,6 +107,69 @@ typedef struct { size_t len; ///< Length of string in bytes } SerdStringView; +#ifdef __cplusplus + +# define SERD_EMPTY_STRING() \ + SerdStringView { "", 0u } + +# define SERD_STRING(str) \ + SerdStringView { str, strlen(str) } + +# define SERD_OPTIONAL_STRING(str) \ + SerdStringView { (str) ? (str) : "", (str) ? strlen(str) : 0u } + +# define SERD_SUBSTRING(str, len) \ + SerdStringView { (str), (len) } + +#else + +/// Return a view of an empty string +# define SERD_EMPTY_STRING() \ + (SerdStringView) { "", 0u } + +/** + 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 Non-null pointer to the start of a null-terminated C string. +*/ +# define SERD_STRING(str) \ + (SerdStringView) { (str), strlen(str) } + +/** + Return a view of an entire string by measuring it, or the empty string. + + This is the same as SERD_STRING(), but tolerates null, in which case an empty + string view is returned. + + @param str Pointer to the start of a null-terminated C string, or null. +*/ +# define SERD_OPTIONAL_STRING(str) \ + (SerdStringView) { (str) ? (str) : "", (str) ? strlen(str) : 0u } + +/** + 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. +*/ +# define SERD_SUBSTRING(str, len) \ + (SerdStringView) { (str), (len) } + +#endif + +/** + @} +*/ + /// A mutable buffer in memory typedef struct { void* SERD_NULLABLE buf; ///< Buffer -- cgit v1.2.1