From 3742190108d9ed054f3956fc420b3d4161a586e5 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Mon, 28 Jun 2021 19:54:33 -0400 Subject: Reduce complexity of URI node writing functions --- src/writer.c | 59 ++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/writer.c b/src/writer.c index 6dd59c8e..5efcb8c5 100644 --- a/src/writer.c +++ b/src/writer.c @@ -774,6 +774,43 @@ is_name(const char* buf, const size_t len) return true; } +SERD_NODISCARD static SerdStatus +write_full_uri_node(SerdWriter* const writer, const SerdNode* const node) +{ + SerdStatus st = SERD_SUCCESS; + const bool resolve_disabled = writer->flags & SERD_WRITE_UNRESOLVED; + + if (resolve_disabled || !serd_env_base_uri(writer->env)) { + // Resolution disabled or we have no base URI, simply write the node + TRY(st, esink("<", 1, writer)); + TRY(st, write_uri_from_node(writer, node)); + TRY(st, esink(">", 1, writer)); + return SERD_SUCCESS; + } + + // Resolve the input node URI reference to a (hopefully) absolute URI + const SerdURIView base_uri = serd_env_base_uri_view(writer->env); + SerdURIView uri = serd_parse_uri(serd_node_string(node)); + SerdURIView abs_uri = serd_resolve_uri(uri, base_uri); + + // Determine if we should write the absolute URI or make it relative again + const bool base_rooted = uri_is_under(&base_uri, &writer->root_uri); + const SerdURIView* root = base_rooted ? &writer->root_uri : &base_uri; + const bool rooted = uri_is_under(&abs_uri, root); + const bool write_abs = !supports_abbrev(writer) || !rooted; + + TRY(st, esink("<", 1, writer)); + + UriSinkContext context = {writer, SERD_SUCCESS}; + if (write_abs) { + serd_write_uri(abs_uri, uri_sink, &context); + } else { + serd_write_uri(serd_relative_uri(uri, base_uri), uri_sink, &context); + } + + return esink(">", 1, writer); +} + SERD_NODISCARD static SerdStatus write_uri_node(SerdWriter* const writer, const SerdNode* const node, @@ -812,27 +849,7 @@ write_uri_node(SerdWriter* const writer, node_str); } - TRY(st, esink("<", 1, writer)); - - if (!(writer->flags & SERD_WRITE_UNRESOLVED) && - serd_env_base_uri(writer->env)) { - const SerdURIView base_uri = serd_env_base_uri_view(writer->env); - SerdURIView uri = serd_parse_uri(node_str); - SerdURIView abs_uri = serd_resolve_uri(uri, base_uri); - bool rooted = uri_is_under(&base_uri, &writer->root_uri); - const SerdURIView* root = rooted ? &writer->root_uri : &base_uri; - UriSinkContext ctx = {writer, SERD_SUCCESS}; - - if (!supports_abbrev(writer) || !uri_is_under(&abs_uri, root)) { - serd_write_uri(abs_uri, uri_sink, &ctx); - } else { - serd_write_uri(serd_relative_uri(uri, base_uri), uri_sink, &ctx); - } - } else { - TRY(st, write_uri_from_node(writer, node)); - } - - return esink(">", 1, writer); + return write_full_uri_node(writer, node); } SERD_NODISCARD static SerdStatus -- cgit v1.2.1