diff options
author | David Robillard <d@drobilla.net> | 2022-07-15 11:25:21 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:07 -0500 |
commit | 1ce0154639ea329d0a085713e8e21b85aa8526d0 (patch) | |
tree | badc24e17cb45132edec317b84f9afdf385ed107 /src | |
parent | b7cce4a24dbc544129a9fabc44cb22025767f10b (diff) | |
download | serd-1ce0154639ea329d0a085713e8e21b85aa8526d0.tar.gz serd-1ce0154639ea329d0a085713e8e21b85aa8526d0.tar.bz2 serd-1ce0154639ea329d0a085713e8e21b85aa8526d0.zip |
Simplify SerdEnv API
Diffstat (limited to 'src')
-rw-r--r-- | src/env.c | 51 | ||||
-rw-r--r-- | src/env.h | 24 | ||||
-rw-r--r-- | src/writer.c | 4 |
3 files changed, 59 insertions, 20 deletions
@@ -151,15 +151,11 @@ serd_env_set_prefix(SerdEnv* const env, } bool -serd_env_qualify(const SerdEnv* const env, - const SerdNode* const uri, - const SerdNode** const prefix, - SerdStringView* const suffix) +serd_env_qualify_in_place(const SerdEnv* const env, + const SerdNode* const uri, + const SerdNode** const prefix, + SerdStringView* const suffix) { - if (!env) { - return false; - } - for (size_t i = 0; i < env->n_prefixes; ++i) { const SerdNode* const prefix_uri = env->prefixes[i].uri; if (uri->length >= prefix_uri->length) { @@ -177,16 +173,35 @@ serd_env_qualify(const SerdEnv* const env, return false; } -SerdStatus -serd_env_expand(const SerdEnv* const env, - const SerdNode* const curie, - SerdStringView* const uri_prefix, - SerdStringView* const uri_suffix) +SerdNode* +serd_env_qualify(const SerdEnv* const env, const SerdNode* const uri) { - if (!env || !curie) { - return SERD_BAD_CURIE; + if (!env || !uri) { + return NULL; + } + + 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 length = prefix_len + 1 + suffix.length; + 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.data, suffix.length); + node->length = length; + return node; } + return NULL; +} + +SerdStatus +serd_env_expand_in_place(const SerdEnv* const env, + const SerdNode* const curie, + SerdStringView* const uri_prefix, + SerdStringView* const uri_suffix) +{ const char* const str = serd_node_string(curie); const char* const colon = (const char*)memchr(str, ':', curie->length + 1); if (curie->type != SERD_CURIE || !colon) { @@ -206,9 +221,9 @@ serd_env_expand(const SerdEnv* const env, } SerdNode* -serd_env_expand_node(const SerdEnv* const env, const SerdNode* const node) +serd_env_expand(const SerdEnv* const env, const SerdNode* const node) { - if (!env) { + if (!env || !node) { return NULL; } @@ -220,7 +235,7 @@ serd_env_expand_node(const SerdEnv* const env, const SerdNode* const 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; } @@ -6,8 +6,32 @@ #include "serd/attributes.h" #include "serd/env.h" +#include "serd/node.h" +#include "serd/status.h" +#include "serd/string_view.h" #include "serd/uri.h" +#include <stdbool.h> + +/// 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_BAD_ARG if `curie` is not valid, or SERD_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_PURE_FUNC SerdURIView serd_env_base_uri_view(const SerdEnv* env); diff --git a/src/writer.c b/src/writer.c index 0ba90c0b..42a6ded0 100644 --- a/src/writer.c +++ b/src/writer.c @@ -852,7 +852,7 @@ write_uri_node(SerdWriter* const writer, } if (has_scheme && !(writer->flags & SERD_WRITE_UNQUALIFIED) && - serd_env_qualify(writer->env, node, &prefix, &suffix) && + serd_env_qualify_in_place(writer->env, node, &prefix, &suffix) && is_name(serd_node_string(prefix), serd_node_length(prefix)) && is_name(suffix.data, suffix.length)) { TRY(st, write_uri_from_node(writer, prefix)); @@ -885,7 +885,7 @@ write_curie(SerdWriter* const writer, const SerdNode* const node) (writer->flags & (SERD_WRITE_UNQUALIFIED | SERD_WRITE_UNRESOLVED)); if (!supports_abbrev(writer) || !fast) { - if ((st = serd_env_expand(writer->env, node, &prefix, &suffix))) { + if ((st = serd_env_expand_in_place(writer->env, node, &prefix, &suffix))) { return w_err(writer, st, "undefined namespace prefix '%s'\n", node_str); } } |