diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/env.c | 43 | ||||
-rw-r--r-- | src/node.c | 21 | ||||
-rw-r--r-- | src/reader.c | 38 | ||||
-rw-r--r-- | src/serd_internal.h | 7 | ||||
-rw-r--r-- | src/string.c | 2 | ||||
-rw-r--r-- | src/uri.c | 35 | ||||
-rw-r--r-- | src/writer.c | 35 |
7 files changed, 77 insertions, 104 deletions
@@ -111,8 +111,6 @@ serd_env_add(SerdEnv* env, const SerdNode* name, const SerdNode* uri) { - assert(name && uri); - assert(name->n_bytes == strlen((const char*)name->buf)); SerdPrefix* const prefix = serd_env_find(env, name->buf, name->n_bytes); if (prefix) { SerdNode old_prefix_uri = prefix->uri; @@ -132,7 +130,7 @@ serd_env_set_prefix(SerdEnv* env, const SerdNode* name, const SerdNode* uri_node) { - if (!name->buf || !uri_node->buf || uri_node->type != SERD_URI) { + if (!name->buf || uri_node->type != SERD_URI) { return SERD_ERR_BAD_ARG; } else if (serd_uri_string_has_scheme(uri_node->buf)) { // Set prefix to absolute URI @@ -165,15 +163,9 @@ serd_env_set_prefix_from_strings(SerdEnv* env, } static inline bool -is_nameStartChar(const uint8_t c) -{ - return is_alpha(c) || is_digit(c) || c == '_'; -} - -static inline bool is_nameChar(const uint8_t c) { - return is_nameStartChar(c) || c == '.' || c == 0xB7; + return is_alpha(c) || is_digit(c) || c == '_'; } /** @@ -183,15 +175,12 @@ is_nameChar(const uint8_t c) static inline bool is_name(const uint8_t* buf, size_t len) { - if (is_nameStartChar(buf[0])) { - for (size_t i = 1; i < len; ++i) { - if (!is_nameChar(buf[i])) { - return false; - } + for (size_t i = 0; i < len; ++i) { + if (!is_nameChar(buf[i])) { + return false; } - return true; } - return false; + return true; } SERD_API @@ -248,23 +237,25 @@ SerdNode serd_env_expand_node(const SerdEnv* env, const SerdNode* node) { - if (node->type == SERD_CURIE) { + switch (node->type) { + case SERD_CURIE: { SerdChunk prefix; SerdChunk suffix; - serd_env_expand(env, node, &prefix, &suffix); - SerdNode ret = { NULL, - prefix.len + suffix.len + 1, - prefix.len + suffix.len, // FIXME: UTF-8 - 0, - SERD_URI }; + if (serd_env_expand(env, node, &prefix, &suffix)) { + return SERD_NODE_NULL; + } + const size_t len = prefix.len + suffix.len; // FIXME: UTF-8? + SerdNode ret = { NULL, len, len, 0, SERD_URI }; 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) { + } + case SERD_URI: { SerdURI ignored; return serd_node_new_uri_from_node(node, &env->base_uri, &ignored); - } else { + } + default: return SERD_NODE_NULL; } } @@ -37,7 +37,7 @@ SERD_API SerdNode serd_node_copy(const SerdNode* node) { - if (!node || !node->buf) { + if (!node) { return SERD_NODE_NULL; } @@ -93,7 +93,9 @@ serd_node_new_uri_from_node(const SerdNode* uri_node, const SerdURI* base, SerdURI* out) { - return serd_node_new_uri_from_string(uri_node->buf, base, out); + return (uri_node->type == SERD_URI) + ? serd_node_new_uri_from_string(uri_node->buf, base, out) + : SERD_NODE_NULL; } SERD_API @@ -104,13 +106,10 @@ serd_node_new_uri_from_string(const uint8_t* str, { if (!str || str[0] == '\0') { return serd_node_new_uri(base, NULL, out); // Empty URI => Base URI - } else { - SerdURI uri; - if (!serd_uri_parse(str, &uri)) { - return serd_node_new_uri(&uri, base, out); // Resolve/Serialise - } } - return SERD_NODE_NULL; + SerdURI uri; + serd_uri_parse(str, &uri); + return serd_node_new_uri(&uri, base, out); // Resolve/Serialise } SERD_API @@ -134,7 +133,9 @@ serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out) node.n_bytes = actual_len; node.n_chars = actual_len; - serd_uri_parse(buf, out); // TODO: cleverly avoid double parse + if (out) { + serd_uri_parse(buf, out); // TODO: cleverly avoid double parse + } return node; } @@ -177,7 +178,7 @@ serd_node_new_decimal(double d, unsigned frac_digits) unsigned i = 0; // Skip trailing zeros - for (; i < frac_digits && (frac % 10 == 0); ++i, --s, frac /= 10) {} + for (; i < frac_digits - 1 && !(frac % 10); ++i, --s, frac /= 10) {} node.n_bytes = node.n_chars = (s - buf) + 1; diff --git a/src/reader.c b/src/reader.c index fbc7a711..44f03997 100644 --- a/src/reader.c +++ b/src/reader.c @@ -112,7 +112,6 @@ warn(SerdReader* reader, const char* fmt, ...) static inline bool page(SerdReader* reader) { - assert(reader->from_file); reader->read_head = 0; const size_t n_read = fread(reader->read_buf, 1, SERD_PAGE_SIZE, reader->fd); if (n_read == 0) { @@ -143,8 +142,7 @@ eat_byte_safe(SerdReader* reader, const uint8_t byte) } if (reader->from_file && (reader->read_head == SERD_PAGE_SIZE)) { - TRY_RET(page(reader)); - assert(reader->read_head < SERD_PAGE_SIZE); + page(reader); } return byte; } @@ -220,7 +218,6 @@ push_byte(SerdReader* reader, Ref ref, const uint8_t c) if (!(c & 0x80)) { // Starts with 0 bit, start of new character ++node->n_chars; } - assert(node->n_bytes >= node->n_chars); *(s - 1) = c; *s = '\0'; } @@ -233,8 +230,7 @@ append_string(SerdReader* reader, Ref ref, const uint8_t* suffix, size_t len) #endif serd_stack_push(&reader->stack, len); SerdNode* const node = deref(reader, ref); - assert(node->n_bytes >= node->n_chars); - uint8_t* const buf = (uint8_t*)node + sizeof(SerdNode); + uint8_t* const buf = (uint8_t*)node + sizeof(SerdNode); memcpy(buf + node->n_bytes, suffix, len + 1); node->n_bytes += len; node->n_chars += len; @@ -266,7 +262,6 @@ emit_statement(SerdReader* reader, SerdStatementFlags* flags, Ref g, Ref s, Ref p, Ref o, Ref d, Ref l) { - assert(s && p && o); bool ret = !reader->statement_sink || !reader->statement_sink(reader->handle, *flags, deref(reader, g), @@ -302,7 +297,9 @@ read_hex_escape(SerdReader* reader, unsigned length, Ref dest) { uint8_t buf[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; for (unsigned i = 0; i < length; ++i) { - buf[i] = read_hex(reader); + if (!(buf[i] = read_hex(reader))) { + return false; + } } uint32_t c; @@ -478,11 +475,11 @@ read_character(SerdReader* reader, Ref dest) if (c == '\0') { error(reader, "unexpected end of file\n", c); return SERD_ERR_BAD_SYNTAX; - } else if (c < 0x20) {// || c == 0x7F) { + } else if (c < 0x20) { return bad_char(reader, dest, "unexpected control character 0x%X\n", eat_byte_safe(reader, c)); - } else if (reader->syntax == SERD_NTRIPLES || !(c & 0x80)) { + } else if (!(c & 0x80)) { push_byte(reader, dest, eat_byte_safe(reader, c)); return SERD_SUCCESS; } else { @@ -850,11 +847,12 @@ read_number(SerdReader* reader, Ref* dest, Ref* datatype) TRY_THROW(read_0_9(reader, ref, true)); } else { // all other cases ::= ( '-' | '+' ) [0-9]+ ( . )? ( [0-9]+ )? ... - TRY_THROW(read_0_9(reader, ref, true)); + assert(is_digit(c)); + read_0_9(reader, ref, true); if ((c = peek_byte(reader)) == '.') { has_decimal = true; push_byte(reader, ref, eat_byte_safe(reader, c)); - TRY_THROW(read_0_9(reader, ref, false)); + read_0_9(reader, ref, false); } } c = peek_byte(reader); @@ -925,11 +923,11 @@ except: } inline static bool -is_object_end(const uint8_t c) +is_token_end(const uint8_t c) { switch (c) { case 0x9: case 0xA: case 0xD: case 0x20: case '\0': - case '#': case '.': case ';': + case '#': case '.': case ';': case '<': return true; default: return false; @@ -950,8 +948,8 @@ read_verb(SerdReader* reader, Ref* dest) */ *dest = read_prefixName(reader, 0); node = deref(reader, *dest); - if (node && is_object_end(peek_byte(reader)) && - ((node->n_bytes == 1 && node->buf[0] == 'a'))) { + if (node && node->n_bytes == 1 && node->buf[0] == 'a' + && is_token_end(peek_byte(reader))) { pop_node(reader, *dest); return (*dest = push_node(reader, SERD_URI, NS_RDF "type", 47)); } else { @@ -1106,7 +1104,7 @@ read_object(SerdReader* reader, ReadContext ctx) */ o = read_prefixName(reader, 0); node = deref(reader, o); - if (node && is_object_end(peek_byte(reader)) && + if (node && is_token_end(peek_byte(reader)) && ((node->n_bytes == 4 && !memcmp(node->buf, "true", 4)) || (node->n_bytes == 5 && !memcmp(node->buf, "false", 5)))) { node->type = SERD_LITERAL; @@ -1154,14 +1152,14 @@ read_objectList(SerdReader* reader, ReadContext ctx, bool blank) // Spec: [7] predicateObjectList ::= verb objectList // (';' verb objectList)* (';')? -// Impl: [7] predicateObjectList ::= verb ws+ objectList +// Impl: [7] predicateObjectList ::= verb ws* objectList // (ws* ';' ws* verb ws+ objectList)* (';')? static bool read_predicateObjectList(SerdReader* reader, ReadContext ctx, bool blank) { Ref predicate = 0; TRY_RET(read_verb(reader, &predicate)); - TRY_THROW(read_ws_plus(reader)); + read_ws_star(reader); ctx.predicate = predicate; TRY_THROW(read_objectList(reader, ctx, blank)); pop_node(reader, predicate); @@ -1176,7 +1174,7 @@ read_predicateObjectList(SerdReader* reader, ReadContext ctx, bool blank) default: TRY_THROW(read_verb(reader, &predicate)); ctx.predicate = predicate; - TRY_THROW(read_ws_plus(reader)); + read_ws_star(reader); TRY_THROW(read_objectList(reader, ctx, blank)); pop_node(reader, predicate); predicate = 0; diff --git a/src/serd_internal.h b/src/serd_internal.h index 39f8d503..d81d0b34 100644 --- a/src/serd_internal.h +++ b/src/serd_internal.h @@ -157,10 +157,9 @@ serd_bulk_sink_flush(SerdBulkSink* bsink) static inline void serd_bulk_sink_free(SerdBulkSink* bsink) { - if (bsink) { - serd_bulk_sink_flush(bsink); - free(bsink->buf); - } + serd_bulk_sink_flush(bsink); + free(bsink->buf); + bsink->buf = NULL; } static inline size_t diff --git a/src/string.c b/src/string.c index 40828612..968a20e1 100644 --- a/src/string.c +++ b/src/string.c @@ -30,7 +30,7 @@ serd_strerror(SerdStatus st) case SERD_ERR_BAD_ARG: return (const uint8_t*)"Invalid argument"; case SERD_ERR_NOT_FOUND: return (const uint8_t*)"Not found"; } - return (const uint8_t*)"Success"; // never reached + return (const uint8_t*)"Unknown error code"; // never reached } SERD_API @@ -98,9 +98,6 @@ SerdStatus serd_uri_parse(const uint8_t* utf8, SerdURI* uri) { *uri = SERD_URI_NULL; - assert(uri->path_base.buf == NULL); - assert(uri->path_base.len == 0); - assert(uri->authority.len == 0); const uint8_t* ptr = utf8; @@ -137,7 +134,6 @@ maybe_authority: if (*ptr == '/' && *(ptr + 1) == '/') { ptr += 2; uri->authority.buf = ptr; - assert(uri->authority.len == 0); for (uint8_t c; (c = *ptr) != '\0'; ++ptr) { switch (c) { case '/': goto path; @@ -331,28 +327,18 @@ serd_uri_serialise(const SerdURI* uri, SerdSink sink, void* stream) size_t write_size = 0; #define WRITE(buf, len) \ write_size += len; \ - if (len) { \ - sink((const uint8_t*)buf, len, stream); \ - } -#define WRITE_CHAR(c) WRITE(&(c), 1) -#define WRITE_COMPONENT(prefix, field, suffix) \ - if ((field).len) { \ - for (const uint8_t* c = (const uint8_t*)prefix; *c != '\0'; ++c) { \ - WRITE(c, 1); \ - } \ - WRITE((field).buf, (field).len); \ - for (const uint8_t* c = (const uint8_t*)suffix; *c != '\0'; ++c) { \ - WRITE(c, 1); \ - } \ - } + sink((const uint8_t*)buf, len, stream); - WRITE_COMPONENT("", uri->scheme, ":"); + if (uri->scheme.buf) { + WRITE(uri->scheme.buf, uri->scheme.len); + WRITE(":", 1); + } if (uri->authority.buf) { WRITE("//", 2); WRITE(uri->authority.buf, uri->authority.len); } if (!uri->path.buf) { - WRITE_COMPONENT("", uri->path_base, ""); + WRITE(uri->path_base.buf, uri->path_base.len); } else { const uint8_t* begin = uri->path.buf; const uint8_t* const end = uri->path.buf + uri->path.len; @@ -377,16 +363,19 @@ serd_uri_serialise(const SerdURI* uri, SerdSink sink, void* stream) } else { // Relative path is just query or fragment, append to base URI - WRITE_COMPONENT("", uri->path_base, ""); + WRITE(uri->path_base.buf, uri->path_base.len); } // Write URI suffix WRITE(begin, end - begin); } - WRITE_COMPONENT("?", uri->query, ""); + if (uri->query.buf) { + WRITE("?", 1); + WRITE(uri->query.buf, uri->query.len); + } if (uri->fragment.buf) { // Note uri->fragment.buf includes the leading `#' - WRITE_COMPONENT("", uri->fragment, ""); + WRITE(uri->fragment.buf, uri->fragment.len); } return write_size; } diff --git a/src/writer.c b/src/writer.c index c388188e..a9209f80 100644 --- a/src/writer.c +++ b/src/writer.c @@ -176,12 +176,10 @@ write_text(SerdWriter* writer, TextContext ctx, continue; } -#define READ_BYTE() do { \ - assert(i < n_bytes); \ - in = utf8[i++] & 0x3f; \ - c <<= 6; \ - c |= in; \ - } while (0) +#define READ_BYTE() \ + in = utf8[i++] & 0x3f; \ + c <<= 6; \ + c |= in; switch (size) { case 4: READ_BYTE(); @@ -338,14 +336,13 @@ write_node(SerdWriter* writer, } else if ((writer->style & SERD_STYLE_RESOLVED) && !serd_uri_string_has_scheme(node->buf)) { SerdURI uri; - if (!serd_uri_parse(node->buf, &uri)) { - SerdURI abs_uri; - serd_uri_resolve(&uri, &writer->base_uri, &abs_uri); - sink("<", 1, writer); - serd_uri_serialise(&abs_uri, (SerdSink)sink, writer); - sink(">", 1, writer); - return true; - } + serd_uri_parse(node->buf, &uri); + SerdURI abs_uri; + serd_uri_resolve(&uri, &writer->base_uri, &abs_uri); + sink("<", 1, writer); + serd_uri_serialise(&abs_uri, (SerdSink)sink, writer); + sink(">", 1, writer); + return true; } sink("<", 1, writer); write_text(writer, WRITE_URI, node->buf, node->n_bytes, '>'); @@ -396,7 +393,7 @@ serd_writer_write_statement(SerdWriter* writer, } sink(" .\n", 3, writer); return SERD_SUCCESS; - case SERD_TURTLE: + default: break; } if (serd_node_equals(subject, &writer->context.subject)) { @@ -428,9 +425,8 @@ serd_writer_write_statement(SerdWriter* writer, } } else { if (writer->context.subject.type) { - if (writer->indent > 0) { - --writer->indent; - } + assert(writer->indent > 0); + --writer->indent; if (serd_stack_is_empty(&writer->anon_stack)) { serd_writer_write_delim(writer, '.'); serd_writer_write_delim(writer, '\n'); @@ -460,8 +456,7 @@ serd_writer_write_statement(SerdWriter* writer, FIELD_OBJECT, flags); } - if (writer->syntax != SERD_NTRIPLES - && ((flags & SERD_ANON_S_BEGIN) || (flags & SERD_ANON_O_BEGIN))) { + if ((flags & SERD_ANON_S_BEGIN) || (flags & SERD_ANON_O_BEGIN)) { WriteContext* ctx = (WriteContext*)serd_stack_push( &writer->anon_stack, sizeof(WriteContext)); *ctx = writer->context; |