diff options
author | David Robillard <d@drobilla.net> | 2020-08-12 13:18:09 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-01-13 23:04:22 -0500 |
commit | a5ff652baf200ff9a368b2451c45676cc4278ee2 (patch) | |
tree | ae4ac2876d9f9d95edda9318c3243ca1016162d8 /src | |
parent | 870eacef28930c9cc5d84fb7424f4519907ea877 (diff) | |
download | serd-a5ff652baf200ff9a368b2451c45676cc4278ee2.tar.gz serd-a5ff652baf200ff9a368b2451c45676cc4278ee2.tar.bz2 serd-a5ff652baf200ff9a368b2451c45676cc4278ee2.zip |
Split up serd_env_expand() implementation
Diffstat (limited to 'src')
-rw-r--r-- | src/env.c | 52 |
1 files changed, 35 insertions, 17 deletions
@@ -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; } |