aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-01-13 16:13:46 +0100
committerDavid Robillard <d@drobilla.net>2023-12-02 16:27:02 -0500
commit413b4d1f58bdf1d6aed785ae9b076c7b7b9a7350 (patch)
tree454cf92d9668431cbdcff8889e7597fcf162bf41 /src
parentcd89a74a2f7bf8c3efc3ecf1597cf39d6295db00 (diff)
downloadserd-413b4d1f58bdf1d6aed785ae9b076c7b7b9a7350.tar.gz
serd-413b4d1f58bdf1d6aed785ae9b076c7b7b9a7350.tar.bz2
serd-413b4d1f58bdf1d6aed785ae9b076c7b7b9a7350.zip
Simplify literal construction API
Diffstat (limited to 'src')
-rw-r--r--src/node.c92
1 files changed, 61 insertions, 31 deletions
diff --git a/src/node.c b/src/node.c
index 96a9bcaa..08893fc7 100644
--- a/src/node.c
+++ b/src/node.c
@@ -4,12 +4,14 @@
#include "node.h"
#include "base64.h"
+#include "serd_internal.h"
#include "string_utils.h"
#include "system.h"
#include "serd/attributes.h"
#include "serd/buffer.h"
#include "serd/node.h"
+#include "serd/string.h"
#include "serd/string_view.h"
#include "serd/uri.h"
@@ -179,47 +181,75 @@ 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.data, str.length, &flags);
- const size_t len = serd_node_pad_size(length);
+ assert(str.length);
+ assert(lang.length);
- SerdNode* node = NULL;
- if (lang.length) {
- const size_t total_len = len + sizeof(SerdNode) + lang.length;
+ 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.data, length);
+ const size_t len = serd_node_pad_size(str.length);
+ const size_t total_len = len + sizeof(SerdNode) + lang.length;
- SerdNode* lang_node = node + 1 + (len / sizeof(SerdNode));
- lang_node->type = SERD_LITERAL;
- lang_node->length = lang.length;
- memcpy(serd_node_buffer(lang_node), lang.data, lang.length);
+ SerdNode* node = serd_node_malloc(total_len, flags, SERD_LITERAL);
+ memcpy(serd_node_buffer(node), str.data, str.length);
+ node->length = str.length;
- } else if (datatype_uri.length) {
- const size_t total_len = len + sizeof(SerdNode) + datatype_uri.length;
+ SerdNode* lang_node = node + 1 + (len / sizeof(SerdNode));
+ lang_node->type = SERD_LITERAL;
+ lang_node->length = lang.length;
+ memcpy(serd_node_buffer(lang_node), lang.data, lang.length);
- node = serd_node_malloc(total_len, flags | SERD_HAS_DATATYPE, SERD_LITERAL);
- node->length = length;
- memcpy(serd_node_buffer(node), str.data, length);
+ return node;
+}
- SerdNode* datatype_node = node + 1 + (len / sizeof(SerdNode));
- datatype_node->type = SERD_URI;
- datatype_node->length = datatype_uri.length;
- memcpy(
- serd_node_buffer(datatype_node), datatype_uri.data, datatype_uri.length);
+SerdNode*
+serd_new_plain_literal(const SerdStringView str, const SerdStringView lang)
+{
+ if (!lang.length) {
+ return serd_new_string(str);
+ }
- } else {
- node = serd_node_malloc(length, flags, SERD_LITERAL);
- memcpy(serd_node_buffer(node), str.data, length);
- node->length = length;
+ SerdNodeFlags flags = 0;
+ serd_strlen(str.data, &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.length) {
+ return serd_new_string(str);
+ }
+
+ if (!strcmp(datatype_uri.data, NS_RDF "langString")) {
+ return NULL;
}
+ SerdNodeFlags flags = 0U;
+ serd_strlen(str.data, &flags);
+
+ flags |= SERD_HAS_DATATYPE;
+
+ const size_t len = serd_node_pad_size(str.length);
+ const size_t total_len = len + sizeof(SerdNode) + datatype_uri.length;
+
+ SerdNode* node = serd_node_malloc(total_len, flags, SERD_LITERAL);
+ memcpy(serd_node_buffer(node), str.data, str.length);
+ node->length = str.length;
+
+ SerdNode* datatype_node = node + 1 + (len / sizeof(SerdNode));
+ datatype_node->length = datatype_uri.length;
+ datatype_node->type = SERD_URI;
+ memcpy(
+ serd_node_buffer(datatype_node), datatype_uri.data, datatype_uri.length);
+
return node;
}