diff options
-rw-r--r-- | include/serd/serd.h | 10 | ||||
-rw-r--r-- | src/env.c | 42 | ||||
-rw-r--r-- | test/test_env.c | 23 |
3 files changed, 75 insertions, 0 deletions
diff --git a/include/serd/serd.h b/include/serd/serd.h index 4f713a2f..09017175 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -985,6 +985,16 @@ SERD_API SerdEnv* SERD_ALLOCATED serd_env_new(SerdStringView base_uri); +/// Copy an environment +SERD_API +SerdEnv* SERD_ALLOCATED +serd_env_copy(const SerdEnv* SERD_NULLABLE env); + +/// Return true iff `a` is equal to `b` +SERD_PURE_API +bool +serd_env_equals(const SerdEnv* SERD_NULLABLE a, const SerdEnv* SERD_NULLABLE b); + /// Free `env` SERD_API void @@ -48,6 +48,26 @@ serd_env_new(const SerdStringView base_uri) return env; } +SerdEnv* +serd_env_copy(const SerdEnv* env) +{ + if (!env) { + return NULL; + } + + SerdEnv* copy = (SerdEnv*)calloc(1, sizeof(struct SerdEnvImpl)); + + copy->n_prefixes = env->n_prefixes; + copy->prefixes = (SerdPrefix*)malloc(copy->n_prefixes * sizeof(SerdPrefix)); + for (size_t i = 0; i < copy->n_prefixes; ++i) { + copy->prefixes[i].name = serd_node_copy(env->prefixes[i].name); + copy->prefixes[i].uri = serd_node_copy(env->prefixes[i].uri); + } + + serd_env_set_base_uri(copy, serd_node_string_view(serd_env_base_uri(env))); + return copy; +} + void serd_env_free(SerdEnv* env) { @@ -64,6 +84,28 @@ serd_env_free(SerdEnv* env) free(env); } +bool +serd_env_equals(const SerdEnv* a, const SerdEnv* b) +{ + if (!a || !b) { + return !a == !b; + } + + if (a->n_prefixes != b->n_prefixes || + !serd_node_equals(a->base_uri_node, b->base_uri_node)) { + return false; + } + + for (size_t i = 0; i < a->n_prefixes; ++i) { + if (!serd_node_equals(a->prefixes[i].name, b->prefixes[i].name) || + !serd_node_equals(a->prefixes[i].uri, b->prefixes[i].uri)) { + return false; + } + } + + return true; +} + SerdURIView serd_env_base_uri_view(const SerdEnv* env) { diff --git a/test/test_env.c b/test/test_env.c index da53df45..c00ae37b 100644 --- a/test/test_env.c +++ b/test/test_env.c @@ -93,11 +93,34 @@ test_env(void) SerdNode* qualified = serd_env_qualify(env, foo_u); assert(serd_node_equals(qualified, foo_c)); + SerdEnv* env_copy = serd_env_copy(env); + assert(serd_env_equals(env, env_copy)); + assert(!serd_env_equals(env, NULL)); + assert(!serd_env_equals(NULL, env)); + assert(serd_env_equals(NULL, NULL)); + + SerdNode* qualified2 = serd_env_expand(env_copy, foo_u); + assert(serd_node_equals(qualified, foo_c)); + serd_node_free(qualified2); + + serd_env_set_prefix(env_copy, + SERD_STATIC_STRING("test"), + SERD_STATIC_STRING("http://example.org/test")); + + assert(!serd_env_equals(env, env_copy)); + + serd_env_set_prefix(env, + SERD_STATIC_STRING("test2"), + SERD_STATIC_STRING("http://example.org/test")); + + assert(!serd_env_equals(env, env_copy)); + serd_node_free(qualified); serd_sink_free(count_prefixes_sink); serd_node_free(foo_c); serd_node_free(foo_u); serd_node_free(b); + serd_env_free(env_copy); serd_env_free(env); } |