aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-02-24 15:49:21 -0500
committerDavid Robillard <d@drobilla.net>2023-12-02 16:27:02 -0500
commit94d3433dcf0e77d2c867c9a2dd6928acfea4184c (patch)
tree7746100e00d4d587ea64ca3034c53058051ebcf5
parent290be9610e32bb427dd0456e4b1e26526c479004 (diff)
downloadserd-94d3433dcf0e77d2c867c9a2dd6928acfea4184c.tar.gz
serd-94d3433dcf0e77d2c867c9a2dd6928acfea4184c.tar.bz2
serd-94d3433dcf0e77d2c867c9a2dd6928acfea4184c.zip
Add serd_node_uri_view()
-rw-r--r--include/serd/node.h14
-rw-r--r--src/node.c12
-rw-r--r--test/test_node.c16
3 files changed, 37 insertions, 5 deletions
diff --git a/include/serd/node.h b/include/serd/node.h
index 75725516..1c64b6b9 100644
--- a/include/serd/node.h
+++ b/include/serd/node.h
@@ -251,6 +251,20 @@ SERD_PURE_API SerdStringView
serd_node_string_view(const SerdNode* SERD_NONNULL node);
/**
+ Return a parsed view of the URI in a node.
+
+ It is best to check the node type before calling this function, though it is
+ safe to call on non-URI nodes. In that case, it will return a null view
+ with all fields zero.
+
+ Note that this parses the URI string contained in the node, so it is a good
+ idea to keep the value if you will be using it several times in the same
+ scope.
+*/
+SERD_API SerdURIView
+serd_node_uri_view(const SerdNode* SERD_NONNULL node);
+
+/**
@}
@defgroup serd_node_operators Operators
@{
diff --git a/src/node.c b/src/node.c
index c4620671..9acbb8be 100644
--- a/src/node.c
+++ b/src/node.c
@@ -458,6 +458,18 @@ serd_node_string_view(const SerdNode* const node)
return r;
}
+SerdURIView
+serd_node_uri_view(const SerdNode* const node)
+{
+ SerdURIView result = SERD_URI_NULL;
+
+ if (node->type == SERD_URI) {
+ serd_uri_parse(serd_node_string(node), &result);
+ }
+
+ return result;
+}
+
SerdNodeFlags
serd_node_flags(const SerdNode* const node)
{
diff --git a/test/test_node.c b/test/test_node.c
index a76bcd35..11351e31 100644
--- a/test/test_node.c
+++ b/test/test_node.c
@@ -166,14 +166,20 @@ test_node_equals(void)
static void
test_node_from_string(void)
{
- SerdNode* hello = serd_new_string(SERD_LITERAL, "hello\"");
- assert(serd_node_length(hello) == 6 &&
- serd_node_flags(hello) == SERD_HAS_QUOTE &&
- !strcmp(serd_node_string(hello), "hello\""));
-
+ SerdNode* const hello = serd_new_string(SERD_LITERAL, "hello\"");
+ assert(serd_node_length(hello) == 6);
+ assert(serd_node_flags(hello) == SERD_HAS_QUOTE);
+ assert(!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);
+
+ SerdNode* const uri = serd_new_string(SERD_URI, "http://example.org/");
+ assert(serd_node_length(uri) == 19);
+ assert(!strcmp(serd_node_string(uri), "http://example.org/"));
+ assert(serd_node_uri_view(uri).authority.length == 11);
+ assert(!strncmp(serd_node_uri_view(uri).authority.data, "example.org", 11));
+ serd_node_free(uri);
}
static void