aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-06-13 21:32:11 -0400
committerDavid Robillard <d@drobilla.net>2018-11-25 22:12:47 +0100
commitcdbd9de9d8d336b0c5f867b3d6e99788518ad7e8 (patch)
tree7e2dd7650dd6c0b01f63481d5e637e1d37d6609a
parentafab9f8037f9281bcef6ee4201061aa234eeadac (diff)
downloadserd-cdbd9de9d8d336b0c5f867b3d6e99788518ad7e8.tar.gz
serd-cdbd9de9d8d336b0c5f867b3d6e99788518ad7e8.tar.bz2
serd-cdbd9de9d8d336b0c5f867b3d6e99788518ad7e8.zip
Simplify SerdEnv API
-rw-r--r--serd/serd.h30
-rw-r--r--src/env.c78
-rw-r--r--src/env.h26
-rw-r--r--src/writer.c11
-rw-r--r--tests/serd_test.c52
5 files changed, 117 insertions, 80 deletions
diff --git a/serd/serd.h b/serd/serd.h
index d379ccee..2438cabd 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -882,17 +882,14 @@ serd_env_get_base_uri(const SerdEnv* env);
*/
SERD_API
SerdStatus
-serd_env_set_base_uri(SerdEnv* env,
- const SerdNode* uri);
+serd_env_set_base_uri(SerdEnv* env, const SerdNode* uri);
/**
Set a namespace prefix.
*/
SERD_API
SerdStatus
-serd_env_set_prefix(SerdEnv* env,
- const SerdNode* name,
- const SerdNode* uri);
+serd_env_set_prefix(SerdEnv* env, const SerdNode* name, const SerdNode* uri);
/**
Set a namespace prefix.
@@ -905,26 +902,12 @@ serd_env_set_prefix_from_strings(SerdEnv* env,
/**
Qualify `uri` into a CURIE if possible.
-*/
-SERD_API
-bool
-serd_env_qualify(const SerdEnv* env,
- const SerdNode* uri,
- const SerdNode** prefix,
- SerdSlice* suffix);
-/**
- Expand `curie`.
-
- Errors: SERD_ERR_BAD_ARG if `curie` is not valid, or SERD_ERR_BAD_CURIE if
- prefix is not defined in `env`.
+ Returns null if `node` can not be qualified.
*/
SERD_API
-SerdStatus
-serd_env_expand(const SerdEnv* env,
- const SerdNode* curie,
- SerdSlice* uri_prefix,
- SerdSlice* uri_suffix);
+SerdNode*
+serd_env_qualify(const SerdEnv* env, const SerdNode* uri);
/**
Expand `node`, which must be a CURIE or URI, to a full URI.
@@ -933,8 +916,7 @@ serd_env_expand(const SerdEnv* env,
*/
SERD_API
SerdNode*
-serd_env_expand_node(const SerdEnv* env,
- const SerdNode* node);
+serd_env_expand(const SerdEnv* env, const SerdNode* node);
/**
Call `func` for each prefix defined in `env`.
diff --git a/src/env.c b/src/env.c
index 262a1ba7..5f71e266 100644
--- a/src/env.c
+++ b/src/env.c
@@ -155,26 +155,11 @@ serd_env_set_prefix(SerdEnv* env,
return SERD_SUCCESS;
}
-SerdStatus
-serd_env_set_prefix_from_strings(SerdEnv* env,
- const char* name,
- const char* uri)
-{
- SerdNode* name_node = serd_new_string(name);
- SerdNode* uri_node = serd_new_uri(uri);
-
- const SerdStatus st = serd_env_set_prefix(env, name_node, uri_node);
-
- serd_node_free(name_node);
- serd_node_free(uri_node);
- return st;
-}
-
bool
-serd_env_qualify(const SerdEnv* env,
- const SerdNode* uri,
- const SerdNode** prefix,
- SerdSlice* suffix)
+serd_env_qualify_in_place(const SerdEnv* env,
+ const SerdNode* uri,
+ const SerdNode** prefix,
+ SerdSlice* suffix)
{
for (size_t i = 0; i < env->n_prefixes; ++i) {
const SerdNode* const prefix_uri = env->prefixes[i].uri;
@@ -194,10 +179,46 @@ serd_env_qualify(const SerdEnv* env,
}
SerdStatus
-serd_env_expand(const SerdEnv* env,
- const SerdNode* curie,
- SerdSlice* uri_prefix,
- SerdSlice* uri_suffix)
+serd_env_set_prefix_from_strings(SerdEnv* env,
+ const char* name,
+ const char* uri)
+{
+ SerdNode* name_node = serd_new_string(name);
+ SerdNode* uri_node = serd_new_uri(uri);
+
+ const SerdStatus st = serd_env_set_prefix(env, name_node, uri_node);
+
+ serd_node_free(name_node);
+ serd_node_free(uri_node);
+ return st;
+}
+
+SerdNode*
+serd_env_qualify(const SerdEnv* env, const SerdNode* uri)
+{
+ const SerdNode* prefix = NULL;
+ SerdSlice suffix = { NULL, 0 };
+ if (serd_env_qualify_in_place(env, uri, &prefix, &suffix)) {
+ const size_t prefix_len = serd_node_get_length(prefix);
+ const size_t n_bytes = prefix_len + 1 + suffix.len;
+ SerdNode* node = serd_node_malloc(n_bytes, 0, SERD_CURIE);
+ memcpy(serd_node_buffer(node),
+ serd_node_get_string(prefix),
+ prefix_len);
+ serd_node_buffer(node)[prefix_len] = ':';
+ memcpy(serd_node_buffer(node) + 1 + prefix_len, suffix.buf, suffix.len);
+ node->n_bytes = n_bytes;
+ return node;
+ }
+
+ return NULL;
+}
+
+SerdStatus
+serd_env_expand_in_place(const SerdEnv* env,
+ const SerdNode* curie,
+ SerdSlice* uri_prefix,
+ SerdSlice* uri_suffix)
{
const char* const str = serd_node_get_string(curie);
const char* const colon = (const char*)memchr(str, ':', curie->n_bytes + 1);
@@ -218,14 +239,17 @@ serd_env_expand(const SerdEnv* env,
}
SerdNode*
-serd_env_expand_node(const SerdEnv* env,
- const SerdNode* node)
+serd_env_expand(const SerdEnv* env, const SerdNode* node)
{
+ if (!node) {
+ return NULL;
+ }
+
switch (node->type) {
case SERD_CURIE: {
SerdSlice prefix;
SerdSlice suffix;
- if (serd_env_expand(env, node, &prefix, &suffix)) {
+ if (serd_env_expand_in_place(env, node, &prefix, &suffix)) {
return NULL;
}
const size_t len = prefix.len + suffix.len;
@@ -237,7 +261,7 @@ serd_env_expand_node(const SerdEnv* env,
}
case SERD_LITERAL:
if (serd_node_get_datatype(node)) {
- SerdNode* datatype = serd_env_expand_node(
+ SerdNode* datatype = serd_env_expand(
env, serd_node_get_datatype(node));
if (datatype) {
SerdNode* ret = serd_new_typed_literal(
diff --git a/src/env.h b/src/env.h
index ecb6cc15..0b15fbc2 100644
--- a/src/env.h
+++ b/src/env.h
@@ -19,6 +19,28 @@
#include "serd/serd.h"
-const SerdURI* serd_env_get_parsed_base_uri(const SerdEnv* env);
+/**
+ Qualify `uri` into a CURIE if possible.
+*/
+bool
+serd_env_qualify_in_place(const SerdEnv* env,
+ const SerdNode* uri,
+ const SerdNode** prefix,
+ SerdSlice* suffix);
+
+/**
+ Expand `curie`.
+
+ Errors: SERD_ERR_BAD_ARG if `curie` is not valid, or SERD_ERR_BAD_CURIE if
+ prefix is not defined in `env`.
+*/
+SerdStatus
+serd_env_expand_in_place(const SerdEnv* env,
+ const SerdNode* curie,
+ SerdSlice* uri_prefix,
+ SerdSlice* uri_suffix);
+
+const SerdURI*
+serd_env_get_parsed_base_uri(const SerdEnv* env);
-#endif // SERD_ENV_H
+#endif // SERD_ENV_H
diff --git a/src/writer.c b/src/writer.c
index cbd3baa6..64be7e11 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -511,7 +511,7 @@ write_uri_node(SerdWriter* const writer,
} else if (supports_abbrev(writer) && !strcmp(node_str, NS_RDF "nil")) {
return sink("()", 2, writer) == 2;
} else if (has_scheme && (writer->style & SERD_STYLE_CURIED) &&
- serd_env_qualify(writer->env, node, &prefix, &suffix) &&
+ serd_env_qualify_in_place(writer->env, node, &prefix, &suffix) &&
is_name(suffix.buf, suffix.len)) {
write_uri_from_node(writer, prefix);
sink(":", 1, writer);
@@ -559,9 +559,12 @@ write_curie(SerdWriter* const writer,
switch (writer->syntax) {
case SERD_NTRIPLES:
case SERD_NQUADS:
- if ((st = serd_env_expand(writer->env, node, &prefix, &suffix))) {
- serd_world_errorf(writer->world, st,
- "undefined namespace prefix `%s'\n", node_str);
+ if ((st = serd_env_expand_in_place(
+ writer->env, node, &prefix, &suffix))) {
+ serd_world_errorf(writer->world,
+ st,
+ "undefined namespace prefix `%s'\n",
+ node_str);
return false;
}
write_sep(writer, SEP_URI_BEGIN);
diff --git a/tests/serd_test.c b/tests/serd_test.c
index fea37af4..c68d2813 100644
--- a/tests/serd_test.c
+++ b/tests/serd_test.c
@@ -474,11 +474,13 @@ main(void)
SerdWorld* world = serd_world_new();
- SerdNode* u = serd_new_uri("http://example.org/foo");
- SerdNode* b = serd_new_curie("invalid");
- SerdNode* c = serd_new_curie("eg.2:b");
- SerdEnv* env = serd_env_new(NULL);
- serd_env_set_prefix_from_strings(env, "eg.2", "http://example.org/");
+ SerdNode* eg = serd_new_uri("http://example.org/");
+ SerdNode* foo_u = serd_new_uri("http://example.org/foo");
+ SerdNode* foo_c = serd_new_curie("eg.2:foo");
+ SerdNode* b = serd_new_curie("invalid");
+ SerdNode* pre = serd_new_curie("eg.2");
+ SerdEnv* env = serd_env_new(NULL);
+ serd_env_set_prefix(env, pre, eg);
if (serd_env_get_base_uri(env)) {
FAIL("Unexpected initial base URI\n");
@@ -496,35 +498,30 @@ main(void)
FAIL("Unexpected base URI\n");
}
- SerdSlice prefix, suffix;
- if (!serd_env_expand(env, b, &prefix, &suffix)) {
- FAILF("Expanded invalid curie %s\n", serd_node_get_string(b));
- }
-
- SerdNode* xnode = serd_env_expand_node(env, hello);
+ SerdNode* xnode = serd_env_expand(env, hello);
if (xnode) {
- FAILF("Expanded %s\n", serd_node_get_string(c));
+ FAILF("Expanded %s\n", serd_node_get_string(hello));
}
serd_node_free(hello);
- SerdNode* xu = serd_env_expand_node(env, u);
+ SerdNode* xu = serd_env_expand(env, foo_c);
if (strcmp(serd_node_get_string(xu), "http://example.org/foo")) {
FAILF("Expanded %s to %s\n",
- serd_node_get_string(c), serd_node_get_string(xu));
+ serd_node_get_string(xu), serd_node_get_string(xu));
}
serd_node_free(xu);
SerdNode* badpre = serd_new_curie("hm:what");
- SerdNode* xbadpre = serd_env_expand_node(env, badpre);
+ SerdNode* xbadpre = serd_env_expand(env, badpre);
if (xbadpre) {
FAILF("Expanded invalid curie %s\n", serd_node_get_string(badpre));
}
serd_node_free(badpre);
- SerdNode* xc = serd_env_expand_node(env, c);
- if (strcmp(serd_node_get_string(xc), "http://example.org/b")) {
+ SerdNode* xc = serd_env_expand(env, foo_c);
+ if (!serd_node_equals(xc, foo_u)) {
FAILF("Expanded %s to %s\n",
- serd_node_get_string(c), serd_node_get_string(xc));
+ serd_node_get_string(foo_c), serd_node_get_string(xc));
}
serd_node_free(xc);
@@ -538,21 +535,30 @@ main(void)
}
int n_prefixes = 0;
- serd_env_set_prefix_from_strings(env, "eg.2", "http://example.org/");
+ serd_env_set_prefix(env, pre, eg);
serd_env_foreach(env, count_prefixes, &n_prefixes);
if (n_prefixes != 1) {
FAILF("Bad prefix count %d\n", n_prefixes);
}
SerdNode* shorter_uri = serd_new_uri("urn:foo");
- const SerdNode* prefix_name;
- if (serd_env_qualify(env, shorter_uri, &prefix_name, &suffix)) {
+ if (serd_env_qualify(env, shorter_uri)) {
FAILF("Qualified %s\n", serd_node_get_string(shorter_uri));
}
serd_node_free(shorter_uri);
- serd_node_free(u);
+
+ SerdNode* qualified = serd_env_qualify(env, foo_u);
+ if (!serd_node_equals(qualified, foo_c)) {
+ FAILF("Qualified %s to %s\n",
+ serd_node_get_string(foo_u),
+ serd_node_get_string(qualified));
+ }
+ serd_node_free(qualified);
+ serd_node_free(foo_c);
+ serd_node_free(foo_u);
serd_node_free(b);
- serd_node_free(c);
+ serd_node_free(pre);
+ serd_node_free(eg);
// Test SerdReader and SerdWriter