diff options
-rw-r--r-- | NEWS | 1 | ||||
-rw-r--r-- | include/serd/serd.h | 33 | ||||
-rw-r--r-- | src/env.c | 10 | ||||
-rw-r--r-- | src/uri.c | 2 | ||||
-rw-r--r-- | src/uri_utils.h | 6 | ||||
-rw-r--r-- | src/writer.c | 12 | ||||
-rw-r--r-- | test/test_env.c | 4 | ||||
-rw-r--r-- | test/test_uri.c | 2 |
8 files changed, 38 insertions, 32 deletions
@@ -5,6 +5,7 @@ serd (1.1.1) unstable; urgency=medium * Remove serd_uri_to_path() * Remove support for reading Turtle named inline nodes extension * Remove useless character counting from API + * Rename SerdChunk to SerdStringView * Use char* for strings in public API -- David Robillard <d@drobilla.net> Wed, 13 Jul 2022 20:39:07 +0000 diff --git a/include/serd/serd.h b/include/serd/serd.h index 80d6b230..3dacf6a9 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -76,11 +76,16 @@ typedef enum { /// Bitwise OR of SerdNodeFlag values typedef uint32_t SerdNodeFlags; -/// An unterminated string fragment +/** + 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_NULLABLE buf; ///< Start of chunk - size_t len; ///< Length of chunk in bytes -} SerdChunk; + const char* SERD_NULLABLE buf; ///< Start of string + size_t len; ///< Length of string in bytes +} SerdStringView; /// A mutable buffer in memory typedef struct { @@ -217,12 +222,12 @@ typedef size_t (*SerdSink)(const void* SERD_NONNULL buf, in-place without allocating memory. */ typedef struct { - SerdChunk scheme; ///< Scheme - SerdChunk authority; ///< Authority - SerdChunk path_base; ///< Path prefix if relative - SerdChunk path; ///< Path suffix - SerdChunk query; ///< Query - SerdChunk fragment; ///< Fragment + SerdStringView scheme; ///< Scheme + SerdStringView authority; ///< Authority + SerdStringView path_base; ///< Path prefix if relative + SerdStringView path; ///< Path suffix + SerdStringView query; ///< Query + SerdStringView fragment; ///< Fragment } SerdURI; static const SerdURI SERD_URI_NULL = @@ -618,7 +623,7 @@ SERD_API bool serd_env_qualify(const SerdEnv* SERD_NULLABLE env, const SerdNode* SERD_NONNULL uri, SerdNode* SERD_NONNULL prefix, - SerdChunk* SERD_NONNULL suffix); + SerdStringView* SERD_NONNULL suffix); /** Expand `curie`. @@ -629,8 +634,8 @@ serd_env_qualify(const SerdEnv* SERD_NULLABLE env, SERD_API SerdStatus serd_env_expand(const SerdEnv* SERD_NULLABLE env, const SerdNode* SERD_NONNULL curie, - SerdChunk* SERD_NONNULL uri_prefix, - SerdChunk* SERD_NONNULL uri_suffix); + SerdStringView* SERD_NONNULL uri_prefix, + SerdStringView* SERD_NONNULL uri_suffix); /** Expand `node`, which must be a CURIE or URI, to a full URI. @@ -868,7 +873,7 @@ serd_buffer_sink(const void* SERD_NONNULL buf, void* SERD_NONNULL stream); /** - Finish a serialisation to a chunk with serd_buffer_sink(). + Finish writing to a buffer with serd_buffer_sink(). The returned string is the result of the serialisation, which is null terminated (by this function) and owned by the caller. @@ -169,7 +169,7 @@ bool serd_env_qualify(const SerdEnv* const env, const SerdNode* const uri, SerdNode* const prefix, - SerdChunk* const suffix) + SerdStringView* const suffix) { if (!env) { return false; @@ -192,8 +192,8 @@ serd_env_qualify(const SerdEnv* const env, SerdStatus serd_env_expand(const SerdEnv* const env, const SerdNode* const curie, - SerdChunk* const uri_prefix, - SerdChunk* const uri_suffix) + SerdStringView* const uri_prefix, + SerdStringView* const uri_suffix) { if (!env) { return SERD_ERR_BAD_CURIE; @@ -233,8 +233,8 @@ serd_env_expand_node(const SerdEnv* const env, const SerdNode* const node) return serd_node_new_uri_from_node(node, &env->base_uri, &ignored); } case SERD_CURIE: { - SerdChunk prefix; - SerdChunk suffix; + SerdStringView prefix; + SerdStringView suffix; if (serd_env_expand(env, node, &prefix, &suffix)) { return SERD_NODE_NULL; } @@ -232,7 +232,7 @@ remove_dot_segments(const char* const path, const size_t len, size_t* const up) /// Merge `base` and `path` in-place static void -merge(SerdChunk* const base, SerdChunk* const path) +merge(SerdStringView* const base, SerdStringView* const path) { size_t up = 0; const char* begin = remove_dot_segments(path->buf, path->len, &up); diff --git a/src/uri_utils.h b/src/uri_utils.h index c9f3ecf5..9146ee79 100644 --- a/src/uri_utils.h +++ b/src/uri_utils.h @@ -17,7 +17,7 @@ typedef struct { } SlashIndexes; static inline bool -chunk_equals(const SerdChunk* a, const SerdChunk* b) +slice_equals(const SerdStringView* a, const SerdStringView* b) { return a->len == b->len && !strncmp(a->buf, b->buf, a->len); } @@ -52,8 +52,8 @@ uri_rooted_index(const SerdURI* uri, const SerdURI* root) SlashIndexes indexes = {SIZE_MAX, SIZE_MAX}; if (!root || !root->scheme.len || - !chunk_equals(&root->scheme, &uri->scheme) || - !chunk_equals(&root->authority, &uri->authority)) { + !slice_equals(&root->scheme, &uri->scheme) || + !slice_equals(&root->authority, &uri->authority)) { return indexes; } diff --git a/src/writer.c b/src/writer.c index 241e6fbf..8070392f 100644 --- a/src/writer.c +++ b/src/writer.c @@ -717,9 +717,9 @@ write_uri_node(SerdWriter* const writer, const SerdNode* node, const Field field) { - SerdStatus st = SERD_SUCCESS; - SerdNode prefix = SERD_NODE_NULL; - SerdChunk suffix = {NULL, 0U}; + SerdStatus st = SERD_SUCCESS; + SerdNode prefix; + SerdStringView suffix; const bool has_scheme = serd_uri_string_has_scheme(node->buf); if (supports_abbrev(writer)) { @@ -778,9 +778,9 @@ write_uri_node(SerdWriter* const writer, SERD_NODISCARD static SerdStatus write_curie(SerdWriter* const writer, const SerdNode* const node) { - SerdChunk prefix = {NULL, 0}; - SerdChunk suffix = {NULL, 0}; - SerdStatus st = SERD_SUCCESS; + SerdStringView prefix = {NULL, 0}; + SerdStringView suffix = {NULL, 0}; + SerdStatus st = SERD_SUCCESS; // In fast-and-loose Turtle/TriG mode CURIEs are simply passed through const bool fast = diff --git a/test/test_env.c b/test/test_env.c index b084de3b..2d9dd702 100644 --- a/test/test_env.c +++ b/test/test_env.c @@ -31,8 +31,8 @@ test_env(void) assert(serd_env_set_base_uri(env, &SERD_NODE_NULL)); assert(serd_node_equals(serd_env_get_base_uri(env, NULL), &SERD_NODE_NULL)); - SerdChunk prefix; - SerdChunk suffix; + SerdStringView prefix; + SerdStringView suffix; assert(!serd_env_qualify(NULL, &u, &u, &suffix)); assert(serd_env_expand(NULL, &c, &prefix, &suffix)); assert(serd_env_expand(env, &b, &prefix, &suffix)); diff --git a/test/test_uri.c b/test/test_uri.c index d22ece63..2a42e5f6 100644 --- a/test/test_uri.c +++ b/test/test_uri.c @@ -130,7 +130,7 @@ test_uri_from_string(void) } static inline bool -chunk_equals(const SerdChunk* a, const SerdChunk* b) +chunk_equals(const SerdStringView* a, const SerdStringView* b) { return (!a->len && !b->len && !a->buf && !b->buf) || (a->len && b->len && a->buf && b->buf && |