diff options
author | David Robillard <d@drobilla.net> | 2021-01-13 16:13:46 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-01-13 23:03:40 -0500 |
commit | dcff19f69bb5cbd718d507e7af6814b997bc4c3f (patch) | |
tree | be52e8387e6358601d5e2e7fcf094de6e0f98097 /src | |
parent | e2b1c1de25366f70db4cd00f1bb61eda513eb74b (diff) | |
download | serd-dcff19f69bb5cbd718d507e7af6814b997bc4c3f.tar.gz serd-dcff19f69bb5cbd718d507e7af6814b997bc4c3f.tar.bz2 serd-dcff19f69bb5cbd718d507e7af6814b997bc4c3f.zip |
Simplify literal construction API
Diffstat (limited to 'src')
-rw-r--r-- | src/node.c | 94 |
1 files changed, 62 insertions, 32 deletions
@@ -17,6 +17,7 @@ #include "node.h" #include "base64.h" +#include "serd_internal.h" #include "static_nodes.h" #include "string_utils.h" #include "system.h" @@ -224,48 +225,77 @@ 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) { - SerdNodeFlags flags = 0; - const size_t length = serd_substrlen(str.buf, str.len, &flags); - const size_t len = serd_node_pad_size(length); + assert(str.len); + assert(lang.len); - SerdNode* node = NULL; - if (lang.len) { - const size_t total_len = len + sizeof(SerdNode) + lang.len; + flags |= SERD_HAS_LANGUAGE; - node = serd_node_malloc(total_len, flags | SERD_HAS_LANGUAGE, SERD_LITERAL); - node->length = length; - memcpy(serd_node_buffer(node), str.buf, length); + const size_t len = serd_node_pad_size(str.len); + const size_t total_len = len + sizeof(SerdNode) + lang.len; - SerdNode* lang_node = node + 1 + (len / sizeof(SerdNode)); - lang_node->type = SERD_LITERAL; - lang_node->length = lang.len; - memcpy(serd_node_buffer(lang_node), lang.buf, lang.len); - serd_node_check_padding(lang_node); + SerdNode* node = serd_node_malloc(total_len, flags, SERD_LITERAL); + memcpy(serd_node_buffer(node), str.buf, str.len); + node->length = str.len; - } else if (datatype_uri.len) { - const size_t total_len = len + sizeof(SerdNode) + datatype_uri.len; + SerdNode* lang_node = node + 1 + (len / sizeof(SerdNode)); + lang_node->type = SERD_LITERAL; + lang_node->length = lang.len; + memcpy(serd_node_buffer(lang_node), lang.buf, lang.len); + serd_node_check_padding(lang_node); - node = serd_node_malloc(total_len, flags | SERD_HAS_DATATYPE, SERD_LITERAL); - node->length = length; - memcpy(serd_node_buffer(node), str.buf, length); + serd_node_check_padding(node); + return node; +} - SerdNode* datatype_node = node + 1 + (len / sizeof(SerdNode)); - datatype_node->type = SERD_URI; - datatype_node->length = datatype_uri.len; - memcpy(serd_node_buffer(datatype_node), datatype_uri.buf, datatype_uri.len); - serd_node_check_padding(datatype_node); +SerdNode* +serd_new_plain_literal(const SerdStringView str, const SerdStringView lang) +{ + if (!lang.len) { + return serd_new_string(str); + } - } else { - node = serd_node_malloc(length, flags, SERD_LITERAL); - memcpy(serd_node_buffer(node), str.buf, length); - node->length = length; + 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 = 0u; + serd_strlen(str.buf, &flags); + + flags |= SERD_HAS_DATATYPE; + + 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->length = str.len; + + SerdNode* datatype_node = node + 1 + (len / sizeof(SerdNode)); + datatype_node->length = 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; } |