diff options
Diffstat (limited to 'src/env.c')
-rw-r--r-- | src/env.c | 99 |
1 files changed, 40 insertions, 59 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; |