aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-04-29 18:36:11 +0200
committerDavid Robillard <d@drobilla.net>2019-12-19 20:52:33 -0500
commit5522f5ff705374d8d7bba3d21a05674640967c3e (patch)
treee192cc79b28b2bbf0cac85a7abbd5e3ae652bff0
parentbff380b4dbeb28d21e71686d389f21403525150b (diff)
downloadserd-5522f5ff705374d8d7bba3d21a05674640967c3e.tar.gz
serd-5522f5ff705374d8d7bba3d21a05674640967c3e.tar.bz2
serd-5522f5ff705374d8d7bba3d21a05674640967c3e.zip
Add blank node API
-rw-r--r--serd/serd.h10
-rw-r--r--src/node.c14
-rw-r--r--tests/serd_test.c16
3 files changed, 40 insertions, 0 deletions
diff --git a/serd/serd.h b/serd/serd.h
index 41ce9820..2a79ff25 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -486,6 +486,16 @@ serd_node_new_literal(const char* str,
const char* lang);
/**
+ Create a new blank node.
+
+ Note this function measures `str`, which is a common bottleneck.
+ Use sord_node_from_serd_node() instead if `str` is already measured.
+*/
+SERD_API
+SerdNode*
+serd_node_new_blank(const char* str);
+
+/**
Return a deep copy of `node`.
*/
SERD_API
diff --git a/src/node.c b/src/node.c
index c2bb3095..68a451f2 100644
--- a/src/node.c
+++ b/src/node.c
@@ -164,6 +164,20 @@ serd_node_new_literal(const char* str,
}
SerdNode*
+serd_node_new_blank(const char* str)
+{
+ if (!str) {
+ return NULL;
+ }
+
+ const size_t n_bytes = strlen(str);
+ SerdNode* node = serd_node_malloc(n_bytes, 0, SERD_BLANK);
+ memcpy(serd_node_buffer(node), str, n_bytes);
+ node->n_bytes = n_bytes;
+ return node;
+}
+
+SerdNode*
serd_node_copy(const SerdNode* node)
{
if (!node) {
diff --git a/tests/serd_test.c b/tests/serd_test.c
index 4d31cbe3..76ba1966 100644
--- a/tests/serd_test.c
+++ b/tests/serd_test.c
@@ -472,8 +472,22 @@ test_relative_uri(void)
}
static void
+test_blank(void)
+{
+ assert(!serd_node_new_blank(NULL));
+
+ SerdNode* blank = serd_node_new_blank("b0");
+ assert(serd_node_get_length(blank) == 2);
+ assert(serd_node_get_flags(blank) == 0);
+ assert(!strcmp(serd_node_get_string(blank), "b0"));
+ serd_node_free(blank);
+}
+
+static void
test_env(void)
{
+ SerdWorld* world = serd_world_new();
+
SerdNode* u = serd_node_new_string(SERD_URI, "http://example.org/foo");
SerdNode* b = serd_node_new_string(SERD_CURIE, "invalid");
SerdNode* c = serd_node_new_string(SERD_CURIE, "eg.2:b");
@@ -526,6 +540,7 @@ test_env(void)
serd_node_free(u);
serd_env_free(env);
+ serd_world_free(world);
}
static void
@@ -688,6 +703,7 @@ main(void)
test_literal();
test_uri_from_string();
test_relative_uri();
+ test_blank();
test_env();
test_read_chunks();