aboutsummaryrefslogtreecommitdiffstats
path: root/src/node.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-01-13 16:13:46 +0100
committerDavid Robillard <d@drobilla.net>2021-03-08 23:23:05 -0500
commit6bcd18ae60482790b645a345f718e7099250f261 (patch)
tree154fa41e8540ff03d43c9a1fa69568e0af5483d2 /src/node.c
parent8f84a055c88c2c271abe9cf86e998a3e5d0a4ced (diff)
downloadserd-6bcd18ae60482790b645a345f718e7099250f261.tar.gz
serd-6bcd18ae60482790b645a345f718e7099250f261.tar.bz2
serd-6bcd18ae60482790b645a345f718e7099250f261.zip
Simplify literal construction API
Diffstat (limited to 'src/node.c')
-rw-r--r--src/node.c111
1 files changed, 74 insertions, 37 deletions
diff --git a/src/node.c b/src/node.c
index ed962150..007be2c0 100644
--- a/src/node.c
+++ b/src/node.c
@@ -183,58 +183,95 @@ 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)
{
return serd_new_simple_node(SERD_BLANK, str);