diff options
-rw-r--r-- | src/env.c | 38 | ||||
-rw-r--r-- | src/reader.c | 7 | ||||
-rw-r--r-- | src/serd_internal.h | 43 | ||||
-rw-r--r-- | src/serdi.c | 18 | ||||
-rw-r--r-- | src/string.c | 91 | ||||
-rw-r--r-- | wscript | 1 |
6 files changed, 55 insertions, 143 deletions
@@ -23,8 +23,8 @@ #include "serd_internal.h" typedef struct { - SerdString* name; - SerdString* uri; + SerdNode name; + SerdNode uri; } SerdPrefix; struct SerdEnvImpl { @@ -32,6 +32,22 @@ struct SerdEnvImpl { size_t n_prefixes; }; +static SerdNode +serd_node_copy(const SerdNode* node) +{ + SerdNode copy = *node; + uint8_t* buf = malloc(copy.n_bytes); + memcpy(buf, node->buf, copy.n_bytes); + copy.buf = buf; + return copy; +} + +static void +serd_node_free(SerdNode* node) +{ + free((uint8_t*)node->buf); // FIXME: const cast +} + SERD_API SerdEnv serd_env_new() @@ -47,8 +63,8 @@ void serd_env_free(SerdEnv env) { for (size_t i = 0; i < env->n_prefixes; ++i) { - serd_string_free(env->prefixes[i].name); - serd_string_free(env->prefixes[i].uri); + serd_node_free(&env->prefixes[i].name); + serd_node_free(&env->prefixes[i].uri); } free(env->prefixes); free(env); @@ -60,7 +76,7 @@ serd_env_find(SerdEnv env, size_t name_len) { for (size_t i = 0; i < env->n_prefixes; ++i) { - const SerdString* prefix_name = env->prefixes[i].name; + const SerdNode* const prefix_name = &env->prefixes[i].name; if (prefix_name->n_bytes == name_len + 1) { if (!memcmp(prefix_name->buf, name, name_len)) { return &env->prefixes[i]; @@ -79,13 +95,13 @@ serd_env_add(SerdEnv env, assert(name && uri); SerdPrefix* const prefix = serd_env_find(env, name->buf, name->n_chars); if (prefix) { - serd_string_free(prefix->uri); - prefix->uri = serd_string_new_from_node(uri); + serd_node_free(&prefix->uri); + prefix->uri = serd_node_copy(uri); } else { env->prefixes = realloc(env->prefixes, (++env->n_prefixes) * sizeof(SerdPrefix)); - env->prefixes[env->n_prefixes - 1].name = serd_string_new_from_node(name); - env->prefixes[env->n_prefixes - 1].uri = serd_string_new_from_node(uri); + env->prefixes[env->n_prefixes - 1].name = serd_node_copy(name); + env->prefixes[env->n_prefixes - 1].uri = serd_node_copy(uri); } } @@ -104,8 +120,8 @@ serd_env_expand(const SerdEnv env, const size_t name_len = colon - qname->buf; const SerdPrefix* const prefix = serd_env_find(env, qname->buf, name_len); if (prefix) { - uri_prefix->buf = prefix->uri->buf; - uri_prefix->len = prefix->uri->n_bytes - 1; + uri_prefix->buf = prefix->uri.buf; + uri_prefix->len = prefix->uri.n_bytes - 1; uri_suffix->buf = colon + 1; uri_suffix->len = qname->n_bytes - (colon - qname->buf) - 2; return true; diff --git a/src/reader.c b/src/reader.c index 35067403..773dd99b 100644 --- a/src/reader.c +++ b/src/reader.c @@ -60,6 +60,13 @@ typedef struct { const Node* predicate; } ReadContext; +/** Measured UTF-8 string. */ +typedef struct { + size_t n_bytes; ///< Size in bytes including trailing null byte + size_t n_chars; ///< Length in characters + uint8_t buf[]; ///< Buffer +} SerdString; + static const Node INTERNAL_NODE_NULL = { 0, 0, 0, 0 }; struct SerdReaderImpl { diff --git a/src/serd_internal.h b/src/serd_internal.h index 1b601e12..7adf1567 100644 --- a/src/serd_internal.h +++ b/src/serd_internal.h @@ -99,27 +99,26 @@ is_digit(const uint8_t c) return in_range(c, '0', '9'); } -/** Measured UTF-8 string. */ -typedef struct { - size_t n_bytes; ///< Size in bytes including trailing null byte - size_t n_chars; ///< Length in characters - uint8_t buf[]; ///< Buffer -} SerdString; - -#if 0 -/** Create a new UTF-8 string from @a utf8. */ -SerdString* -serd_string_new(const uint8_t* utf8); - -/** Copy @a string. */ -SerdString* -serd_string_copy(const SerdString* str); -#endif - -void -serd_string_free(SerdString* str); - -SerdString* -serd_string_new_from_node(const SerdNode* node); +/** UTF-8 strlen. + * @return Lengh of @a utf8 in characters. + * @param utf8 A null-terminated UTF-8 string. + * @param out_n_bytes (Output) Set to the size of @a utf8 in bytes. + */ +static inline size_t +serd_strlen(const uint8_t* utf8, size_t* out_n_bytes) +{ + size_t n_chars = 0; + size_t i = 0; + for (; utf8[i]; ++i) { + if ((utf8[i] & 0xC0) != 0x80) { + // Does not start with `10', start of a new character + ++n_chars; + } + } + if (out_n_bytes) { + *out_n_bytes = i + 1; + } + return n_chars; +} #endif // SERD_INTERNAL_H diff --git a/src/serdi.c b/src/serdi.c index 810de74c..451905b8 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -96,24 +96,6 @@ copy_string(const uint8_t* str, size_t* n_bytes) return ret; } -#if 0 -static SerdNode -serd_node_copy(const SerdNode* node) -{ - SerdNode copy = *node; - uint8_t* buf = malloc(copy.n_bytes); - memcpy(buf, node->buf, copy.n_bytes); - copy.buf = buf; - return copy; -} - -static void -serd_node_free(SerdNode* node) -{ - free((uint8_t*)node->buf); // FIXME: const cast -} -#endif - static bool event_base(void* handle, const SerdNode* uri_node) diff --git a/src/string.c b/src/string.c deleted file mode 100644 index 77346676..00000000 --- a/src/string.c +++ /dev/null @@ -1,91 +0,0 @@ -/* Serd, an RDF serialisation library. - * Copyright 2011 David Robillard <d@drobilla.net> - * - * Serd is free software: you can redistribute it and/or modify it under - * the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Serd is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public - * License for details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#include <assert.h> -#include <stdbool.h> -#include <stdlib.h> -#include <string.h> - -#include "serd_internal.h" - -#if 0 -static inline size_t -utf8_strlen(const uint8_t* utf8, size_t* out_n_bytes) -{ - size_t n_chars = 0; - size_t i = 0; - for (; utf8[i]; ++i) { - if ((utf8[i] & 0xC0) != 0x80) { - // Does not start with `10', start of a new character - ++n_chars; - } - } - if (out_n_bytes) { - *out_n_bytes = i + 1; - } - return n_chars; -} -#endif - -static SerdString* -serd_string_new_measured(const uint8_t* utf8, size_t n_bytes, size_t n_chars) -{ - SerdString* const str = malloc(sizeof(SerdString) + n_bytes); - str->n_bytes = n_bytes; - str->n_chars = n_chars; - memcpy(str->buf, utf8, n_bytes); - return str; -} - -#if 0 -SERD_API -SerdString* -serd_string_new(const uint8_t* utf8) -{ - size_t n_bytes; - size_t n_chars = utf8_strlen(utf8, &n_bytes); - return serd_string_new_measured(utf8, n_bytes, n_chars); -} -#endif - -SERD_API -SerdString* -serd_string_new_from_node(const SerdNode* node) -{ - return serd_string_new_measured(node->buf, node->n_bytes, node->n_chars); -} - -#if 0 -SERD_API -SerdString* -serd_string_copy(const SerdString* s) -{ - if (s) { - SerdString* const copy = malloc(sizeof(SerdString) + s->n_bytes); - memcpy(copy, s, sizeof(SerdString) + s->n_bytes); - return copy; - } - return NULL; -} -#endif - -SERD_API -void -serd_string_free(SerdString* str) -{ - free(str); -} @@ -57,7 +57,6 @@ def build(bld): lib_source = ''' src/env.c src/reader.c - src/string.c src/uri.c src/writer.c ''' |