From 1c1c060d7a449952051b2dd78176e48db53df1c4 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 20 Feb 2021 20:10:57 -0500 Subject: WIP: Make serd_env_expand() always return a node if it is not relative The old interface was potentially faster, because it avoided unnecessary copies, but was annoying to use and problematic because nodes that don't need expansion were indistinguishable from nodes that failed to expand (like those that use undefined namespace prefixes). In the grand scheme of things, the potential performance improvement isn't worth it, and unexpected unexpanded nodes causes deep nightmarish problems, so just make serd_env_expand() copy the input if it doesn't need expansion to make the API simpler and safer. --- src/env.c | 31 +++++++++++++++++++++++++------ src/uri.c | 3 --- 2 files changed, 25 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/env.c b/src/env.c index 21dbb45d..55901e26 100644 --- a/src/env.c +++ b/src/env.c @@ -278,15 +278,34 @@ expand_literal(const SerdEnv* env, const SerdNode* node) assert(serd_node_type(node) == SERD_LITERAL); const SerdNode* const datatype = serd_node_datatype(node); - if (datatype && serd_node_type(datatype) == SERD_CURIE) { + if (!datatype) { + return serd_node_copy(node); + } + + if (serd_node_type(datatype) == SERD_CURIE) { SerdStringView prefix; SerdStringView suffix; - if (!serd_env_expand_in_place(env, datatype, &prefix, &suffix)) { - return serd_new_typed_literal_expanded( - serd_node_string_view(node), serd_node_flags(node), SERD_URI, prefix, suffix); + if (serd_env_expand_in_place(env, datatype, &prefix, &suffix)) { + return NULL; + } + + return serd_new_typed_literal_expanded(serd_node_string_view(node), + serd_node_flags(node), + SERD_URI, + prefix, + suffix); + } + + if (serd_node_type(datatype) == SERD_URI) { + const SerdURIView datatype_uri = serd_parse_uri(serd_node_string(datatype)); + + const SerdURIView abs_datatype_uri = + serd_resolve_uri(datatype_uri, env->base_uri); + + if (!abs_datatype_uri.scheme.len) { + return NULL; } - } else if (datatype && serd_node_type(datatype) == SERD_URI) { return serd_new_typed_literal_uri( serd_node_string_view(node), serd_node_flags(node), @@ -336,7 +355,7 @@ serd_env_expand(const SerdEnv* env, const SerdNode* node) case SERD_CURIE: return expand_curie(env, node); case SERD_BLANK: - return NULL; + return serd_node_copy(node); } } diff --git a/src/uri.c b/src/uri.c index 780f7e72..6b029284 100644 --- a/src/uri.c +++ b/src/uri.c @@ -283,9 +283,6 @@ serd_resolve_uri(const SerdURIView r, const SerdURIView base) SerdURIView t = SERD_URI_NULL; - t.path_prefix.buf = NULL; - t.path_prefix.len = 0; - if (r.authority.len) { t.authority = r.authority; t.path = r.path; -- cgit v1.2.1