diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/env.c | 99 | ||||
-rw-r--r-- | src/env.h | 10 | ||||
-rw-r--r-- | src/node.c | 8 | ||||
-rw-r--r-- | src/read_turtle.c | 10 | ||||
-rw-r--r-- | src/statement.c | 3 | ||||
-rw-r--r-- | src/writer.c | 55 |
6 files changed, 58 insertions, 127 deletions
@@ -218,51 +218,29 @@ serd_env_set_prefix(SerdEnv* const env, return SERD_SUCCESS; } -bool -serd_env_qualify_in_place(const SerdEnv* const env, - const SerdNode* const uri, - const SerdNode** const prefix, - SerdStringView* const suffix) +SerdStatus +serd_env_qualify(const SerdEnv* const env, + const SerdStringView uri, + SerdStringView* const prefix, + SerdStringView* const suffix) { 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) { + const SerdNode* const prefix_uri = env->prefixes[i].uri; + const size_t prefix_uri_len = serd_node_length(prefix_uri); + if (uri.data && uri.length >= prefix_uri_len) { const char* prefix_str = serd_node_string(prefix_uri); - const char* uri_str = serd_node_string(uri); + const char* uri_str = uri.data; - if (!strncmp(uri_str, prefix_str, prefix_uri->length)) { - *prefix = env->prefixes[i].name; - suffix->data = uri_str + prefix_uri->length; - suffix->length = uri->length - prefix_uri->length; - return true; + if (!strncmp(uri_str, prefix_str, prefix_uri_len)) { + *prefix = serd_node_string_view(env->prefixes[i].name); + suffix->data = uri_str + prefix_uri_len; + suffix->length = uri.length - prefix_uri_len; + return SERD_SUCCESS; } } } - return false; -} -SerdNode* -serd_env_qualify(const SerdEnv* const env, const SerdNode* const uri) -{ - 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; + return SERD_FAILURE; } SerdStatus @@ -280,14 +258,15 @@ serd_env_expand_in_place(const SerdEnv* const env, const size_t name_len = (size_t)(colon - str); const SerdPrefix* const prefix = serd_env_find(env, str, name_len); - if (prefix) { - uri_prefix->data = serd_node_string(prefix->uri); - uri_prefix->length = prefix->uri ? prefix->uri->length : 0; - uri_suffix->data = colon + 1; - uri_suffix->length = curie.length - name_len - 1; - return SERD_SUCCESS; + if (!prefix || !prefix->uri) { + return SERD_BAD_CURIE; } - return SERD_BAD_CURIE; + + uri_prefix->data = serd_node_string(prefix->uri); + uri_prefix->length = prefix->uri ? prefix->uri->length : 0; + uri_suffix->data = colon + 1; + uri_suffix->length = curie.length - name_len - 1; + return SERD_SUCCESS; } static SerdNode* @@ -298,29 +277,33 @@ expand_uri(const SerdEnv* env, const SerdNode* node) return serd_new_resolved_uri(serd_node_string_view(node), env->base_uri); } -static SerdNode* -expand_curie(const SerdEnv* env, const SerdNode* node) +SerdNode* +serd_env_expand_curie(const SerdEnv* const env, const SerdStringView curie) { - assert(serd_node_type(node) == SERD_CURIE); + if (!env) { + return NULL; + } - SerdStringView prefix; - SerdStringView suffix; - if (serd_env_expand_in_place( - env, serd_node_string_view(node), &prefix, &suffix)) { + SerdStringView prefix = serd_empty_string(); + SerdStringView suffix = serd_empty_string(); + SerdStatus st = serd_env_expand_in_place(env, curie, &prefix, &suffix); + if (st || !prefix.data || !suffix.data) { return NULL; } - const size_t len = prefix.length + suffix.length; - SerdNode* ret = serd_node_malloc(len, 0, SERD_URI); - char* buf = serd_node_buffer(ret); + const size_t len = prefix.length + suffix.length; + SerdNode* const ret = serd_node_malloc(len, 0U, SERD_URI); + if (ret) { + char* const string = serd_node_buffer(ret); + memcpy(string, prefix.data, prefix.length); + memcpy(string + prefix.length, suffix.data, suffix.length); + } - snprintf(buf, len + 1, "%s%s", prefix.data, suffix.data); - ret->length = len; return ret; } SerdNode* -serd_env_expand(const SerdEnv* env, const SerdNode* node) +serd_env_expand_node(const SerdEnv* const env, const SerdNode* const node) { if (!env || !node) { return NULL; @@ -331,8 +314,6 @@ serd_env_expand(const SerdEnv* env, const SerdNode* node) break; case SERD_URI: return expand_uri(env, node); - case SERD_CURIE: - return expand_curie(env, node); case SERD_BLANK: case SERD_VARIABLE: break; @@ -5,21 +5,11 @@ #define SERD_SRC_ENV_H #include "serd/env.h" -#include "serd/node.h" #include "serd/status.h" #include "serd/string_view.h" #include "serd/uri.h" #include "zix/attributes.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); - /** Return the URI for the prefix with the given name. @@ -291,12 +291,6 @@ serd_new_blank(const SerdStringView str) return serd_new_token(SERD_BLANK, str); } -SerdNode* -serd_new_curie(const SerdStringView str) -{ - return serd_new_token(SERD_CURIE, str); -} - ExessResult serd_node_get_value_as(const SerdNode* const node, const ExessDatatype value_type, @@ -738,7 +732,7 @@ serd_node_datatype(const SerdNode* const node) } const SerdNode* const datatype = serd_node_meta_c(node); - assert(datatype->type == SERD_URI || datatype->type == SERD_CURIE); + assert(datatype->type == SERD_URI); return datatype; } diff --git a/src/read_turtle.c b/src/read_turtle.c index fa7b9731..042393e0 100644 --- a/src/read_turtle.c +++ b/src/read_turtle.c @@ -513,7 +513,7 @@ read_turtle_iri(SerdReader* const reader, return read_IRIREF(reader, dest); } - if (!(*dest = push_node(reader, SERD_CURIE, "", 0))) { + if (!(*dest = push_node(reader, SERD_LITERAL, "", 0))) { return SERD_BAD_STACK; } @@ -567,7 +567,7 @@ read_verb(SerdReader* reader, SerdNode** const dest) /* Either a qname, or "a". Read the prefix first, and if it is in fact "a", produce that instead. */ - if (!(*dest = push_node(reader, SERD_CURIE, "", 0))) { + if (!(*dest = push_node(reader, SERD_URI, "", 0))) { return SERD_BAD_STACK; } @@ -671,7 +671,7 @@ read_named_object(SerdReader* const reader, Deal with this here by trying to read a prefixed node, then if it turns out to actually be "true" or "false", switch it to a boolean literal. */ - if (!(*dest = push_node(reader, SERD_CURIE, "", 0))) { + if (!(*dest = push_node(reader, SERD_URI, "", 0))) { return SERD_BAD_STACK; } @@ -935,7 +935,9 @@ read_turtle_subject(SerdReader* const reader, st = read_BLANK_NODE_LABEL(reader, dest, &ate_dot); break; default: - TRY(st, read_turtle_iri(reader, dest, &ate_dot)); + if ((st = read_turtle_iri(reader, dest, &ate_dot))) { + return r_err(reader, st, "expected subject"); + } } if (ate_dot) { diff --git a/src/statement.c b/src/statement.c index 9d2d4229..418e999e 100644 --- a/src/statement.c +++ b/src/statement.c @@ -17,8 +17,7 @@ static bool is_resource(const SerdNode* const node) { const SerdNodeType type = node ? serd_node_type(node) : (SerdNodeType)0; - return type == SERD_URI || type == SERD_CURIE || type == SERD_BLANK || - type == SERD_VARIABLE; + return type == SERD_URI || type == SERD_BLANK || type == SERD_VARIABLE; } bool diff --git a/src/writer.c b/src/writer.c index 05be0500..31daa71c 100644 --- a/src/writer.c +++ b/src/writer.c @@ -840,24 +840,23 @@ write_uri_node(SerdWriter* const writer, const SerdNode* const node, const SerdField field) { - SerdStatus st = SERD_SUCCESS; - const char* const node_str = serd_node_string(node); - const bool has_scheme = serd_uri_string_has_scheme(node_str); + SerdStatus st = SERD_SUCCESS; + const SerdStringView string = serd_node_string_view(node); + const bool has_scheme = serd_uri_string_has_scheme(string.data); if (supports_abbrev(writer)) { - const SerdNode* prefix_node = NULL; - SerdStringView suffix = {NULL, 0}; - if (field == SERD_PREDICATE && !strcmp(node_str, NS_RDF "type")) { + if (field == SERD_PREDICATE && !strcmp(string.data, NS_RDF "type")) { return esink("a", 1, writer); } - if (!strcmp(node_str, NS_RDF "nil")) { + if (!strcmp(string.data, NS_RDF "nil")) { return esink("()", 2, writer); } + SerdStringView prefix = {NULL, 0}; + SerdStringView suffix = {NULL, 0}; if (has_scheme && !(writer->flags & SERD_WRITE_UNQUALIFIED) && - serd_env_qualify_in_place(writer->env, node, &prefix_node, &suffix)) { - const SerdStringView prefix = serd_node_string_view(prefix_node); + !serd_env_qualify(writer->env, string, &prefix, &suffix)) { TRY(st, write_lname(writer, prefix.data, prefix.length)); TRY(st, esink(":", 1, writer)); return write_lname(writer, suffix.data, suffix.length); @@ -868,45 +867,14 @@ write_uri_node(SerdWriter* const writer, !serd_env_base_uri(writer->env)) { return w_err(writer, SERD_BAD_ARG, - "URI reference <%s> in unsupported syntax", - node_str); + "syntax does not support URI reference <%s>", + string.data); } return write_full_uri_node(writer, node); } SERD_NODISCARD static SerdStatus -write_curie(SerdWriter* const writer, const SerdNode* const node) -{ - const char* const node_str = serd_node_string(node); - SerdStringView prefix = {NULL, 0}; - SerdStringView suffix = {NULL, 0}; - SerdStatus st = SERD_SUCCESS; - - // In fast-and-loose Turtle/TriG mode CURIEs are simply passed through - const bool fast = - (writer->flags & (SERD_WRITE_UNQUALIFIED | SERD_WRITE_UNRESOLVED)); - - if (!supports_abbrev(writer) || !fast) { - const SerdStringView curie = serd_node_string_view(node); - if ((st = serd_env_expand_in_place(writer->env, curie, &prefix, &suffix))) { - return w_err(writer, st, "undefined namespace prefix '%s'", node_str); - } - } - - if (!supports_abbrev(writer)) { - TRY(st, esink("<", 1, writer)); - TRY(st, ewrite_uri(writer, prefix.data, prefix.length)); - TRY(st, ewrite_uri(writer, suffix.data, suffix.length)); - TRY(st, esink(">", 1, writer)); - } else { - TRY(st, write_lname(writer, node_str, node->length)); - } - - return st; -} - -SERD_NODISCARD static SerdStatus write_blank(SerdWriter* const writer, const SerdNode* node, const SerdField field, @@ -974,9 +942,6 @@ write_node(SerdWriter* const writer, case SERD_URI: st = write_uri_node(writer, node, field); break; - case SERD_CURIE: - st = write_curie(writer, node); - break; case SERD_BLANK: st = write_blank(writer, node, field, flags); break; |