From 3ec66799f0b9be2f83251721f91a8b913abd29ec 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 | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'include/serd') diff --git a/include/serd/serd.h b/include/serd/serd.h index 649217f3..dcc872ef 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -197,6 +197,36 @@ typedef struct { size_t len; ///< Length of chunk in bytes } SerdStringView; +#ifdef __cplusplus + +# define SERD_EMPTY_STRING(str) \ + SerdStringView { "", 0 } + +# define SERD_STATIC_STRING(str) \ + SerdStringView { (str), sizeof(str) - 1 } + +# define SERD_MEASURE_STRING(str) \ + SerdStringView { (str), (str) ? strlen(str) : 0 } + +# define SERD_STRING_VIEW(str, len) \ + SerdStringView { (str), (len) } + +#else + +# define SERD_EMPTY_STRING(str) \ + (SerdStringView) { "", 0 } + +# define SERD_STATIC_STRING(str) \ + (SerdStringView) { (str), sizeof(str) - 1 } + +# define SERD_MEASURE_STRING(str) \ + (SerdStringView) { (str), (str != NULL) ? strlen(str) : 0 } + +# define SERD_STRING_VIEW(str, len) \ + (SerdStringView) { (str), (len) } + +#endif + /// A mutable buffer in memory typedef struct { void* SERD_NULLABLE buf; ///< Buffer -- cgit v1.2.1