aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/serd/node.h10
-rw-r--r--src/node.c9
-rw-r--r--test/test_node.c2
3 files changed, 21 insertions, 0 deletions
diff --git a/include/serd/node.h b/include/serd/node.h
index eb4f8c9e..75725516 100644
--- a/include/serd/node.h
+++ b/include/serd/node.h
@@ -5,6 +5,7 @@
#define SERD_NODE_H
#include "serd/attributes.h"
+#include "serd/string_view.h"
#include "serd/uri.h"
#include <stdbool.h>
@@ -241,6 +242,15 @@ SERD_CONST_API const char* SERD_NONNULL
serd_node_string(const SerdNode* SERD_NONNULL node);
/**
+ Return a view of the string in a node.
+
+ This is a convenience wrapper for serd_node_string() and serd_node_length()
+ that can be used to get both in a single call.
+*/
+SERD_PURE_API SerdStringView
+serd_node_string_view(const SerdNode* SERD_NONNULL node);
+
+/**
@}
@defgroup serd_node_operators Operators
@{
diff --git a/src/node.c b/src/node.c
index ea9d174d..c4620671 100644
--- a/src/node.c
+++ b/src/node.c
@@ -11,6 +11,7 @@
#include "serd/buffer.h"
#include "serd/node.h"
#include "serd/string.h"
+#include "serd/string_view.h"
#include "serd/uri.h"
#include <assert.h>
@@ -449,6 +450,14 @@ serd_node_length(const SerdNode* const node)
return node->length;
}
+SerdStringView
+serd_node_string_view(const SerdNode* const node)
+{
+ const SerdStringView r = {(const char*)(node + 1), node->length};
+
+ return r;
+}
+
SerdNodeFlags
serd_node_flags(const SerdNode* const node)
{
diff --git a/test/test_node.c b/test/test_node.c
index 937d8a5f..a76bcd35 100644
--- a/test/test_node.c
+++ b/test/test_node.c
@@ -171,6 +171,8 @@ test_node_from_string(void)
serd_node_flags(hello) == SERD_HAS_QUOTE &&
!strcmp(serd_node_string(hello), "hello\""));
+ assert(!strcmp(serd_node_string_view(hello).data, "hello\""));
+ assert(serd_node_string_view(hello).length == 6);
serd_node_free(hello);
}