aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--serd/serd.h4
-rw-r--r--src/node.c14
-rw-r--r--tests/serd_test.c12
3 files changed, 20 insertions, 10 deletions
diff --git a/serd/serd.h b/serd/serd.h
index 8744b634..c3ba5268 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -488,7 +488,9 @@ serd_node_new_substring(SerdType type, const char* str, size_t len);
*/
SERD_API
SerdNode*
-serd_node_new_literal(const char* str, const char* datatype, const char* lang);
+serd_node_new_literal(const char* str,
+ const SerdNode* datatype,
+ const char* lang);
/**
Return a deep copy of `node`.
diff --git a/src/node.c b/src/node.c
index 14526005..880aa3f3 100644
--- a/src/node.c
+++ b/src/node.c
@@ -113,9 +113,13 @@ serd_node_new_substring(SerdType type, const char* str, const size_t len)
}
SerdNode*
-serd_node_new_literal(const char* str, const char* datatype, const char* lang)
+serd_node_new_literal(const char* str,
+ const SerdNode* datatype,
+ const char* lang)
{
- if (!str || (lang && datatype && strcmp(datatype, NS_RDF "#langString"))) {
+ if (!str || (lang && datatype &&
+ strcmp(serd_node_buffer_c(datatype), NS_RDF "#langString")) ||
+ (datatype && serd_node_get_type(datatype) != SERD_URI)) {
return NULL;
}
@@ -138,16 +142,14 @@ serd_node_new_literal(const char* str, const char* datatype, const char* lang)
memcpy(serd_node_buffer(lang_node), lang, lang_len);
} else if (datatype) {
flags |= SERD_HAS_DATATYPE;
- const size_t datatype_len = strlen(datatype);
+ const size_t datatype_len = strlen(serd_node_buffer_c(datatype));
const size_t total_len = len + sizeof(SerdNode) + datatype_len;
node = serd_node_malloc(total_len, flags, SERD_LITERAL);
memcpy(serd_node_buffer(node), str, n_bytes);
node->n_bytes = n_bytes;
SerdNode* datatype_node = node + 1 + (len / serd_node_align);
- datatype_node->type = SERD_URI;
- datatype_node->n_bytes = datatype_len;
- memcpy(serd_node_buffer(datatype_node), datatype, datatype_len);
+ memcpy(datatype_node, datatype, sizeof(SerdNode) + datatype_len);
} else {
node = serd_node_malloc(n_bytes, flags, SERD_LITERAL);
memcpy(serd_node_buffer(node), str, n_bytes);
diff --git a/tests/serd_test.c b/tests/serd_test.c
index 2f0d32fe..0049dbca 100644
--- a/tests/serd_test.c
+++ b/tests/serd_test.c
@@ -306,8 +306,10 @@ main(void)
"en"));
serd_node_free(hello_l);
- SerdNode* hello_dt = serd_node_new_literal(
- "hello_dt\"", "http://example.org/Thing", NULL);
+ SerdNode* eg_Thing = serd_node_new_uri_from_string(
+ "http://example.org/Thing", NULL, NULL);
+
+ SerdNode* hello_dt = serd_node_new_literal("hello_dt\"", eg_Thing, NULL);
assert(serd_node_get_length(hello_dt) == 9);
assert(!strcmp(serd_node_get_string(hello_dt), "hello_dt\""));
assert(serd_node_get_flags(hello_dt) ==
@@ -315,6 +317,7 @@ main(void)
assert(!strcmp(serd_node_get_string(serd_node_get_datatype(hello_dt)),
"http://example.org/Thing"));
serd_node_free(hello_dt);
+ serd_node_free(eg_Thing);
// Test serd_node_new_uri_from_string
@@ -459,7 +462,9 @@ main(void)
junk[i][0], junk[i][1], junk[i][2]));
}
- SerdNode* t = serd_node_new_literal((char*)buf, "urn:Type", NULL);
+ SerdNode* urn_Type = serd_node_new_uri_from_string("urn:Type", NULL, NULL);
+
+ SerdNode* t = serd_node_new_literal((char*)buf, urn_Type, NULL);
SerdNode* l = serd_node_new_literal((char*)buf, NULL, "en");
const SerdNode* good[][5] = { { s, p, o },
{ s, p, o },
@@ -497,6 +502,7 @@ main(void)
serd_node_free(o);
serd_node_free(t);
serd_node_free(l);
+ serd_node_free(urn_Type);
// Test buffer sink
SerdBuffer buffer = { NULL, 0 };