aboutsummaryrefslogtreecommitdiffstats
path: root/src/env.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/env.c')
-rw-r--r--src/env.c85
1 files changed, 53 insertions, 32 deletions
diff --git a/src/env.c b/src/env.c
index 2a11f287..529ae436 100644
--- a/src/env.c
+++ b/src/env.c
@@ -20,6 +20,7 @@
#include "serd/serd.h"
+#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -270,46 +271,66 @@ serd_env_expand_in_place(const SerdEnv* env,
return SERD_ERR_BAD_CURIE;
}
-SerdNode*
-serd_env_expand(const SerdEnv* env, const SerdNode* node)
+static SerdNode*
+expand_literal(const SerdEnv* env, const SerdNode* node)
{
- if (!node) {
- return NULL;
+ assert(serd_node_type(node) == SERD_LITERAL);
+
+ SerdNode* datatype = serd_env_expand(env, serd_node_datatype(node));
+ if (datatype) {
+ SerdNode* ret = serd_new_typed_literal(serd_node_string_view(node),
+ serd_node_string_view(datatype));
+
+ serd_node_free(datatype);
+ return ret;
}
- switch (node->type) {
- case SERD_LITERAL: {
- const SerdNode* const short_datatype = serd_node_datatype(node);
- if (short_datatype) {
- SerdNode* const datatype = serd_env_expand(env, short_datatype);
- if (datatype) {
- SerdNode* ret = serd_new_typed_literal(serd_node_string_view(node),
- serd_node_string_view(datatype));
- serd_node_free(datatype);
- return ret;
- }
- }
+ return NULL;
+}
+
+static SerdNode*
+expand_uri(const SerdEnv* env, const SerdNode* node)
+{
+ assert(serd_node_type(node) == SERD_URI);
+
+ return serd_new_resolved_uri(serd_node_string_view(node), env->base_uri);
+}
+
+static SerdNode*
+expand_curie(const SerdEnv* env, const SerdNode* node)
+{
+ assert(serd_node_type(node) == SERD_CURIE);
+
+ SerdStringView prefix;
+ SerdStringView suffix;
+ if (serd_env_expand_in_place(env, node, &prefix, &suffix)) {
return NULL;
}
- case SERD_URI:
- return serd_new_resolved_uri(serd_node_string_view(node), env->base_uri);
- case SERD_CURIE: {
- SerdStringView prefix;
- SerdStringView suffix;
- if (serd_env_expand_in_place(env, node, &prefix, &suffix)) {
+
+ const size_t len = prefix.len + suffix.len;
+ SerdNode* ret = serd_node_malloc(len, 0, SERD_URI);
+ char* buf = serd_node_buffer(ret);
+ snprintf(buf, len + 1, "%s%s", prefix.buf, suffix.buf);
+ ret->n_bytes = len;
+ return ret;
+}
+
+SerdNode*
+serd_env_expand(const SerdEnv* env, const SerdNode* node)
+{
+ if (node) {
+ switch (node->type) {
+ case SERD_LITERAL:
+ return expand_literal(env, node);
+ case SERD_URI:
+ return expand_uri(env, node);
+ case SERD_CURIE:
+ return expand_curie(env, node);
+ case SERD_BLANK:
return NULL;
}
-
- const size_t len = prefix.len + suffix.len;
- SerdNode* ret = serd_node_malloc(len, 0, SERD_URI);
- char* buf = serd_node_buffer(ret);
- snprintf(buf, len + 1, "%s%s", prefix.buf, suffix.buf);
- ret->n_bytes = len;
- return ret;
- }
- case SERD_BLANK:
- break;
}
+
return NULL;
}