From a365de08588b5da0783d0ec905576f272108442c Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 2 Mar 2021 11:09:33 -0500 Subject: Simplify SerdEnv API --- src/env.c | 45 ++++++++++++++++++++++++++++++++++----------- src/env.h | 23 +++++++++++++++++++++++ src/writer.c | 4 ++-- 3 files changed, 59 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/env.c b/src/env.c index 944319d8..f7c8c6ba 100644 --- a/src/env.c +++ b/src/env.c @@ -155,10 +155,10 @@ serd_env_set_prefix(SerdEnv* env, } bool -serd_env_qualify(const SerdEnv* env, - const SerdNode* uri, - const SerdNode** prefix, - SerdStringView* suffix) +serd_env_qualify_in_place(const SerdEnv* env, + const SerdNode* uri, + const SerdNode** prefix, + SerdStringView* suffix) { for (size_t i = 0; i < env->n_prefixes; ++i) { const SerdNode* const prefix_uri = env->prefixes[i].uri; @@ -177,11 +177,30 @@ serd_env_qualify(const SerdEnv* env, return false; } +SerdNode* +serd_env_qualify(const SerdEnv* env, const SerdNode* uri) +{ + const SerdNode* prefix = NULL; + SerdStringView suffix = {NULL, 0}; + if (serd_env_qualify_in_place(env, uri, &prefix, &suffix)) { + const size_t prefix_len = serd_node_length(prefix); + const size_t n_bytes = prefix_len + 1 + suffix.len; + SerdNode* node = serd_node_malloc(n_bytes, 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); + node->n_bytes = n_bytes; + return node; + } + + return NULL; +} + SerdStatus -serd_env_expand(const SerdEnv* env, - const SerdNode* curie, - SerdStringView* uri_prefix, - SerdStringView* uri_suffix) +serd_env_expand_in_place(const SerdEnv* env, + const SerdNode* curie, + SerdStringView* uri_prefix, + SerdStringView* uri_suffix) { const char* const str = serd_node_string(curie); const char* const colon = (const char*)memchr(str, ':', curie->n_bytes + 1); @@ -202,13 +221,17 @@ serd_env_expand(const SerdEnv* env, } SerdNode* -serd_env_expand_node(const SerdEnv* env, const SerdNode* node) +serd_env_expand(const SerdEnv* env, const SerdNode* node) { + if (!node) { + return NULL; + } + switch (node->type) { case SERD_LITERAL: { const SerdNode* const short_datatype = serd_node_datatype(node); if (short_datatype) { - SerdNode* const datatype = serd_env_expand_node(env, 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)); @@ -223,7 +246,7 @@ serd_env_expand_node(const SerdEnv* env, const SerdNode* node) case SERD_CURIE: { SerdStringView prefix; SerdStringView suffix; - if (serd_env_expand(env, node, &prefix, &suffix)) { + if (serd_env_expand_in_place(env, node, &prefix, &suffix)) { return NULL; } diff --git a/src/env.h b/src/env.h index 1c36b4ee..1b129cec 100644 --- a/src/env.h +++ b/src/env.h @@ -19,6 +19,29 @@ #include "serd/serd.h" +#include + +/** + Qualify `uri` into a CURIE if possible. +*/ +bool +serd_env_qualify_in_place(const SerdEnv* env, + const SerdNode* uri, + const SerdNode** prefix, + SerdStringView* suffix); + +/** + Expand `curie`. + + Errors: SERD_ERR_BAD_ARG if `curie` is not valid, or SERD_ERR_BAD_CURIE if + prefix is not defined in `env`. +*/ +SerdStatus +serd_env_expand_in_place(const SerdEnv* env, + const SerdNode* curie, + SerdStringView* uri_prefix, + SerdStringView* uri_suffix); + SERD_CONST_FUNC SerdURIView serd_env_base_uri_view(const SerdEnv* env); diff --git a/src/writer.c b/src/writer.c index d632c7af..be3f2469 100644 --- a/src/writer.c +++ b/src/writer.c @@ -588,7 +588,7 @@ write_uri_node(SerdWriter* const writer, } if (has_scheme && (writer->flags & SERD_WRITE_CURIED) && - serd_env_qualify(writer->env, node, &prefix, &suffix) && + serd_env_qualify_in_place(writer->env, node, &prefix, &suffix) && is_name(suffix.buf, suffix.len)) { write_uri_from_node(writer, prefix); sink(":", 1, writer); @@ -647,7 +647,7 @@ write_curie(SerdWriter* const writer, switch (writer->syntax) { case SERD_NTRIPLES: case SERD_NQUADS: - if ((st = serd_env_expand(writer->env, node, &prefix, &suffix))) { + if ((st = serd_env_expand_in_place(writer->env, node, &prefix, &suffix))) { serd_world_errorf( writer->world, st, "undefined namespace prefix `%s'\n", node_str); return false; -- cgit v1.2.1