From 6bcd18ae60482790b645a345f718e7099250f261 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 13 Jan 2021 16:13:46 +0100 Subject: Simplify literal construction API --- src/env.c | 5 ++- src/node.c | 111 ++++++++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 76 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/env.c b/src/env.c index f0d0705c..944319d8 100644 --- a/src/env.c +++ b/src/env.c @@ -210,9 +210,8 @@ serd_env_expand_node(const SerdEnv* env, const SerdNode* node) if (short_datatype) { SerdNode* const datatype = serd_env_expand_node(env, short_datatype); if (datatype) { - SerdNode* ret = serd_new_literal(serd_node_string_view(node), - serd_node_string_view(datatype), - SERD_EMPTY_STRING()); + SerdNode* ret = serd_new_typed_literal(serd_node_string_view(node), + serd_node_string_view(datatype)); serd_node_free(datatype); return ret; } diff --git a/src/node.c b/src/node.c index ed962150..007be2c0 100644 --- a/src/node.c +++ b/src/node.c @@ -183,57 +183,94 @@ serd_new_string(const SerdStringView str) return node; } -SerdNode* -serd_new_literal(const SerdStringView str, - const SerdStringView datatype_uri, - const SerdStringView lang) +/// Internal pre-measured implementation of serd_new_plain_literal +static SerdNode* +serd_new_plain_literal_i(const SerdStringView str, + SerdNodeFlags flags, + const SerdStringView lang) { - if (!str.len || (lang.len && datatype_uri.len && - strcmp(datatype_uri.buf, NS_RDF "langString"))) { - return NULL; - } + assert(str.len); + assert(lang.len); - SerdNodeFlags flags = 0; - const size_t n_bytes = serd_substrlen(str.buf, str.len, &flags); - const size_t len = serd_node_pad_size(n_bytes); + flags |= SERD_HAS_LANGUAGE; - SerdNode* node = NULL; - if (lang.len) { - const size_t total_len = len + sizeof(SerdNode) + lang.len; + const size_t len = serd_node_pad_size(str.len); + const size_t total_len = len + sizeof(SerdNode) + lang.len; - node = serd_node_malloc(total_len, flags | SERD_HAS_LANGUAGE, SERD_LITERAL); - node->n_bytes = n_bytes; - memcpy(serd_node_buffer(node), str.buf, n_bytes); + SerdNode* node = serd_node_malloc(total_len, flags, SERD_LITERAL); + memcpy(serd_node_buffer(node), str.buf, str.len); + node->n_bytes = str.len; - SerdNode* lang_node = node + 1 + (len / serd_node_align); - lang_node->type = SERD_LITERAL; - lang_node->n_bytes = lang.len; - memcpy(serd_node_buffer(lang_node), lang.buf, lang.len); - serd_node_check_padding(lang_node); + SerdNode* lang_node = node + 1 + (len / serd_node_align); + lang_node->type = SERD_LITERAL; + lang_node->n_bytes = lang.len; + memcpy(serd_node_buffer(lang_node), lang.buf, lang.len); + serd_node_check_padding(lang_node); - } else if (datatype_uri.len) { - const size_t total_len = len + sizeof(SerdNode) + datatype_uri.len; + serd_node_check_padding(node); + return node; +} - node = serd_node_malloc(total_len, flags | SERD_HAS_DATATYPE, SERD_LITERAL); - node->n_bytes = n_bytes; - memcpy(serd_node_buffer(node), str.buf, n_bytes); +/// Internal pre-measured implementation of serd_new_typed_literal +static SerdNode* +serd_new_typed_literal_i(const SerdStringView str, + SerdNodeFlags flags, + const SerdStringView datatype_uri) +{ + assert(str.len); + assert(datatype_uri.len); + assert(strcmp(datatype_uri.buf, NS_RDF "langString")); - SerdNode* datatype_node = node + 1 + (len / serd_node_align); - datatype_node->type = SERD_URI; - datatype_node->n_bytes = datatype_uri.len; - memcpy(serd_node_buffer(datatype_node), datatype_uri.buf, datatype_uri.len); - serd_node_check_padding(datatype_node); + flags |= SERD_HAS_DATATYPE; - } else { - node = serd_node_malloc(n_bytes, flags, SERD_LITERAL); - memcpy(serd_node_buffer(node), str.buf, n_bytes); - node->n_bytes = n_bytes; - } + const size_t len = serd_node_pad_size(str.len); + const size_t total_len = len + sizeof(SerdNode) + datatype_uri.len; + + SerdNode* node = serd_node_malloc(total_len, flags, SERD_LITERAL); + memcpy(serd_node_buffer(node), str.buf, str.len); + node->n_bytes = str.len; + + SerdNode* datatype_node = node + 1 + (len / serd_node_align); + datatype_node->n_bytes = datatype_uri.len; + datatype_node->type = SERD_URI; + memcpy(serd_node_buffer(datatype_node), datatype_uri.buf, datatype_uri.len); + serd_node_check_padding(datatype_node); serd_node_check_padding(node); return node; } +SerdNode* +serd_new_plain_literal(const SerdStringView str, const SerdStringView lang) +{ + if (!lang.len) { + return serd_new_string(str); + } + + SerdNodeFlags flags = 0; + serd_strlen(str.buf, &flags); + + return serd_new_plain_literal_i(str, flags, lang); +} + +SerdNode* +serd_new_typed_literal(const SerdStringView str, + const SerdStringView datatype_uri) +{ + if (!datatype_uri.len) { + return serd_new_string(str); + } + + if (!strcmp(datatype_uri.buf, NS_RDF "langString")) { + return NULL; + } + + SerdNodeFlags flags = 0; + serd_strlen(str.buf, &flags); + + return serd_new_typed_literal_i(str, flags, datatype_uri); +} + SerdNode* serd_new_blank(const SerdStringView str) { -- cgit v1.2.1