diff options
author | David Robillard <d@drobilla.net> | 2020-08-12 13:18:09 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2021-03-08 23:23:06 -0500 |
commit | 7d69e5aef5203e66d5730ccf8aed1d7596ddcb2e (patch) | |
tree | 0d189585383a8374b690111f8b2b87d10a1897dd | |
parent | d71d5bf14845b552547905584954eab6b52ed18c (diff) | |
download | serd-7d69e5aef5203e66d5730ccf8aed1d7596ddcb2e.tar.gz serd-7d69e5aef5203e66d5730ccf8aed1d7596ddcb2e.tar.bz2 serd-7d69e5aef5203e66d5730ccf8aed1d7596ddcb2e.zip |
Split up node expansion code
-rw-r--r-- | src/env.c | 85 |
1 files changed, 53 insertions, 32 deletions
@@ -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; } |