diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/env.c | 67 | ||||
-rw-r--r-- | src/env.h | 7 | ||||
-rw-r--r-- | src/n3.c | 13 | ||||
-rw-r--r-- | src/node.c | 11 | ||||
-rw-r--r-- | src/nodes.c | 6 | ||||
-rw-r--r-- | src/statement.c | 3 | ||||
-rw-r--r-- | src/writer.c | 52 |
7 files changed, 43 insertions, 116 deletions
@@ -208,51 +208,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.len >= 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.buf; - if (!strncmp(uri_str, prefix_str, prefix_uri->length)) { - *prefix = env->prefixes[i].name; - suffix->buf = uri_str + prefix_uri->length; - suffix->len = 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->buf = uri_str + prefix_uri_len; + suffix->len = uri.len - 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.len; - 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.buf, suffix.len); - node->length = length; - return node; - } - - return NULL; + return SERD_FAILURE; } SerdStatus @@ -288,15 +266,16 @@ 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)) { + if (serd_env_expand_in_place(env, curie, &prefix, &suffix)) { return NULL; } @@ -310,7 +289,7 @@ expand_curie(const SerdEnv* env, const SerdNode* node) } 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; @@ -321,8 +300,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; @@ -21,13 +21,6 @@ #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`. @@ -886,8 +886,7 @@ read_PrefixedName(SerdReader* const reader, } if ((st = push_byte(reader, dest, eat_byte_safe(reader, ':'))) || - (st = read_PN_LOCAL(reader, dest, ate_dot)) > SERD_FAILURE || - (reader->flags & SERD_READ_PREFIXED)) { + (st = read_PN_LOCAL(reader, dest, ate_dot)) > SERD_FAILURE) { return st; } @@ -1012,7 +1011,7 @@ read_iri(SerdReader* const reader, SerdNode** const dest, bool* const ate_dot) return read_IRIREF(reader, dest); } - if (!(*dest = push_node(reader, SERD_CURIE, "", 0))) { + if (!(*dest = push_node(reader, SERD_LITERAL, "", 0))) { return SERD_ERR_OVERFLOW; } @@ -1104,7 +1103,7 @@ read_verb(SerdReader* reader, SerdNode** 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_ERR_OVERFLOW; } @@ -1328,7 +1327,7 @@ read_object(SerdReader* const reader, /* Either a boolean literal, or a qname. Read the prefix first, and if it is in fact a "true" or "false" literal, produce that instead. */ - if (!(o = push_node(reader, SERD_CURIE, "", 0))) { + if (!(o = push_node(reader, SERD_URI, "", 0))) { return SERD_ERR_OVERFLOW; } @@ -1533,7 +1532,9 @@ read_subject(SerdReader* const reader, st = read_BLANK_NODE_LABEL(reader, dest, &ate_dot); break; default: - st = read_iri(reader, dest, &ate_dot); + if ((st = read_iri(reader, dest, &ate_dot))) { + return r_err(reader, st, "expected subject"); + } } if (ate_dot) { @@ -170,8 +170,7 @@ result(const SerdStatus status, const size_t count) SerdNode* serd_new_simple_node(const SerdNodeType type, const SerdStringView str) { - if (type != SERD_BLANK && type != SERD_CURIE && type != SERD_URI && - type != SERD_VARIABLE) { + if (type != SERD_BLANK && type != SERD_URI && type != SERD_VARIABLE) { return NULL; } @@ -287,12 +286,6 @@ serd_new_blank(const SerdStringView str) return serd_new_simple_node(SERD_BLANK, str); } -SerdNode* -serd_new_curie(const SerdStringView str) -{ - return serd_new_simple_node(SERD_CURIE, str); -} - ExessResult serd_node_get_value_as(const SerdNode* const node, const ExessDatatype value_type, @@ -748,7 +741,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/nodes.c b/src/nodes.c index e6476f81..412d0d24 100644 --- a/src/nodes.c +++ b/src/nodes.c @@ -179,12 +179,6 @@ serd_nodes_uri(SerdNodes* const nodes, const SerdStringView string) } const SerdNode* -serd_nodes_curie(SerdNodes* const nodes, const SerdStringView string) -{ - return serd_nodes_manage(nodes, serd_new_curie(string)); -} - -const SerdNode* serd_nodes_blank(SerdNodes* const nodes, const SerdStringView string) { return serd_nodes_manage(nodes, serd_new_blank(string)); diff --git a/src/statement.c b/src/statement.c index 0ec6520a..f3034566 100644 --- a/src/statement.c +++ b/src/statement.c @@ -28,8 +28,7 @@ is_resource(const SerdNode* const node) { const SerdNodeType type = serd_node_type(node); - 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 c86b066d..bfce4f9e 100644 --- a/src/writer.c +++ b/src/writer.c @@ -761,7 +761,7 @@ is_name(const char* buf, const size_t len) { // TODO: This is more strict than it should be for (size_t i = 0; i < len; ++i) { - if (!(is_alpha(buf[i]) || is_digit(buf[i]))) { + if (!(is_alpha(buf[i]) || is_digit(buf[i]) || lname_must_escape(buf[i]))) { return false; } } @@ -811,11 +811,11 @@ write_uri_node(SerdWriter* const writer, const SerdNode* const node, const SerdField field) { - SerdStatus st = SERD_SUCCESS; - const SerdNode* prefix = NULL; - SerdStringView suffix = {NULL, 0}; - const char* node_str = serd_node_string(node); - const bool has_scheme = serd_uri_string_has_scheme(node_str); + SerdStatus st = SERD_SUCCESS; + SerdStringView prefix = {NULL, 0}; + SerdStringView suffix = {NULL, 0}; + const char* node_str = serd_node_string(node); + const bool has_scheme = serd_uri_string_has_scheme(node_str); if (supports_abbrev(writer)) { if (field == SERD_PREDICATE && serd_node_equals(node, writer->world->rdf_type)) { @@ -827,12 +827,12 @@ write_uri_node(SerdWriter* const writer, } if (has_scheme && !(writer->flags & SERD_WRITE_UNQUALIFIED) && - serd_env_qualify_in_place(writer->env, node, &prefix, &suffix) && - is_name(serd_node_string(prefix), serd_node_length(prefix)) && - is_name(suffix.buf, suffix.len)) { - TRY(st, write_uri_from_node(writer, prefix)); + !serd_env_qualify( + writer->env, serd_node_string_view(node), &prefix, &suffix) && + is_name(prefix.buf, prefix.len) && is_name(suffix.buf, suffix.len)) { + TRY(st, write_lname(writer, prefix.buf, prefix.len)); TRY(st, esink(":", 1, writer)); - return ewrite_uri(writer, suffix.buf, suffix.len); + return write_lname(writer, suffix.buf, suffix.len); } } @@ -848,33 +848,6 @@ write_uri_node(SerdWriter* const writer, } SERD_WARN_UNUSED_RESULT static SerdStatus -write_curie(SerdWriter* const writer, const SerdNode* const node) -{ - writer->last_sep = SEP_NONE; - - SerdStringView prefix = {NULL, 0}; - SerdStringView suffix = {NULL, 0}; - SerdStatus st = SERD_SUCCESS; - - if (writer->syntax == SERD_NTRIPLES || writer->syntax == SERD_NQUADS) { - 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 in `%s'", - serd_node_string(node)); - } - - TRY(st, esink("<", 1, writer)); - TRY(st, ewrite_uri(writer, prefix.buf, prefix.len)); - TRY(st, ewrite_uri(writer, suffix.buf, suffix.len)); - return esink(">", 1, writer); - } - - return write_lname(writer, serd_node_string(node), node->length); -} - -SERD_WARN_UNUSED_RESULT static SerdStatus write_blank(SerdWriter* const writer, const SerdNode* node, const SerdField field, @@ -940,9 +913,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; |