aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/env.c52
1 files changed, 35 insertions, 17 deletions
diff --git a/src/env.c b/src/env.c
index ef8dcca2..2bae1f83 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>
@@ -243,6 +244,7 @@ serd_env_qualify(const SerdEnv* const env, const SerdNode* const uri)
const size_t prefix_len = serd_node_length(prefix);
const size_t length = prefix_len + 1 + suffix.len;
SerdNode* node = serd_node_malloc(length, 0, SERD_CURIE);
+
memcpy(serd_node_buffer(node), serd_node_string(prefix), prefix_len);
serd_node_buffer(node)[prefix_len] = ':';
memcpy(serd_node_buffer(node) + 1 + prefix_len, suffix.buf, suffix.len);
@@ -277,8 +279,36 @@ serd_env_expand_in_place(const SerdEnv* const env,
return SERD_ERR_BAD_CURIE;
}
+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;
+ }
+
+ 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->length = len;
+ return ret;
+}
+
SerdNode*
-serd_env_expand(const SerdEnv* const env, const SerdNode* const node)
+serd_env_expand(const SerdEnv* env, const SerdNode* node)
{
if (!env || !node) {
return NULL;
@@ -288,25 +318,13 @@ serd_env_expand(const SerdEnv* const env, const SerdNode* const node)
case SERD_LITERAL:
break;
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)) {
- 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->length = len;
- return ret;
- }
+ return expand_uri(env, node);
+ case SERD_CURIE:
+ return expand_curie(env, node);
case SERD_BLANK:
break;
}
+
return NULL;
}