aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2011-05-12 16:46:29 +0000
committerDavid Robillard <d@drobilla.net>2011-05-12 16:46:29 +0000
commitbc42e1c4d4fc93592b7e08226192acc00ccf37bc (patch)
treee97b59d0c982c42317a8eebd1851d69a2f85879c
parent13a66cb43bcab491f6ae7e1280b035b2522496c8 (diff)
downloadserd-bc42e1c4d4fc93592b7e08226192acc00ccf37bc.tar.gz
serd-bc42e1c4d4fc93592b7e08226192acc00ccf37bc.tar.bz2
serd-bc42e1c4d4fc93592b7e08226192acc00ccf37bc.zip
Use conventional string lengths not including NULL terminator
git-svn-id: http://svn.drobilla.net/serd/trunk@174 490d8e77-9747-427b-9fa3-0b8f29cee8a0
-rw-r--r--serd/serd.h8
-rw-r--r--src/env.c20
-rw-r--r--src/node.c10
-rw-r--r--src/reader.c58
-rw-r--r--src/writer.c22
5 files changed, 59 insertions, 59 deletions
diff --git a/serd/serd.h b/serd/serd.h
index 5c802a79..4d87c1a3 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -200,8 +200,8 @@ typedef uint32_t SerdNodeFlags;
*/
typedef struct {
const uint8_t* buf; /**< Value string */
- size_t n_bytes; /**< Size in bytes (including null) */
- size_t n_chars; /**< Length in characters */
+ size_t n_bytes; /**< Size in bytes (not including null) */
+ size_t n_chars; /**< Length in characters (not including null)*/
SerdNodeFlags flags; /**< String properties */
SerdType type; /**< Node type */
} SerdNode;
@@ -247,9 +247,9 @@ typedef enum {
/**
UTF-8 strlen.
- @return Length of @c str in characters.
+ @return Length of @c str in characters (except NULL).
@param str A null-terminated UTF-8 string.
- @param n_bytes (Output) Set to the size of @a str in bytes (incl. NULL).
+ @param n_bytes (Output) Set to the size of @a str in bytes (except NULL).
@param flags (Output) Set to the applicable flags.
*/
SERD_API
diff --git a/src/env.c b/src/env.c
index 26c7eb11..4011298d 100644
--- a/src/env.c
+++ b/src/env.c
@@ -94,7 +94,7 @@ serd_env_find(const SerdEnv* env,
{
for (size_t i = 0; i < env->n_prefixes; ++i) {
const SerdNode* const prefix_name = &env->prefixes[i].name;
- if (prefix_name->n_bytes == name_len + 1) {
+ if (prefix_name->n_bytes == name_len) {
if (!memcmp(prefix_name->buf, name, name_len)) {
return &env->prefixes[i];
}
@@ -109,7 +109,7 @@ serd_env_add(SerdEnv* env,
const SerdNode* uri)
{
assert(name && uri);
- SerdPrefix* const prefix = serd_env_find(env, name->buf, name->n_chars);
+ SerdPrefix* const prefix = serd_env_find(env, name->buf, name->n_bytes);
if (prefix) {
SerdNode old_prefix_uri = prefix->uri;
prefix->uri = serd_node_copy(uri);
@@ -160,10 +160,10 @@ serd_env_qualify(const SerdEnv* env,
if (uri->n_bytes >= prefix_uri->n_bytes) {
if (!strncmp((const char*)uri->buf,
(const char*)prefix_uri->buf,
- prefix_uri->n_bytes - 1)) {
+ prefix_uri->n_bytes)) {
*prefix_name = env->prefixes[i].name;
- suffix->buf = uri->buf + prefix_uri->n_bytes - 1;
- suffix->len = uri->n_bytes - prefix_uri->n_bytes;
+ suffix->buf = uri->buf + prefix_uri->n_bytes;
+ suffix->len = uri->n_bytes - prefix_uri->n_bytes - 1;
return true;
}
}
@@ -178,7 +178,7 @@ serd_env_expand(const SerdEnv* env,
SerdChunk* uri_prefix,
SerdChunk* uri_suffix)
{
- const uint8_t* const colon = memchr(qname->buf, ':', qname->n_bytes);
+ const uint8_t* const colon = memchr(qname->buf, ':', qname->n_bytes + 1);
if (!colon) {
return SERD_ERR_BAD_ARG; // Illegal qname
}
@@ -187,9 +187,9 @@ serd_env_expand(const SerdEnv* env,
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->len = prefix->uri.n_bytes;
uri_suffix->buf = colon + 1;
- uri_suffix->len = qname->n_bytes - (colon - qname->buf) - 2;
+ uri_suffix->len = qname->n_bytes - (colon - qname->buf) - 1;
return SERD_SUCCESS;
}
return SERD_ERR_NOT_FOUND;
@@ -209,8 +209,8 @@ serd_env_expand_node(const SerdEnv* env,
prefix.len + suffix.len, // FIXME: UTF-8
0,
SERD_URI };
- ret.buf = malloc(ret.n_bytes);
- snprintf((char*)ret.buf, ret.n_bytes, "%s%s", prefix.buf, suffix.buf);
+ ret.buf = malloc(ret.n_bytes + 1);
+ snprintf((char*)ret.buf, ret.n_bytes + 1, "%s%s", prefix.buf, suffix.buf);
return ret;
} else if (node->type == SERD_URI) {
SerdURI ignored;
diff --git a/src/node.c b/src/node.c
index b19e11f4..2e908c69 100644
--- a/src/node.c
+++ b/src/node.c
@@ -40,7 +40,7 @@ serd_strlen(const uint8_t* str, size_t* n_bytes, SerdNodeFlags* flags)
}
}
if (n_bytes) {
- *n_bytes = i + 1;
+ *n_bytes = i;
}
return n_chars;
}
@@ -65,8 +65,8 @@ serd_node_copy(const SerdNode* node)
}
SerdNode copy = *node;
- uint8_t* buf = malloc(copy.n_bytes);
- memcpy(buf, node->buf, copy.n_bytes);
+ uint8_t* buf = malloc(copy.n_bytes + 1);
+ memcpy(buf, node->buf, copy.n_bytes + 1);
copy.buf = buf;
return copy;
}
@@ -147,13 +147,13 @@ serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out)
const size_t len = serd_uri_string_length(&abs_uri);
uint8_t* buf = malloc(len + 1);
- SerdNode node = { buf, len + 1, len, 0, SERD_URI }; // FIXME: UTF-8
+ SerdNode node = { buf, len, len, 0, SERD_URI }; // FIXME: UTF-8
uint8_t* ptr = buf;
const size_t actual_len = serd_uri_serialise(&abs_uri, string_sink, &ptr);
buf[actual_len] = '\0';
- node.n_bytes = actual_len + 1;
+ node.n_bytes = actual_len;
node.n_chars = actual_len;
// FIXME: double parse
diff --git a/src/reader.c b/src/reader.c
index e956c3df..8804af8b 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -56,7 +56,7 @@ typedef struct {
/** Measured UTF-8 string. */
typedef struct {
- size_t n_bytes; ///< Size in bytes including trailing null byte
+ size_t n_bytes; ///< Size in bytes
size_t n_chars; ///< Length in characters
uint8_t buf[]; ///< Buffer
} SerdString;
@@ -195,11 +195,11 @@ static Ref
push_string(SerdReader* reader, const char* c_str, size_t n_bytes)
{
uint8_t* mem = serd_stack_push(&reader->stack,
- sizeof(SerdString) + n_bytes);
+ sizeof(SerdString) + n_bytes + 1);
SerdString* const str = (SerdString*)mem;
str->n_bytes = n_bytes;
- str->n_chars = n_bytes - 1;
- memcpy(str->buf, c_str, n_bytes);
+ str->n_chars = n_bytes;
+ memcpy(str->buf, c_str, n_bytes + 1);
#ifdef SERD_STACK_CHECK
reader->alloc_stack = realloc(
reader->alloc_stack, sizeof(uint8_t*) * (++reader->n_allocs));
@@ -236,9 +236,9 @@ push_byte(SerdReader* reader, Ref ref, const uint8_t c)
// Does not start with `10', start of a new character
++str->n_chars;
}
- assert(str->n_bytes > str->n_chars);
- str->buf[str->n_bytes - 2] = c;
- str->buf[str->n_bytes - 1] = '\0';
+ assert(str->n_bytes >= str->n_chars);
+ str->buf[str->n_bytes - 1] = c;
+ str->buf[str->n_bytes] = '\0';
}
static void
@@ -259,7 +259,7 @@ pop_string(SerdReader* reader, Ref ref)
--reader->n_allocs;
#endif
SerdString* str = deref(reader, ref);
- serd_stack_pop(&reader->stack, sizeof(SerdString) + str->n_bytes);
+ serd_stack_pop(&reader->stack, sizeof(SerdString) + str->n_bytes + 1);
}
}
@@ -624,7 +624,7 @@ static Ref
read_longString(SerdReader* reader, SerdNodeFlags* flags)
{
eat_string(reader, "\"\"\"", 3);
- Ref str = push_string(reader, "", 1);
+ Ref str = push_string(reader, "", 0);
SerdStatus st;
while (!(st = read_lcharacter(reader, str, flags))) {}
if (st < SERD_ERR_UNKNOWN) {
@@ -639,7 +639,7 @@ static Ref
read_string(SerdReader* reader, SerdNodeFlags* flags)
{
eat_byte(reader, '\"');
- Ref str = push_string(reader, "", 1);
+ Ref str = push_string(reader, "", 0);
SerdStatus st;
while (!(st = read_scharacter(reader, str, flags))) {}
if (st < SERD_ERR_UNKNOWN) {
@@ -672,7 +672,7 @@ read_quotedString(SerdReader* reader, SerdNodeFlags* flags)
static inline Ref
read_relativeURI(SerdReader* reader)
{
- Ref str = push_string(reader, "", 1);
+ Ref str = push_string(reader, "", 0);
SerdStatus st;
while (!(st = read_ucharacter(reader, str))) {}
if (st < SERD_ERR_UNKNOWN) {
@@ -730,7 +730,7 @@ read_prefixName(SerdReader* reader)
return 0;
}
TRY_RET(c = read_nameStartChar(reader, false));
- Ref str = push_string(reader, "", 1);
+ Ref str = push_string(reader, "", 0);
push_byte(reader, str, c);
while ((c = read_nameChar(reader)) != 0) {
push_byte(reader, str, c);
@@ -764,7 +764,7 @@ read_language(SerdReader* reader)
error(reader, "unexpected `%c'\n", start);
return 0;
}
- Ref str = push_string(reader, "", 1);
+ Ref str = push_string(reader, "", 0);
push_byte(reader, str, eat_byte(reader, start));
uint8_t c;
while ((c = peek_byte(reader)) && in_range(c, 'a', 'z')) {
@@ -799,7 +799,7 @@ read_qname(SerdReader* reader)
{
Ref prefix = read_prefixName(reader);
if (!prefix) {
- prefix = push_string(reader, "", 1);
+ prefix = push_string(reader, "", 0);
}
TRY_THROW(eat_byte(reader, ':'));
push_byte(reader, prefix, ':');
@@ -840,7 +840,7 @@ read_number(SerdReader* reader, Node* dest, Node* datatype)
#define XSD_DECIMAL NS_XSD "decimal"
#define XSD_DOUBLE NS_XSD "double"
#define XSD_INTEGER NS_XSD "integer"
- Ref str = push_string(reader, "", 1);
+ Ref str = push_string(reader, "", 0);
uint8_t c = peek_byte(reader);
bool has_decimal = false;
if (c == '-' || c == '+') {
@@ -870,11 +870,11 @@ read_number(SerdReader* reader, Node* dest, Node* datatype)
default: break;
}
read_0_9(reader, str, true);
- *datatype = push_uri(reader, XSD_DOUBLE, sizeof(XSD_DOUBLE));
+ *datatype = push_uri(reader, XSD_DOUBLE, sizeof(XSD_DOUBLE) - 1);
} else if (has_decimal) {
- *datatype = push_uri(reader, XSD_DECIMAL, sizeof(XSD_DECIMAL));
+ *datatype = push_uri(reader, XSD_DECIMAL, sizeof(XSD_DECIMAL) - 1);
} else {
- *datatype = push_uri(reader, XSD_INTEGER, sizeof(XSD_INTEGER));
+ *datatype = push_uri(reader, XSD_INTEGER, sizeof(XSD_INTEGER) - 1);
}
*dest = make_node(SERD_LITERAL, str);
assert(dest->value);
@@ -953,7 +953,7 @@ read_verb(SerdReader* reader, Node* dest)
switch (pre[1]) {
case 0x9: case 0xA: case 0xD: case 0x20:
eat_byte(reader, 'a');
- *dest = push_uri(reader, NS_RDF "type", 48);
+ *dest = push_uri(reader, NS_RDF "type", 47);
return true;
default: break; // fall through
}
@@ -968,7 +968,7 @@ read_nodeID(SerdReader* reader)
{
eat_byte(reader, '_');
eat_byte(reader, ':');
- Ref str = push_string(reader, "", 1);
+ Ref str = push_string(reader, "", 0);
return read_name(reader, str, true);
}
@@ -981,7 +981,7 @@ blank_id(SerdReader* reader)
char str[32]; // FIXME: ensure length of reader->blank_prefix is OK
const int len = snprintf(str, sizeof(str), "%s%u",
prefix, reader->next_id++);
- return push_string(reader, str, len + 1);
+ return push_string(reader, str, len);
}
// Spec: [21] blank ::= nodeID | '[]'
@@ -1101,13 +1101,13 @@ read_object(SerdReader* reader, ReadContext ctx)
peek_string(reader, pre, 6);
if (!memcmp(pre, "true", 4) && is_object_end(pre[4])) {
eat_string(reader, "true", 4);
- const Ref value = push_string(reader, "true", 5);
- datatype = push_uri(reader, XSD_BOOLEAN, XSD_BOOLEAN_LEN + 1);
+ const Ref value = push_string(reader, "true", 4);
+ datatype = push_uri(reader, XSD_BOOLEAN, XSD_BOOLEAN_LEN);
o = make_node(SERD_LITERAL, value);
} else if (!memcmp(pre, "false", 5) && is_object_end(pre[5])) {
eat_string(reader, "false", 5);
- const Ref value = push_string(reader, "false", 6);
- datatype = push_uri(reader, XSD_BOOLEAN, XSD_BOOLEAN_LEN + 1);
+ const Ref value = push_string(reader, "false", 5);
+ datatype = push_uri(reader, XSD_BOOLEAN, XSD_BOOLEAN_LEN);
o = make_node(SERD_LITERAL, value);
} else if (!is_object_end(c)) {
o = make_node(SERD_CURIE, read_qname(reader));
@@ -1302,7 +1302,7 @@ read_prefixID(SerdReader* reader)
bool ret = false;
Ref name = read_prefixName(reader);
if (!name) {
- name = push_string(reader, "", 1);
+ name = push_string(reader, "", 0);
}
TRY_THROW(eat_byte(reader, ':') == ':');
read_ws_star(reader);
@@ -1396,9 +1396,9 @@ serd_reader_new(SerdSyntax syntax,
#define RDF_FIRST NS_RDF "first"
#define RDF_REST NS_RDF "rest"
#define RDF_NIL NS_RDF "nil"
- me->rdf_first = make_node(SERD_URI, push_string(me, RDF_FIRST, 49));
- me->rdf_rest = make_node(SERD_URI, push_string(me, RDF_REST, 48));
- me->rdf_nil = make_node(SERD_URI, push_string(me, RDF_NIL, 47));
+ me->rdf_first = make_node(SERD_URI, push_string(me, RDF_FIRST, 48));
+ me->rdf_rest = make_node(SERD_URI, push_string(me, RDF_REST, 47));
+ me->rdf_nil = make_node(SERD_URI, push_string(me, RDF_NIL, 46));
return me;
}
diff --git a/src/writer.c b/src/writer.c
index bd78a050..d0120cde 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -214,7 +214,7 @@ write_node(SerdWriter* writer,
} // else fall through
case SERD_BLANK_ID:
writer->sink("_:", 2, writer->stream);
- writer->sink(node->buf, node->n_bytes - 1, writer->stream);
+ writer->sink(node->buf, node->n_bytes, writer->stream);
break;
case SERD_CURIE:
switch (writer->syntax) {
@@ -229,7 +229,7 @@ write_node(SerdWriter* writer,
writer->sink(">", 1, writer->stream);
break;
case SERD_TURTLE:
- writer->sink(node->buf, node->n_bytes - 1, writer->stream);
+ writer->sink(node->buf, node->n_bytes, writer->stream);
}
break;
case SERD_LITERAL:
@@ -238,7 +238,7 @@ write_node(SerdWriter* writer,
if (!strcmp((const char*)datatype->buf, NS_XSD "boolean")
|| !strcmp((const char*)datatype->buf, NS_XSD "decimal")
|| !strcmp((const char*)datatype->buf, NS_XSD "integer")) {
- writer->sink(node->buf, node->n_bytes - 1, writer->stream);
+ writer->sink(node->buf, node->n_bytes, writer->stream);
break;
}
}
@@ -246,16 +246,16 @@ write_node(SerdWriter* writer,
&& ((node->flags & SERD_HAS_NEWLINE)
|| (node->flags & SERD_HAS_QUOTE))) {
writer->sink("\"\"\"", 3, writer->stream);
- write_text(writer, WRITE_LONG_STRING, node->buf, node->n_bytes - 1, '\0');
+ write_text(writer, WRITE_LONG_STRING, node->buf, node->n_bytes, '\0');
writer->sink("\"\"\"", 3, writer->stream);
} else {
writer->sink("\"", 1, writer->stream);
- write_text(writer, WRITE_STRING, node->buf, node->n_bytes - 1, '"');
+ write_text(writer, WRITE_STRING, node->buf, node->n_bytes, '"');
writer->sink("\"", 1, writer->stream);
}
if (lang && lang->buf) {
writer->sink("@", 1, writer->stream);
- writer->sink(lang->buf, lang->n_bytes - 1, writer->stream);
+ writer->sink(lang->buf, lang->n_bytes, writer->stream);
} else if (datatype && datatype->buf) {
writer->sink("^^", 2, writer->stream);
write_node(writer, datatype, NULL, NULL, false);
@@ -271,7 +271,7 @@ write_node(SerdWriter* writer,
SerdNode prefix;
SerdChunk suffix;
if (serd_env_qualify(writer->env, node, &prefix, &suffix)) {
- write_text(writer, WRITE_URI, prefix.buf, prefix.n_bytes - 1, '>');
+ write_text(writer, WRITE_URI, prefix.buf, prefix.n_bytes, '>');
writer->sink(":", 1, writer->stream);
write_text(writer, WRITE_URI, suffix.buf, suffix.len, '>');
return true;
@@ -289,7 +289,7 @@ write_node(SerdWriter* writer,
}
}
writer->sink("<", 1, writer->stream);
- write_text(writer, WRITE_URI, node->buf, node->n_bytes - 1, '>');
+ write_text(writer, WRITE_URI, node->buf, node->n_bytes, '>');
writer->sink(">", 1, writer->stream);
return true;
}
@@ -472,7 +472,7 @@ serd_writer_set_base_uri(SerdWriter* writer,
reset_context(writer);
}
writer->sink("@base <", 7, writer->stream);
- writer->sink(uri->buf, uri->n_bytes - 1, writer->stream);
+ writer->sink(uri->buf, uri->n_bytes, writer->stream);
writer->sink("> .\n", 4, writer->stream);
}
reset_context(writer);
@@ -494,9 +494,9 @@ serd_writer_set_prefix(SerdWriter* writer,
reset_context(writer);
}
writer->sink("@prefix ", 8, writer->stream);
- writer->sink(name->buf, name->n_bytes - 1, writer->stream);
+ writer->sink(name->buf, name->n_bytes, writer->stream);
writer->sink(": <", 3, writer->stream);
- write_text(writer, WRITE_URI, uri->buf, uri->n_bytes - 1, '>');
+ write_text(writer, WRITE_URI, uri->buf, uri->n_bytes, '>');
writer->sink("> .\n", 4, writer->stream);
}
reset_context(writer);