diff options
-rw-r--r-- | NEWS | 1 | ||||
-rw-r--r-- | include/serd/serd.h | 103 | ||||
-rw-r--r-- | src/base64.c | 20 | ||||
-rw-r--r-- | src/byte_sink.h | 19 | ||||
-rw-r--r-- | src/byte_source.c | 8 | ||||
-rw-r--r-- | src/byte_source.h | 10 | ||||
-rw-r--r-- | src/env.c | 21 | ||||
-rw-r--r-- | src/n3.c | 12 | ||||
-rw-r--r-- | src/node.c | 63 | ||||
-rw-r--r-- | src/reader.c | 45 | ||||
-rw-r--r-- | src/reader.h | 12 | ||||
-rw-r--r-- | src/serdi.c | 54 | ||||
-rw-r--r-- | src/stack.h | 21 | ||||
-rw-r--r-- | src/string.c | 36 | ||||
-rw-r--r-- | src/string_utils.h | 6 | ||||
-rw-r--r-- | src/uri.c | 58 | ||||
-rw-r--r-- | src/uri_utils.h | 17 | ||||
-rw-r--r-- | src/writer.c | 78 | ||||
-rw-r--r-- | test/test_env.c | 27 | ||||
-rw-r--r-- | test/test_node.c | 35 | ||||
-rw-r--r-- | test/test_reader_writer.c | 60 | ||||
-rw-r--r-- | test/test_string.c | 12 | ||||
-rw-r--r-- | test/test_uri.c | 71 | ||||
-rw-r--r-- | test/test_writer.c | 29 |
24 files changed, 388 insertions, 430 deletions
@@ -4,6 +4,7 @@ serd (1.1.1) unstable; urgency=medium * Remove serd_uri_to_path() * Remove support for reading Turtle named inline nodes extension * Remove useless character counting from API + * Use char* for strings in public API -- David Robillard <d@drobilla.net> Wed, 13 Jul 2022 20:39:07 +0000 diff --git a/include/serd/serd.h b/include/serd/serd.h index 69315b18..3b47ce5c 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -78,8 +78,8 @@ typedef uint32_t SerdNodeFlags; /// An unterminated string fragment typedef struct { - const uint8_t* SERD_NULLABLE buf; ///< Start of chunk - size_t len; ///< Length of chunk in bytes + const char* SERD_NULLABLE buf; ///< Start of chunk + size_t len; ///< Length of chunk in bytes } SerdChunk; /** @@ -114,7 +114,7 @@ typedef enum { /// Return a string describing a status code SERD_CONST_API -const uint8_t* SERD_NONNULL +const char* SERD_NONNULL serd_strerror(SerdStatus status); /** @@ -131,8 +131,7 @@ serd_strerror(SerdStatus status); @param flags (Output) Set to the applicable flags. */ SERD_API size_t -serd_strlen(const uint8_t* SERD_NONNULL str, - SerdNodeFlags* SERD_NULLABLE flags); +serd_strlen(const char* SERD_NONNULL str, SerdNodeFlags* SERD_NULLABLE flags); /** Parse a string to a double. @@ -157,9 +156,9 @@ serd_strtod(const char* SERD_NONNULL str, @return A newly allocated blob which must be freed with serd_free(). */ SERD_API void* SERD_ALLOCATED -serd_base64_decode(const uint8_t* SERD_NONNULL str, - size_t len, - size_t* SERD_NONNULL size); +serd_base64_decode(const char* SERD_NONNULL str, + size_t len, + size_t* SERD_NONNULL size); /** @} @@ -232,18 +231,18 @@ static const SerdURI SERD_URI_NULL = @param hostname If non-NULL, set to the hostname, if present. @return The path component of the URI. */ -SERD_API uint8_t* SERD_NULLABLE -serd_file_uri_parse(const uint8_t* SERD_NONNULL uri, - uint8_t* SERD_NONNULL* SERD_NULLABLE hostname); +SERD_API char* SERD_NULLABLE +serd_file_uri_parse(const char* SERD_NONNULL uri, + char* SERD_NONNULL* SERD_NULLABLE hostname); /// Return true iff `utf8` starts with a valid URI scheme SERD_PURE_API bool -serd_uri_string_has_scheme(const uint8_t* SERD_NULLABLE utf8); +serd_uri_string_has_scheme(const char* SERD_NULLABLE utf8); /// Parse `utf8`, writing result to `out` SERD_API SerdStatus -serd_uri_parse(const uint8_t* SERD_NONNULL utf8, SerdURI* SERD_NONNULL out); +serd_uri_parse(const char* SERD_NONNULL utf8, SerdURI* SERD_NONNULL out); /** Set target `t` to reference `r` resolved against `base`. @@ -337,10 +336,10 @@ typedef enum { /// A syntactic RDF node typedef struct { - const uint8_t* SERD_NULLABLE buf; ///< Value string - size_t n_bytes; ///< Size in bytes (excluding null) - SerdNodeFlags flags; ///< Node flags (string properties) - SerdType type; ///< Node type + const char* SERD_NULLABLE buf; ///< Value string + size_t n_bytes; ///< Size in bytes (excluding null) + SerdNodeFlags flags; ///< Node flags (string properties) + SerdType type; ///< Node type } SerdNode; static const SerdNode SERD_NODE_NULL = {NULL, 0, 0, SERD_NOTHING}; @@ -351,7 +350,7 @@ static const SerdNode SERD_NODE_NULL = {NULL, 0, 0, SERD_NOTHING}; This measures, but does not copy, `str`. No memory is allocated. */ SERD_API SerdNode -serd_node_from_string(SerdType type, const uint8_t* SERD_NULLABLE str); +serd_node_from_string(SerdType type, const char* SERD_NULLABLE str); /** Make a (shallow) node from a prefix of `str`. @@ -360,9 +359,9 @@ serd_node_from_string(SerdType type, const uint8_t* SERD_NULLABLE str); Note that the returned node may not be null terminated. */ SERD_API SerdNode -serd_node_from_substring(SerdType type, - const uint8_t* SERD_NULLABLE str, - size_t len); +serd_node_from_substring(SerdType type, + const char* SERD_NULLABLE str, + size_t len); /// Simple wrapper for serd_node_new_uri() to resolve a URI node SERD_API SerdNode @@ -372,7 +371,7 @@ serd_node_new_uri_from_node(const SerdNode* SERD_NONNULL uri_node, /// Simple wrapper for serd_node_new_uri() to resolve a URI string SERD_API SerdNode -serd_node_new_uri_from_string(const uint8_t* SERD_NULLABLE str, +serd_node_new_uri_from_string(const char* SERD_NULLABLE str, const SerdURI* SERD_NULLABLE base, SerdURI* SERD_NULLABLE out); @@ -386,9 +385,9 @@ serd_node_new_uri_from_string(const uint8_t* SERD_NULLABLE str, If `out` is not NULL, it will be set to the parsed URI. */ SERD_API SerdNode -serd_node_new_file_uri(const uint8_t* SERD_NONNULL path, - const uint8_t* SERD_NULLABLE hostname, - SerdURI* SERD_NULLABLE out); +serd_node_new_file_uri(const char* SERD_NONNULL path, + const char* SERD_NULLABLE hostname, + SerdURI* SERD_NULLABLE out); /** Create a new node by serialising `uri` into a new string. @@ -504,12 +503,12 @@ typedef uint32_t SerdStatementFlags; /// An error description typedef struct { - SerdStatus status; ///< Error code - const uint8_t* SERD_NULLABLE filename; ///< File with error - unsigned line; ///< Line in file with error or 0 - unsigned col; ///< Column in file with error - const char* SERD_NONNULL fmt; ///< Printf-style format string - va_list* SERD_NONNULL args; ///< Arguments for fmt + SerdStatus status; ///< Error code + const char* SERD_NULLABLE filename; ///< File with error + unsigned line; ///< Line in file with error or 0 + unsigned col; ///< Column in file with error + const char* SERD_NONNULL fmt; ///< Printf-style format string + va_list* SERD_NONNULL args; ///< Arguments for fmt } SerdError; /** @@ -604,9 +603,9 @@ serd_env_set_prefix(SerdEnv* SERD_NONNULL env, /// Set a namespace prefix SERD_API SerdStatus -serd_env_set_prefix_from_strings(SerdEnv* SERD_NONNULL env, - const uint8_t* SERD_NONNULL name, - const uint8_t* SERD_NONNULL uri); +serd_env_set_prefix_from_strings(SerdEnv* SERD_NONNULL env, + const char* SERD_NONNULL name, + const char* SERD_NONNULL uri); /// Qualify `uri` into a CURIE if possible SERD_API bool @@ -697,8 +696,8 @@ serd_reader_get_handle(const SerdReader* SERD_NONNULL reader); this can be avoided, while preserving blank node names. */ SERD_API void -serd_reader_add_blank_prefix(SerdReader* SERD_NONNULL reader, - const uint8_t* SERD_NULLABLE prefix); +serd_reader_add_blank_prefix(SerdReader* SERD_NONNULL reader, + const char* SERD_NULLABLE prefix); /** Set the URI of the default graph. @@ -713,8 +712,8 @@ serd_reader_set_default_graph(SerdReader* SERD_NONNULL reader, /// Read a file at a given `uri` SERD_API SerdStatus -serd_reader_read_file(SerdReader* SERD_NONNULL reader, - const uint8_t* SERD_NONNULL uri); +serd_reader_read_file(SerdReader* SERD_NONNULL reader, + const char* SERD_NONNULL uri); /** Start an incremental read from a file handle. @@ -725,10 +724,10 @@ serd_reader_read_file(SerdReader* SERD_NONNULL reader, arrives, set `bulk` to false. */ SERD_API SerdStatus -serd_reader_start_stream(SerdReader* SERD_NONNULL reader, - FILE* SERD_NONNULL file, - const uint8_t* SERD_NULLABLE name, - bool bulk); +serd_reader_start_stream(SerdReader* SERD_NONNULL reader, + FILE* SERD_NONNULL file, + const char* SERD_NULLABLE name, + bool bulk); /** Start an incremental read from a user-specified source. @@ -741,7 +740,7 @@ serd_reader_start_source_stream(SerdReader* SERD_NONNULL reader, SerdSource SERD_NONNULL read_func, SerdStreamErrorFunc SERD_NONNULL error_func, void* SERD_NONNULL stream, - const uint8_t* SERD_NULLABLE name, + const char* SERD_NULLABLE name, size_t page_size); /** @@ -761,9 +760,9 @@ serd_reader_end_stream(SerdReader* SERD_NONNULL reader); /// Read `file` SERD_API SerdStatus -serd_reader_read_file_handle(SerdReader* SERD_NONNULL reader, - FILE* SERD_NONNULL file, - const uint8_t* SERD_NULLABLE name); +serd_reader_read_file_handle(SerdReader* SERD_NONNULL reader, + FILE* SERD_NONNULL file, + const char* SERD_NULLABLE name); /// Read a user-specified byte source SERD_API SerdStatus @@ -771,13 +770,13 @@ serd_reader_read_source(SerdReader* SERD_NONNULL reader, SerdSource SERD_NONNULL source, SerdStreamErrorFunc SERD_NONNULL error, void* SERD_NONNULL stream, - const uint8_t* SERD_NULLABLE name, + const char* SERD_NULLABLE name, size_t page_size); /// Read `utf8` SERD_API SerdStatus -serd_reader_read_string(SerdReader* SERD_NONNULL reader, - const uint8_t* SERD_NONNULL utf8); +serd_reader_read_string(SerdReader* SERD_NONNULL reader, + const char* SERD_NONNULL utf8); /** Skip over bytes in the input until a specific byte is encountered. @@ -868,7 +867,7 @@ serd_chunk_sink(const void* SERD_NONNULL buf, The returned string is the result of the serialisation, which is null terminated (by this function) and owned by the caller. */ -SERD_API uint8_t* SERD_NULLABLE +SERD_API char* SERD_NULLABLE serd_chunk_sink_finish(SerdChunk* SERD_NONNULL stream); /** @@ -889,8 +888,8 @@ serd_writer_set_error_sink(SerdWriter* SERD_NONNULL writer, to "undo" added prefixes. */ SERD_API void -serd_writer_chop_blank_prefix(SerdWriter* SERD_NONNULL writer, - const uint8_t* SERD_NULLABLE prefix); +serd_writer_chop_blank_prefix(SerdWriter* SERD_NONNULL writer, + const char* SERD_NULLABLE prefix); /** Set the current output base URI, and emit a directive if applicable. diff --git a/src/base64.c b/src/base64.c index ce8fe74d..13f08ab0 100644 --- a/src/base64.c +++ b/src/base64.c @@ -60,7 +60,6 @@ serd_base64_encode(uint8_t* const str, const bool wrap_lines) { bool has_newline = false; - for (size_t i = 0, j = 0; i < size; i += 3, j += 4) { uint8_t in[4] = {0, 0, 0, 0}; size_t n_in = MIN(3, size - i); @@ -94,28 +93,23 @@ decode_chunk(const uint8_t in[4], uint8_t out[3]) } void* -serd_base64_decode(const uint8_t* const str, - const size_t len, - size_t* const size) +serd_base64_decode(const char* const str, const size_t len, size_t* const size) { - void* buf = malloc((len * 3) / 4 + 2); + const uint8_t* const ustr = (const uint8_t*)str; - *size = 0; + void* buf = malloc((len * 3) / 4 + 2); + *size = 0; for (size_t i = 0, j = 0; i < len; j += 3) { uint8_t in[] = "===="; size_t n_in = 0; for (; i < len && n_in < 4; ++n_in) { - for (; i < len && !is_base64(str[i]); ++i) { - // Skip junk - } - - in[n_in] = str[i++]; + for (; i < len && !is_base64(ustr[i]); ++i) { + } // Skip junk + in[n_in] = ustr[i++]; } - if (n_in > 1) { *size += decode_chunk(in, (uint8_t*)buf + j); } } - return buf; } diff --git a/src/byte_sink.h b/src/byte_sink.h index 65b5eb12..39248c8c 100644 --- a/src/byte_sink.h +++ b/src/byte_sink.h @@ -10,13 +10,12 @@ #include "serd/serd.h" #include <stddef.h> -#include <stdint.h> #include <string.h> typedef struct SerdByteSinkImpl { SerdSink sink; void* stream; - uint8_t* buf; + char* buf; size_t size; size_t block_size; } SerdByteSink; @@ -24,12 +23,13 @@ typedef struct SerdByteSinkImpl { static inline SerdByteSink serd_byte_sink_new(SerdSink sink, void* stream, size_t block_size) { - SerdByteSink bsink = {sink, stream, NULL, 0, block_size}; - - if (block_size > 1) { - bsink.buf = (uint8_t*)serd_allocate_buffer(block_size); - } - + SerdByteSink bsink; + bsink.sink = sink; + bsink.stream = stream; + bsink.size = 0; + bsink.block_size = block_size; + bsink.buf = + ((block_size > 1) ? (char*)serd_allocate_buffer(block_size) : NULL); return bsink; } @@ -74,7 +74,7 @@ serd_byte_sink_write(const void* buf, size_t len, SerdByteSink* bsink) // Write as much as possible into the remaining buffer space memcpy(bsink->buf + bsink->size, buf, n); bsink->size += n; - buf = (const uint8_t*)buf + n; + buf = (const char*)buf + n; len -= n; // Flush page if buffer is full @@ -83,7 +83,6 @@ serd_byte_sink_write(const void* buf, size_t len, SerdByteSink* bsink) bsink->size = 0; } } - return orig_len; } diff --git a/src/byte_source.c b/src/byte_source.c index 7c191745..72a2366e 100644 --- a/src/byte_source.c +++ b/src/byte_source.c @@ -38,7 +38,7 @@ serd_byte_source_open_source(SerdByteSource* const source, const SerdSource read_func, const SerdStreamErrorFunc error_func, void* const stream, - const uint8_t* const name, + const char* const name, const size_t page_size) { const Cursor cur = {name, 1, 1}; @@ -82,14 +82,14 @@ serd_byte_source_prepare(SerdByteSource* const source) SerdStatus serd_byte_source_open_string(SerdByteSource* const source, - const uint8_t* const utf8) + const char* const utf8) { - const Cursor cur = {(const uint8_t*)"(string)", 1, 1}; + const Cursor cur = {"(string)", 1, 1}; memset(source, '\0', sizeof(*source)); source->page_size = 1; source->cur = cur; - source->read_buf = utf8; + source->read_buf = (const uint8_t*)utf8; return SERD_SUCCESS; } diff --git a/src/byte_source.h b/src/byte_source.h index 3eafe4b8..218e47fa 100644 --- a/src/byte_source.h +++ b/src/byte_source.h @@ -13,9 +13,9 @@ #include <stdio.h> typedef struct { - const uint8_t* filename; - unsigned line; - unsigned col; + const char* filename; + unsigned line; + unsigned col; } Cursor; typedef struct { @@ -38,14 +38,14 @@ SerdStatus serd_byte_source_open_file(SerdByteSource* source, FILE* file, bool bulk); SerdStatus -serd_byte_source_open_string(SerdByteSource* source, const uint8_t* utf8); +serd_byte_source_open_string(SerdByteSource* source, const char* utf8); SerdStatus serd_byte_source_open_source(SerdByteSource* source, SerdSource read_func, SerdStreamErrorFunc error_func, void* stream, - const uint8_t* name, + const char* name, size_t page_size); SerdStatus @@ -4,7 +4,6 @@ #include "serd/serd.h" #include <stdbool.h> -#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -91,7 +90,7 @@ serd_env_set_base_uri(SerdEnv* const env, const SerdNode* const uri) SERD_PURE_FUNC static SerdPrefix* serd_env_find(const SerdEnv* const env, - const uint8_t* const name, + const char* const name, const size_t name_len) { for (size_t i = 0; i < env->n_prefixes; ++i) { @@ -156,9 +155,9 @@ serd_env_set_prefix(SerdEnv* const env, } SerdStatus -serd_env_set_prefix_from_strings(SerdEnv* const env, - const uint8_t* const name, - const uint8_t* const uri) +serd_env_set_prefix_from_strings(SerdEnv* const env, + const char* const name, + const char* const uri) { const SerdNode name_node = serd_node_from_string(SERD_LITERAL, name); const SerdNode uri_node = serd_node_from_string(SERD_URI, uri); @@ -179,9 +178,7 @@ serd_env_qualify(const SerdEnv* const env, for (size_t i = 0; i < env->n_prefixes; ++i) { const SerdNode* const prefix_uri = &env->prefixes[i].uri; if (uri->n_bytes >= prefix_uri->n_bytes) { - if (!strncmp((const char*)uri->buf, - (const char*)prefix_uri->buf, - prefix_uri->n_bytes)) { + if (!strncmp(uri->buf, prefix_uri->buf, prefix_uri->n_bytes)) { *prefix = env->prefixes[i].name; suffix->buf = uri->buf + prefix_uri->n_bytes; suffix->len = uri->n_bytes - prefix_uri->n_bytes; @@ -202,8 +199,8 @@ serd_env_expand(const SerdEnv* const env, return SERD_ERR_BAD_CURIE; } - const uint8_t* const colon = - (const uint8_t*)memchr(curie->buf, ':', curie->n_bytes + 1); + const char* const colon = + (const char*)memchr(curie->buf, ':', curie->n_bytes + 1); if (curie->type != SERD_CURIE || !colon) { return SERD_ERR_BAD_ARG; } @@ -242,9 +239,9 @@ serd_env_expand_node(const SerdEnv* const env, const SerdNode* const node) return SERD_NODE_NULL; } const size_t len = prefix.len + suffix.len; - uint8_t* buf = (uint8_t*)malloc(len + 1); + char* buf = (char*)malloc(len + 1); SerdNode ret = {buf, len, 0, SERD_URI}; - snprintf((char*)buf, ret.n_bytes + 1, "%s%s", prefix.buf, suffix.buf); + snprintf(buf, ret.n_bytes + 1, "%s%s", prefix.buf, suffix.buf); return ret; } case SERD_BLANK: @@ -1006,11 +1006,10 @@ read_BLANK_NODE_LABEL(SerdReader* const reader, return SERD_ERR_BAD_SYNTAX; } - const Ref ref = *dest = - push_node(reader, - SERD_BLANK, - reader->bprefix ? (char*)reader->bprefix : "", - reader->bprefix_len); + const Ref ref = *dest = push_node(reader, + SERD_BLANK, + reader->bprefix ? reader->bprefix : "", + reader->bprefix_len); int c = peek_byte(reader); // First: (PN_CHARS | '_' | [0-9]) if (is_digit(c) || c == '_') { @@ -1049,7 +1048,6 @@ read_BLANK_NODE_LABEL(SerdReader* const reader, } } } - return SERD_SUCCESS; } @@ -1578,7 +1576,7 @@ tokcmp(SerdReader* const reader, return -1; } - return serd_strncasecmp((const char*)node->buf, tok, n); + return serd_strncasecmp(node->buf, tok, n); } SerdStatus @@ -47,14 +47,14 @@ serd_uri_string_length(const SerdURI* const uri) static size_t string_sink(const void* const buf, const size_t len, void* const stream) { - uint8_t** ptr = (uint8_t**)stream; + char** ptr = (char**)stream; memcpy(*ptr, buf, len); *ptr += len; return len; } SerdNode -serd_node_from_string(const SerdType type, const uint8_t* const str) +serd_node_from_string(SerdType type, const char* const str) { if (!str) { return SERD_NODE_NULL; @@ -67,9 +67,9 @@ serd_node_from_string(const SerdType type, const uint8_t* const str) } SerdNode -serd_node_from_substring(const SerdType type, - const uint8_t* const str, - const size_t len) +serd_node_from_substring(const SerdType type, + const char* const str, + const size_t len) { if (!str) { return SERD_NODE_NULL; @@ -89,7 +89,7 @@ serd_node_copy(const SerdNode* const node) } SerdNode copy = *node; - uint8_t* buf = (uint8_t*)malloc(copy.n_bytes + 1); + char* buf = (char*)malloc(copy.n_bytes + 1); memcpy(buf, node->buf, copy.n_bytes + 1); copy.buf = buf; return copy; @@ -100,8 +100,7 @@ serd_node_equals(const SerdNode* const a, const SerdNode* const b) { return (a == b) || (a->type == b->type && a->n_bytes == b->n_bytes && - ((a->buf == b->buf) || - !memcmp((const char*)a->buf, (const char*)b->buf, a->n_bytes + 1))); + ((a->buf == b->buf) || !memcmp(a->buf, b->buf, a->n_bytes + 1))); } SerdNode @@ -115,7 +114,7 @@ serd_node_new_uri_from_node(const SerdNode* const uri_node, } SerdNode -serd_node_new_uri_from_string(const uint8_t* const str, +serd_node_new_uri_from_string(const char* const str, const SerdURI* const base, SerdURI* const out) { @@ -130,7 +129,7 @@ serd_node_new_uri_from_string(const uint8_t* const str, } static bool -is_uri_path_char(const uint8_t c) +is_uri_path_char(const char c) { if (is_alpha(c) || is_digit(c)) { return true; @@ -176,28 +175,28 @@ is_dir_sep(const char c) } SerdNode -serd_node_new_file_uri(const uint8_t* const path, - const uint8_t* const hostname, - SerdURI* const out) +serd_node_new_file_uri(const char* const path, + const char* const hostname, + SerdURI* const out) { - const size_t path_len = strlen((const char*)path); - const size_t hostname_len = hostname ? strlen((const char*)hostname) : 0; + const size_t path_len = strlen(path); + const size_t hostname_len = hostname ? strlen(hostname) : 0; const bool is_windows = is_windows_path(path); size_t uri_len = 0; - uint8_t* uri = NULL; + char* uri = NULL; - if (is_dir_sep((char)path[0]) || is_windows) { + if (is_dir_sep(path[0]) || is_windows) { uri_len = strlen("file://") + hostname_len + is_windows; - uri = (uint8_t*)calloc(uri_len + 1, 1); + uri = (char*)calloc(uri_len + 1, 1); memcpy(uri, "file://", 7); if (hostname) { - memcpy(uri + 7, hostname, hostname_len); + memcpy(uri + 7, hostname, hostname_len + 1); } if (is_windows) { - ((char*)uri)[7 + hostname_len] = '/'; + uri[7 + hostname_len] = '/'; } } @@ -212,14 +211,13 @@ serd_node_new_file_uri(const uint8_t* const path, serd_chunk_sink("/", 1, &chunk); #endif } else { - char escape_str[4] = {'%', 0, 0, 0}; + char escape_str[10] = {'%', 0, 0, 0, 0, 0, 0, 0, 0, 0}; snprintf(escape_str + 1, sizeof(escape_str) - 1, "%X", (unsigned)path[i]); serd_chunk_sink(escape_str, 3, &chunk); } } - const uint8_t* const string = serd_chunk_sink_finish(&chunk); - + const char* const string = serd_chunk_sink_finish(&chunk); if (string && out) { serd_uri_parse(string, out); } @@ -238,9 +236,9 @@ serd_node_new_uri(const SerdURI* const uri, } const size_t len = serd_uri_string_length(&abs_uri); - uint8_t* buf = (uint8_t*)malloc(len + 1); + char* buf = (char*)malloc(len + 1); SerdNode node = {buf, len, 0, SERD_URI}; - uint8_t* ptr = buf; + char* ptr = buf; const size_t actual_len = serd_uri_serialise(&abs_uri, string_sink, &ptr); buf[actual_len] = '\0'; @@ -261,9 +259,9 @@ serd_node_new_relative_uri(const SerdURI* const uri, { const size_t uri_len = serd_uri_string_length(uri); const size_t base_len = serd_uri_string_length(base); - uint8_t* buf = (uint8_t*)malloc(uri_len + base_len + 1); + char* buf = (char*)malloc(uri_len + base_len + 1); SerdNode node = {buf, 0, 0, SERD_URI}; - uint8_t* ptr = buf; + char* ptr = buf; const size_t actual_len = serd_uri_serialise_relative(uri, base, root, string_sink, &ptr); @@ -294,7 +292,7 @@ serd_node_new_decimal(const double d, const unsigned frac_digits) const double abs_d = fabs(d); const unsigned int_digits = serd_digits(abs_d); char* buf = (char*)calloc(int_digits + frac_digits + 3, 1); - SerdNode node = {(const uint8_t*)buf, 0, 0, SERD_LITERAL}; + SerdNode node = {buf, 0, 0, SERD_LITERAL}; const double int_part = floor(abs_d); // Point s to decimal point location @@ -345,7 +343,7 @@ serd_node_new_integer(const int64_t i) uint64_t abs_i = (uint64_t)((i < 0) ? -i : i); const unsigned digits = serd_digits((double)abs_i); char* buf = (char*)calloc(digits + 2, 1); - SerdNode node = {(const uint8_t*)buf, 0, 0, SERD_LITERAL}; + SerdNode node = {(const char*)buf, 0, 0, SERD_LITERAL}; // Point s to the end char* s = buf + digits - 1; @@ -370,12 +368,13 @@ serd_node_new_blob(const void* const buf, const bool wrap_lines) { const size_t len = serd_base64_get_length(size, wrap_lines); - uint8_t* str = (uint8_t*)calloc(len + 2, 1); + char* const str = (char*)calloc(len + 2, 1); SerdNode node = {str, len, 0, SERD_LITERAL}; - if (serd_base64_encode(str, buf, size, wrap_lines)) { + if (serd_base64_encode((uint8_t*)str, buf, size, wrap_lines)) { node.flags |= SERD_HAS_NEWLINE; } + return node; } @@ -383,7 +382,7 @@ void serd_node_free(SerdNode* const node) { if (node && node->buf) { - free((uint8_t*)node->buf); + free((char*)node->buf); node->buf = NULL; } } diff --git a/src/reader.c b/src/reader.c index 0aa468a2..45f5f157 100644 --- a/src/reader.c +++ b/src/reader.c @@ -86,15 +86,15 @@ push_node_padded(SerdReader* const reader, node->type = type; node->buf = NULL; - uint8_t* buf = (uint8_t*)(node + 1); + char* buf = (char*)(node + 1); memcpy(buf, str, n_bytes + 1); #ifdef SERD_STACK_CHECK reader->allocs = (Ref*)realloc(reader->allocs, sizeof(reader->allocs) * (++reader->n_allocs)); - reader->allocs[reader->n_allocs - 1] = ((uint8_t*)mem - reader->stack.buf); + reader->allocs[reader->n_allocs - 1] = ((char*)mem - reader->stack.buf); #endif - return (Ref)((uint8_t*)node - reader->stack.buf); + return (Ref)((char*)node - reader->stack.buf); } Ref @@ -111,7 +111,7 @@ deref(SerdReader* const reader, const Ref ref) { if (ref) { SerdNode* node = (SerdNode*)(reader->stack.buf + ref); - node->buf = (uint8_t*)node + sizeof(SerdNode); + node->buf = (char*)node + sizeof(SerdNode); return node; } return NULL; @@ -127,8 +127,8 @@ pop_node(SerdReader* const reader, const Ref ref) --reader->n_allocs; #endif SerdNode* const node = deref(reader, ref); - uint8_t* const top = reader->stack.buf + reader->stack.size; - serd_stack_pop_aligned(&reader->stack, (size_t)(top - (uint8_t*)node)); + char* const top = reader->stack.buf + reader->stack.size; + serd_stack_pop_aligned(&reader->stack, (size_t)(top - (char*)node)); } return 0; } @@ -241,17 +241,16 @@ serd_reader_get_handle(const SerdReader* const reader) } void -serd_reader_add_blank_prefix(SerdReader* const reader, - const uint8_t* const prefix) +serd_reader_add_blank_prefix(SerdReader* const reader, const char* const prefix) { free(reader->bprefix); reader->bprefix_len = 0; reader->bprefix = NULL; - const size_t prefix_len = prefix ? strlen((const char*)prefix) : 0; + const size_t prefix_len = prefix ? strlen(prefix) : 0; if (prefix_len) { reader->bprefix_len = prefix_len; - reader->bprefix = (uint8_t*)malloc(reader->bprefix_len + 1); + reader->bprefix = (char*)malloc(reader->bprefix_len + 1); memcpy(reader->bprefix, prefix, reader->bprefix_len + 1); } } @@ -265,14 +264,14 @@ serd_reader_set_default_graph(SerdReader* const reader, } SerdStatus -serd_reader_read_file(SerdReader* const reader, const uint8_t* const uri) +serd_reader_read_file(SerdReader* const reader, const char* const uri) { - uint8_t* const path = serd_file_uri_parse(uri, NULL); + char* const path = serd_file_uri_parse(uri, NULL); if (!path) { return SERD_ERR_BAD_ARG; } - FILE* fd = serd_fopen((const char*)path, "rb"); + FILE* fd = serd_fopen(path, "rb"); if (!fd) { serd_free(path); return SERD_ERR_UNKNOWN; @@ -302,10 +301,10 @@ skip_bom(SerdReader* const me) } SerdStatus -serd_reader_start_stream(SerdReader* const reader, - FILE* const file, - const uint8_t* const name, - const bool bulk) +serd_reader_start_stream(SerdReader* const reader, + FILE* const file, + const char* const name, + const bool bulk) { return serd_reader_start_source_stream(reader, bulk ? (SerdSource)fread @@ -321,7 +320,7 @@ serd_reader_start_source_stream(SerdReader* const reader, const SerdSource read_func, const SerdStreamErrorFunc error_func, void* const stream, - const uint8_t* const name, + const char* const name, const size_t page_size) { return serd_byte_source_open_source( @@ -369,9 +368,9 @@ serd_reader_end_stream(SerdReader* const reader) } SerdStatus -serd_reader_read_file_handle(SerdReader* const reader, - FILE* const file, - const uint8_t* const name) +serd_reader_read_file_handle(SerdReader* const reader, + FILE* const file, + const char* const name) { return serd_reader_read_source(reader, (SerdSource)fread, @@ -386,7 +385,7 @@ serd_reader_read_source(SerdReader* const reader, const SerdSource source, const SerdStreamErrorFunc error, void* const stream, - const uint8_t* const name, + const char* const name, const size_t page_size) { SerdStatus st = serd_reader_start_source_stream( @@ -406,7 +405,7 @@ serd_reader_read_source(SerdReader* const reader, } SerdStatus -serd_reader_read_string(SerdReader* const reader, const uint8_t* const utf8) +serd_reader_read_string(SerdReader* const reader, const char* const utf8) { serd_byte_source_open_string(&reader->source, utf8); diff --git a/src/reader.h b/src/reader.h index 81edbc09..05774abe 100644 --- a/src/reader.h +++ b/src/reader.h @@ -55,7 +55,7 @@ struct SerdReaderImpl { SerdSyntax syntax; unsigned next_id; uint8_t* buf; - uint8_t* bprefix; + char* bprefix; size_t bprefix_len; bool strict; ///< True iff strict parsing bool seen_genid; @@ -153,7 +153,7 @@ static inline SerdStatus eat_string(SerdReader* reader, const char* str, unsigned n) { for (unsigned i = 0; i < n; ++i) { - if (!eat_byte_check(reader, ((const uint8_t*)str)[i])) { + if (!eat_byte_check(reader, str[i])) { return SERD_ERR_BAD_SYNTAX; } } @@ -166,11 +166,13 @@ push_byte(SerdReader* reader, Ref ref, const int c) assert(c != EOF); SERD_STACK_ASSERT_TOP(reader, ref); - uint8_t* const s = (uint8_t*)serd_stack_push(&reader->stack, 1); + char* const s = (char*)serd_stack_push(&reader->stack, 1); SerdNode* const node = (SerdNode*)(reader->stack.buf + ref); - ++node->n_bytes; - *(s - 1) = (uint8_t)c; + + *(s - 1) = (char)c; *s = '\0'; + + ++node->n_bytes; return SERD_SUCCESS; } diff --git a/src/serdi.c b/src/serdi.c index 6ab55b20..cb5bbfa0 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -20,7 +20,6 @@ #include <errno.h> #include <stdbool.h> -#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -179,24 +178,24 @@ main(int argc, char** argv) { const char* const prog = argv[0]; - FILE* in_fd = NULL; - SerdSyntax input_syntax = (SerdSyntax)0; - SerdSyntax output_syntax = (SerdSyntax)0; - bool from_file = true; - bool ascii = false; - bool bulk_read = true; - bool bulk_write = false; - bool full_uris = false; - bool lax = false; - bool quiet = false; - const uint8_t* in_name = NULL; - const uint8_t* add_prefix = NULL; - const uint8_t* chop_prefix = NULL; - const uint8_t* root_uri = NULL; - int a = 1; + FILE* in_fd = NULL; + SerdSyntax input_syntax = (SerdSyntax)0; + SerdSyntax output_syntax = (SerdSyntax)0; + bool from_file = true; + bool ascii = false; + bool bulk_read = true; + bool bulk_write = false; + bool full_uris = false; + bool lax = false; + bool quiet = false; + const char* in_name = NULL; + const char* add_prefix = NULL; + const char* chop_prefix = NULL; + const char* root_uri = NULL; + int a = 1; for (; a < argc && from_file && argv[a][0] == '-'; ++a) { if (argv[a][1] == '\0') { - in_name = (const uint8_t*)"(stdin)"; + in_name = (const char*)"(stdin)"; in_fd = stdin; break; } @@ -229,7 +228,7 @@ main(int argc, char** argv) } else if (opt == 'v') { return print_version(); } else if (opt == 's') { - in_name = (const uint8_t*)"(string)"; + in_name = "(string)"; from_file = false; break; } else if (opt == 'c') { @@ -237,7 +236,7 @@ main(int argc, char** argv) return missing_arg(prog, 'c'); } - chop_prefix = (const uint8_t*)argv[a]; + chop_prefix = argv[a]; break; } else if (opt == 'i') { if (argv[a][o + 1] || ++a == argc) { @@ -262,14 +261,14 @@ main(int argc, char** argv) return missing_arg(prog, 'p'); } - add_prefix = (const uint8_t*)argv[a]; + add_prefix = argv[a]; break; } else if (opt == 'r') { if (argv[a][o + 1] || ++a == argc) { return missing_arg(prog, 'r'); } - root_uri = (const uint8_t*)argv[a]; + root_uri = argv[a]; break; } else { SERDI_ERRORF("invalid option -- '%s'\n", argv[a] + 1); @@ -288,22 +287,22 @@ main(int argc, char** argv) _setmode(_fileno(stdout), _O_BINARY); #endif - uint8_t* input_path = NULL; - const uint8_t* input = (const uint8_t*)argv[a++]; + char* input_path = NULL; + const char* input = (const char*)argv[a++]; if (from_file) { in_name = in_name ? in_name : input; if (!in_fd) { - if (!strncmp((const char*)input, "file:", 5)) { + if (!strncmp(input, "file:", 5)) { input_path = serd_file_uri_parse(input, NULL); input = input_path; } - if (!input || !(in_fd = serd_fopen((const char*)input, "rb"))) { + if (!input || !(in_fd = serd_fopen(input, "rb"))) { return 1; } } } - if (!input_syntax && !(input_syntax = guess_syntax((const char*)in_name))) { + if (!input_syntax && !(input_syntax = guess_syntax(in_name))) { input_syntax = SERD_TRIG; } @@ -320,8 +319,7 @@ main(int argc, char** argv) SerdURI base_uri = SERD_URI_NULL; SerdNode base = SERD_NODE_NULL; if (a < argc) { // Base URI given on command line - base = - serd_node_new_uri_from_string((const uint8_t*)argv[a], NULL, &base_uri); + base = serd_node_new_uri_from_string((const char*)argv[a], NULL, &base_uri); } else if (from_file && in_fd != stdin) { // Use input file URI base = serd_node_new_file_uri(input, NULL, &base_uri); } diff --git a/src/stack.h b/src/stack.h index d118430a..7cd40a2f 100644 --- a/src/stack.h +++ b/src/stack.h @@ -15,9 +15,9 @@ /** A dynamic stack in memory. */ typedef struct { - uint8_t* buf; ///< Stack memory - size_t buf_size; ///< Allocated size of buf (>= size) - size_t size; ///< Conceptual size of stack in buf + char* buf; ///< Stack memory + size_t buf_size; ///< Allocated size of buf (>= size) + size_t size; ///< Conceptual size of stack in buf } SerdStack; /** An offset to start the stack at. Note 0 is reserved for NULL. */ @@ -27,7 +27,7 @@ static inline SerdStack serd_stack_new(size_t size) { SerdStack stack; - stack.buf = (uint8_t*)calloc(size, 1); + stack.buf = (char*)calloc(size, 1); stack.buf_size = size; stack.size = SERD_STACK_BOTTOM; return stack; @@ -54,12 +54,10 @@ serd_stack_push(SerdStack* stack, size_t n_bytes) const size_t new_size = stack->size + n_bytes; if (stack->buf_size < new_size) { stack->buf_size += (stack->buf_size >> 1); // *= 1.5 - stack->buf = (uint8_t*)realloc(stack->buf, stack->buf_size); + stack->buf = (char*)realloc(stack->buf, stack->buf_size); } - - uint8_t* const ret = (stack->buf + stack->size); - - stack->size = new_size; + char* const ret = (stack->buf + stack->size); + stack->size = new_size; return ret; } @@ -83,8 +81,7 @@ serd_stack_push_aligned(SerdStack* stack, size_t n_bytes, size_t align) } // Set top of stack to pad count so we can properly pop later - assert(pad < UINT8_MAX); - stack->buf[stack->size - 1] = (uint8_t)pad; + stack->buf[stack->size - 1] = (char)pad; // Push requested space at aligned location return serd_stack_push(stack, n_bytes); @@ -97,7 +94,7 @@ serd_stack_pop_aligned(SerdStack* stack, size_t n_bytes) serd_stack_pop(stack, n_bytes); // Get amount of padding from top of stack - const uint8_t pad = stack->buf[stack->size - 1]; + const uint8_t pad = (uint8_t)stack->buf[stack->size - 1]; // Pop padding and pad count serd_stack_pop(stack, pad + 1U); diff --git a/src/string.c b/src/string.c index 37d71c8b..b0ffe51a 100644 --- a/src/string.c +++ b/src/string.c @@ -7,7 +7,6 @@ #include <assert.h> #include <math.h> -#include <stdint.h> #include <stdlib.h> #include <string.h> @@ -17,38 +16,39 @@ serd_free(void* const ptr) free(ptr); } -const uint8_t* +const char* serd_strerror(const SerdStatus status) { switch (status) { case SERD_SUCCESS: - return (const uint8_t*)"Success"; + return "Success"; case SERD_FAILURE: - return (const uint8_t*)"Non-fatal failure"; + return "Non-fatal failure"; case SERD_ERR_UNKNOWN: - return (const uint8_t*)"Unknown error"; + break; case SERD_ERR_BAD_SYNTAX: - return (const uint8_t*)"Invalid syntax"; + return "Invalid syntax"; case SERD_ERR_BAD_ARG: - return (const uint8_t*)"Invalid argument"; + return "Invalid argument"; case SERD_ERR_NOT_FOUND: - return (const uint8_t*)"Not found"; + return "Not found"; case SERD_ERR_ID_CLASH: - return (const uint8_t*)"Blank node ID clash"; + return "Blank node ID clash"; case SERD_ERR_BAD_CURIE: - return (const uint8_t*)"Invalid CURIE"; + return "Invalid CURIE"; case SERD_ERR_INTERNAL: - return (const uint8_t*)"Internal error"; + return "Internal error"; case SERD_ERR_BAD_WRITE: - return (const uint8_t*)"Error writing to file/stream"; + return "Error writing to file/stream"; case SERD_ERR_BAD_TEXT: - return (const uint8_t*)"Invalid text encoding"; + return "Invalid text encoding"; } - return (const uint8_t*)"Unknown error"; // never reached + + return "Unknown error"; } static void -serd_update_flags(const uint8_t c, SerdNodeFlags* const flags) +serd_update_flags(const char c, SerdNodeFlags* const flags) { switch (c) { case '\r': @@ -64,7 +64,7 @@ serd_update_flags(const uint8_t c, SerdNodeFlags* const flags) } size_t -serd_substrlen(const uint8_t* const str, +serd_substrlen(const char* const str, const size_t len, SerdNodeFlags* const flags) { @@ -80,7 +80,7 @@ serd_substrlen(const uint8_t* const str, } size_t -serd_strlen(const uint8_t* const str, SerdNodeFlags* const flags) +serd_strlen(const char* const str, SerdNodeFlags* const flags) { if (flags) { size_t i = 0; @@ -91,7 +91,7 @@ serd_strlen(const uint8_t* const str, SerdNodeFlags* const flags) return i; } - return strlen((const char*)str); + return strlen(str); } static double diff --git a/src/string_utils.h b/src/string_utils.h index c6b85ac7..723df710 100644 --- a/src/string_utils.h +++ b/src/string_utils.h @@ -77,14 +77,14 @@ is_base64(const int c) } static inline bool -is_windows_path(const uint8_t* path) +is_windows_path(const char* path) { return is_alpha(path[0]) && (path[1] == ':' || path[1] == '|') && (path[2] == '/' || path[2] == '\\'); } size_t -serd_substrlen(const uint8_t* str, size_t len, SerdNodeFlags* flags); +serd_substrlen(const char* str, size_t len, SerdNodeFlags* flags); static inline uint8_t hex_digit_value(const uint8_t c) @@ -103,7 +103,7 @@ serd_strncasecmp(const char* s1, const char* s2, size_t n) { for (; n > 0 && *s2; s1++, s2++, --n) { if (serd_to_upper(*s1) != serd_to_upper(*s2)) { - return ((*(const uint8_t*)s1 < *(const uint8_t*)s2) ? -1 : +1); + return (*s1 < *s2) ? -1 : +1; } } @@ -12,26 +12,26 @@ #include <stdlib.h> #include <string.h> -uint8_t* -serd_file_uri_parse(const uint8_t* const uri, uint8_t** const hostname) +char* +serd_file_uri_parse(const char* const uri, char** const hostname) { - const uint8_t* path = uri; + const char* path = uri; if (hostname) { *hostname = NULL; } - - if (!strncmp((const char*)uri, "file://", 7)) { - const uint8_t* auth = uri + 7; + if (!strncmp(uri, "file://", 7)) { + const char* auth = uri + 7; if (*auth == '/') { // No hostname path = auth; } else { // Has hostname - if (!(path = (const uint8_t*)strchr((const char*)auth, '/'))) { + if (!(path = strchr(auth, '/'))) { return NULL; } if (hostname) { - *hostname = (uint8_t*)calloc((size_t)(path - auth + 1), 1); - memcpy(*hostname, auth, (size_t)(path - auth)); + const size_t len = (size_t)(path - auth); + *hostname = (char*)calloc(len + 1, 1); + memcpy(*hostname, auth, len); } } } @@ -41,14 +41,14 @@ serd_file_uri_parse(const uint8_t* const uri, uint8_t** const hostname) } SerdChunk chunk = {NULL, 0}; - for (const uint8_t* s = path; *s; ++s) { + for (const char* s = path; *s; ++s) { if (*s == '%') { if (*(s + 1) == '%') { serd_chunk_sink("%", 1, &chunk); ++s; } else if (is_hexdig(*(s + 1)) && is_hexdig(*(s + 2))) { - const uint8_t hi = hex_digit_value(s[1]); - const uint8_t lo = hex_digit_value(s[2]); + const uint8_t hi = hex_digit_value((const uint8_t)s[1]); + const uint8_t lo = hex_digit_value((const uint8_t)s[2]); const char c = (char)((hi << 4U) | lo); serd_chunk_sink(&c, 1, &chunk); s += 2; @@ -64,14 +64,14 @@ serd_file_uri_parse(const uint8_t* const uri, uint8_t** const hostname) } bool -serd_uri_string_has_scheme(const uint8_t* utf8) +serd_uri_string_has_scheme(const char* utf8) { // RFC3986: scheme ::= ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) if (!utf8 || !is_alpha(utf8[0])) { return false; // Invalid scheme initial character, URI is relative } - for (uint8_t c = 0; (c = *++utf8) != '\0';) { + for (char c = 0; (c = *++utf8) != '\0';) { if (!is_uri_scheme_char(c)) { return false; } @@ -85,11 +85,11 @@ serd_uri_string_has_scheme(const uint8_t* utf8) } SerdStatus -serd_uri_parse(const uint8_t* const utf8, SerdURI* const out) +serd_uri_parse(const char* const utf8, SerdURI* const out) { *out = SERD_URI_NULL; - const uint8_t* ptr = utf8; + const char* ptr = utf8; /* See http://tools.ietf.org/html/rfc3986#section-3 URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] @@ -97,7 +97,7 @@ serd_uri_parse(const uint8_t* const utf8, SerdURI* const out) /* S3.1: scheme ::= ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) */ if (is_alpha(*ptr)) { - for (uint8_t c = *++ptr; true; c = *++ptr) { + for (char c = *++ptr; true; c = *++ptr) { switch (c) { case '\0': case '/': @@ -129,7 +129,7 @@ maybe_authority: if (*ptr == '/' && *(ptr + 1) == '/') { ptr += 2; out->authority.buf = ptr; - for (uint8_t c = 0; (c = *ptr) != '\0'; ++ptr) { + for (char c = 0; (c = *ptr) != '\0'; ++ptr) { switch (c) { case '/': goto path; @@ -159,7 +159,7 @@ path: } out->path.buf = ptr; out->path.len = 0; - for (uint8_t c = 0; (c = *ptr) != '\0'; ++ptr) { + for (char c = 0; (c = *ptr) != '\0'; ++ptr) { switch (c) { case '?': goto query; @@ -177,7 +177,7 @@ path: query: if (*ptr == '?') { out->query.buf = ++ptr; - for (uint8_t c = 0; (c = *ptr) != '\0'; ++ptr) { + for (char c = 0; (c = *ptr) != '\0'; ++ptr) { if (c == '#') { goto fragment; } @@ -207,15 +207,13 @@ end: @param up Set to the number of up-references (e.g. "../") trimmed @return A pointer to the new start of `path` */ -static const uint8_t* -remove_dot_segments(const uint8_t* const path, - const size_t len, - size_t* const up) +static const char* +remove_dot_segments(const char* const path, const size_t len, size_t* const up) { *up = 0; for (size_t i = 0; i < len;) { - const char* const p = (char*)path + i; + const char* const p = path + i; if (!strcmp(p, ".")) { ++i; // Chop input "." } else if (!strcmp(p, "..")) { @@ -227,7 +225,7 @@ remove_dot_segments(const uint8_t* const path, ++*up; i += 3; // Chop leading "../", or replace "/../" with "/" } else { - return (uint8_t*)p; + return p; } } @@ -238,13 +236,13 @@ remove_dot_segments(const uint8_t* const path, static void merge(SerdChunk* const base, SerdChunk* const path) { - size_t up = 0; - const uint8_t* begin = remove_dot_segments(path->buf, path->len, &up); - const uint8_t* end = path->buf + path->len; + size_t up = 0; + const char* begin = remove_dot_segments(path->buf, path->len, &up); + const char* end = path->buf + path->len; if (base->len) { // Find the up'th last slash - const uint8_t* base_last = (base->buf + base->len - 1); + const char* base_last = (base->buf + base->len - 1); ++up; do { if (*base_last == '/') { diff --git a/src/uri_utils.h b/src/uri_utils.h index e2f30edb..c9f3ecf5 100644 --- a/src/uri_utils.h +++ b/src/uri_utils.h @@ -9,7 +9,6 @@ #include "string_utils.h" #include <stdbool.h> -#include <stdint.h> #include <string.h> typedef struct { @@ -20,8 +19,7 @@ typedef struct { static inline bool chunk_equals(const SerdChunk* a, const SerdChunk* b) { - return a->len == b->len && - !strncmp((const char*)a->buf, (const char*)b->buf, a->len); + return a->len == b->len && !strncmp(a->buf, b->buf, a->len); } static inline size_t @@ -30,11 +28,14 @@ uri_path_len(const SerdURI* uri) return uri->path_base.len + uri->path.len; } -static inline uint8_t +static inline char uri_path_at(const SerdURI* uri, size_t i) { - return (i < uri->path_base.len) ? uri->path_base.buf[i] - : uri->path.buf[i - uri->path_base.len]; + if (i < uri->path_base.len) { + return uri->path_base.buf[i]; + } + + return uri->path.buf[i - uri->path_base.len]; } /** @@ -60,8 +61,8 @@ uri_rooted_index(const SerdURI* uri, const SerdURI* root) const size_t root_len = uri_path_len(root); const size_t min_len = path_len < root_len ? path_len : root_len; for (size_t i = 0; i < min_len; ++i) { - const uint8_t u = uri_path_at(uri, i); - const uint8_t r = uri_path_at(root, i); + const char u = uri_path_at(uri, i); + const char r = uri_path_at(root, i); if (u == r) { if (u == '/') { diff --git a/src/writer.c b/src/writer.c index c72ce86b..751df38f 100644 --- a/src/writer.c +++ b/src/writer.c @@ -125,7 +125,7 @@ struct SerdWriterImpl { SerdErrorSink error_sink; void* error_handle; WriteContext context; - uint8_t* bprefix; + char* bprefix; size_t bprefix_len; Sep last_sep; int indent; @@ -178,7 +178,7 @@ w_err(SerdWriter* writer, SerdStatus st, const char* fmt, ...) va_list args; // NOLINT(cppcoreguidelines-init-variables) va_start(args, fmt); - const SerdError e = {st, (const uint8_t*)"", 0, 0, fmt, &args}; + const SerdError e = {st, "", 0, 0, fmt, &args}; serd_error(writer->error_sink, writer->error_handle, &e); va_end(args); return st; @@ -187,14 +187,14 @@ w_err(SerdWriter* writer, SerdStatus st, const char* fmt, ...) static void copy_node(SerdNode* dst, const SerdNode* src) { - const size_t new_size = src->n_bytes + 1U; - uint8_t* const new_buf = (uint8_t*)realloc((char*)dst->buf, new_size); + const size_t new_size = src->n_bytes + 1U; + char* const new_buf = (char*)realloc((char*)dst->buf, new_size); if (new_buf) { dst->buf = new_buf; dst->n_bytes = src->n_bytes; dst->flags = src->flags; dst->type = src->type; - memcpy((char*)dst->buf, src->buf, new_size); + memcpy(new_buf, src->buf, new_size); } } @@ -285,8 +285,8 @@ write_character(SerdWriter* writer, return sink(escape, 10, writer); } -SERD_NODISCARD static bool -uri_must_escape(const uint8_t c) +static bool +uri_must_escape(const int c) { switch (c) { case ' ': @@ -306,10 +306,7 @@ uri_must_escape(const uint8_t c) } static size_t -write_uri(SerdWriter* writer, - const uint8_t* utf8, - size_t n_bytes, - SerdStatus* st) +write_uri(SerdWriter* writer, const char* utf8, size_t n_bytes, SerdStatus* st) { size_t len = 0; for (size_t i = 0; i < n_bytes;) { @@ -334,7 +331,7 @@ write_uri(SerdWriter* writer, // Write UTF-8 character size_t size = 0; - len += write_character(writer, utf8 + i, &size, st); + len += write_character(writer, (const uint8_t*)utf8 + i, &size, st); i += size; if (*st && (writer->style & SERD_STYLE_STRICT)) { break; @@ -344,7 +341,7 @@ write_uri(SerdWriter* writer, // Corrupt input, write percent-encoded bytes and scan to next start char escape[4] = {0, 0, 0, 0}; for (; i < n_bytes && (utf8[i] & 0x80); ++i) { - snprintf(escape, sizeof(escape), "%%%02X", utf8[i]); + snprintf(escape, sizeof(escape), "%%%02X", (uint8_t)utf8[i]); len += sink(escape, 3, writer); } } @@ -354,7 +351,7 @@ write_uri(SerdWriter* writer, } SERD_NODISCARD static SerdStatus -ewrite_uri(SerdWriter* writer, const uint8_t* utf8, size_t n_bytes) +ewrite_uri(SerdWriter* writer, const char* utf8, size_t n_bytes) { SerdStatus st = SERD_SUCCESS; write_uri(writer, utf8, n_bytes, &st); @@ -371,7 +368,7 @@ write_uri_from_node(SerdWriter* writer, const SerdNode* node) } static bool -lname_must_escape(const uint8_t c) +lname_must_escape(const char c) { /* This arbitrary list of characters, most of which have nothing to do with Turtle, must be handled as special cases here because the RDF and SPARQL @@ -408,7 +405,7 @@ lname_must_escape(const uint8_t c) } SERD_NODISCARD static SerdStatus -write_lname(SerdWriter* writer, const uint8_t* utf8, size_t n_bytes) +write_lname(SerdWriter* writer, const char* utf8, size_t n_bytes) { SerdStatus st = SERD_SUCCESS; for (size_t i = 0; i < n_bytes; ++i) { @@ -434,10 +431,10 @@ write_lname(SerdWriter* writer, const uint8_t* utf8, size_t n_bytes) } SERD_NODISCARD static SerdStatus -write_text(SerdWriter* writer, - TextContext ctx, - const uint8_t* utf8, - size_t n_bytes) +write_text(SerdWriter* writer, + TextContext ctx, + const char* utf8, + size_t n_bytes) { size_t n_consecutive_quotes = 0; SerdStatus st = SERD_SUCCESS; @@ -460,7 +457,7 @@ write_text(SerdWriter* writer, break; // Reached end } - const uint8_t in = utf8[i++]; + const char in = utf8[i++]; if (ctx == WRITE_LONG_STRING) { n_consecutive_quotes = (in == '\"') ? (n_consecutive_quotes + 1) : 0; @@ -524,7 +521,7 @@ write_text(SerdWriter* writer, // Write UTF-8 character size_t size = 0; - write_character(writer, utf8 + i - 1, &size, &st); + write_character(writer, (const uint8_t*)utf8 + i - 1, &size, &st); if (st && (writer->style & SERD_STYLE_STRICT)) { return st; } @@ -553,7 +550,7 @@ uri_sink(const void* buf, size_t len, void* stream) UriSinkContext* const context = (UriSinkContext*)stream; SerdWriter* const writer = context->writer; - return write_uri(writer, (const uint8_t*)buf, len, &context->status); + return write_uri(writer, (const char*)buf, len, &context->status); } SERD_NODISCARD static SerdStatus @@ -662,7 +659,7 @@ write_literal(SerdWriter* writer, SerdStatus st = SERD_SUCCESS; if (supports_abbrev(writer) && datatype && datatype->buf) { - const char* type_uri = (const char*)datatype->buf; + const char* type_uri = datatype->buf; if (!strncmp(type_uri, NS_XSD, sizeof(NS_XSD) - 1) && (!strcmp(type_uri + sizeof(NS_XSD) - 1, "boolean") || !strcmp(type_uri + sizeof(NS_XSD) - 1, "integer"))) { @@ -671,8 +668,7 @@ write_literal(SerdWriter* writer, if (!strncmp(type_uri, NS_XSD, sizeof(NS_XSD) - 1) && !strcmp(type_uri + sizeof(NS_XSD) - 1, "decimal") && - strchr((const char*)node->buf, '.') && - node->buf[node->n_bytes - 1] != '.') { + strchr(node->buf, '.') && node->buf[node->n_bytes - 1] != '.') { /* xsd:decimal literals without trailing digits, e.g. "5.", can not be written bare in Turtle. We could add a 0 which is prettier, but changes the text and breaks round tripping. @@ -704,7 +700,7 @@ write_literal(SerdWriter* writer, // Return true iff `buf` is a valid prefixed name prefix or suffix static bool -is_name(const uint8_t* buf, const size_t len) +is_name(const char* buf, const size_t len) { // TODO: This is more strict than it should be for (size_t i = 0; i < len; ++i) { @@ -727,12 +723,11 @@ write_uri_node(SerdWriter* const writer, const bool has_scheme = serd_uri_string_has_scheme(node->buf); if (supports_abbrev(writer)) { - if (field == FIELD_PREDICATE && - !strcmp((const char*)node->buf, NS_RDF "type")) { + if (field == FIELD_PREDICATE && !strcmp(node->buf, NS_RDF "type")) { return esink("a", 1, writer); } - if (!strcmp((const char*)node->buf, NS_RDF "nil")) { + if (!strcmp(node->buf, NS_RDF "nil")) { return esink("()", 2, writer); } @@ -835,9 +830,8 @@ write_blank(SerdWriter* const writer, } TRY(st, esink("_:", 2, writer)); - if (writer->bprefix && !strncmp((const char*)node->buf, - (const char*)writer->bprefix, - writer->bprefix_len)) { + if (writer->bprefix && + !strncmp(node->buf, writer->bprefix, writer->bprefix_len)) { TRY(st, esink(node->buf + writer->bprefix_len, node->n_bytes - writer->bprefix_len, @@ -913,12 +907,12 @@ write_list_next(SerdWriter* writer, { SerdStatus st = SERD_SUCCESS; - if (!strcmp((const char*)object->buf, NS_RDF "nil")) { + if (!strcmp(object->buf, NS_RDF "nil")) { TRY(st, write_sep(writer, SEP_LIST_END)); return SERD_FAILURE; } - if (!strcmp((const char*)predicate->buf, NS_RDF "first")) { + if (!strcmp(predicate->buf, NS_RDF "first")) { TRY(st, write_node(writer, object, datatype, lang, FIELD_OBJECT, flags)); } else { TRY(st, write_sep(writer, SEP_LIST_SEP)); @@ -1158,16 +1152,16 @@ serd_writer_set_error_sink(SerdWriter* writer, } void -serd_writer_chop_blank_prefix(SerdWriter* writer, const uint8_t* prefix) +serd_writer_chop_blank_prefix(SerdWriter* writer, const char* prefix) { free(writer->bprefix); writer->bprefix_len = 0; writer->bprefix = NULL; - const size_t prefix_len = prefix ? strlen((const char*)prefix) : 0; + const size_t prefix_len = prefix ? strlen(prefix) : 0; if (prefix_len) { writer->bprefix_len = prefix_len; - writer->bprefix = (uint8_t*)malloc(writer->bprefix_len + 1); + writer->bprefix = (char*)malloc(writer->bprefix_len + 1); memcpy(writer->bprefix, prefix, writer->bprefix_len + 1); } } @@ -1264,8 +1258,8 @@ serd_file_sink(const void* buf, size_t len, void* stream) size_t serd_chunk_sink(const void* buf, size_t len, void* stream) { - SerdChunk* chunk = (SerdChunk*)stream; - uint8_t* new_buf = (uint8_t*)realloc((uint8_t*)chunk->buf, chunk->len + len); + SerdChunk* chunk = (SerdChunk*)stream; + char* new_buf = (char*)realloc((char*)chunk->buf, chunk->len + len); if (new_buf) { memcpy(new_buf + chunk->len, buf, len); chunk->buf = new_buf; @@ -1274,9 +1268,9 @@ serd_chunk_sink(const void* buf, size_t len, void* stream) return len; } -uint8_t* +char* serd_chunk_sink_finish(SerdChunk* stream) { serd_chunk_sink("", 1, stream); - return (uint8_t*)stream->buf; + return (char*)stream->buf; } diff --git a/test/test_env.c b/test/test_env.c index 1de075f3..b084de3b 100644 --- a/test/test_env.c +++ b/test/test_env.c @@ -6,11 +6,8 @@ #include "serd/serd.h" #include <assert.h> -#include <stdint.h> #include <string.h> -#define USTR(s) ((const uint8_t*)(s)) - static SerdStatus count_prefixes(void* handle, const SerdNode* name, const SerdNode* uri) { @@ -24,12 +21,11 @@ count_prefixes(void* handle, const SerdNode* name, const SerdNode* uri) static void test_env(void) { - SerdNode u = serd_node_from_string(SERD_URI, USTR("http://example.org/foo")); - SerdNode b = serd_node_from_string(SERD_CURIE, USTR("invalid")); - SerdNode c = serd_node_from_string(SERD_CURIE, USTR("eg.2:b")); + SerdNode u = serd_node_from_string(SERD_URI, "http://example.org/foo"); + SerdNode b = serd_node_from_string(SERD_CURIE, "invalid"); + SerdNode c = serd_node_from_string(SERD_CURIE, "eg.2:b"); SerdEnv* env = serd_env_new(NULL); - serd_env_set_prefix_from_strings( - env, USTR("eg.2"), USTR("http://example.org/")); + serd_env_set_prefix_from_strings(env, "eg.2", "http://example.org/"); assert(!serd_env_set_base_uri(env, NULL)); assert(serd_env_set_base_uri(env, &SERD_NODE_NULL)); @@ -48,35 +44,34 @@ test_env(void) assert(serd_node_equals(&xnode, &SERD_NODE_NULL)); SerdNode xu = serd_env_expand_node(env, &u); - assert(!strcmp((const char*)xu.buf, "http://example.org/foo")); + assert(!strcmp(xu.buf, "http://example.org/foo")); serd_node_free(&xu); - SerdNode badpre = serd_node_from_string(SERD_CURIE, USTR("hm:what")); + SerdNode badpre = serd_node_from_string(SERD_CURIE, "hm:what"); SerdNode xbadpre = serd_env_expand_node(env, &badpre); assert(serd_node_equals(&xbadpre, &SERD_NODE_NULL)); SerdNode xc = serd_env_expand_node(env, &c); - assert(!strcmp((const char*)xc.buf, "http://example.org/b")); + assert(!strcmp(xc.buf, "http://example.org/b")); serd_node_free(&xc); assert(serd_env_set_prefix(env, &SERD_NODE_NULL, &SERD_NODE_NULL)); - const SerdNode lit = serd_node_from_string(SERD_LITERAL, USTR("hello")); + const SerdNode lit = serd_node_from_string(SERD_LITERAL, "hello"); assert(serd_env_set_prefix(env, &b, &lit)); assert(!serd_env_new(&lit)); - const SerdNode blank = serd_node_from_string(SERD_BLANK, USTR("b1")); + const SerdNode blank = serd_node_from_string(SERD_BLANK, "b1"); const SerdNode xblank = serd_env_expand_node(env, &blank); assert(serd_node_equals(&xblank, &SERD_NODE_NULL)); int n_prefixes = 0; - serd_env_set_prefix_from_strings( - env, USTR("eg.2"), USTR("http://example.org/")); + serd_env_set_prefix_from_strings(env, "eg.2", "http://example.org/"); serd_env_foreach(env, count_prefixes, &n_prefixes); assert(n_prefixes == 1); - SerdNode shorter_uri = serd_node_from_string(SERD_URI, USTR("urn:foo")); + SerdNode shorter_uri = serd_node_from_string(SERD_URI, "urn:foo"); SerdNode prefix_name; assert(!serd_env_qualify(env, &shorter_uri, &prefix_name, &suffix)); diff --git a/test/test_node.c b/test/test_node.c index 1fc7f0bd..6343be5b 100644 --- a/test/test_node.c +++ b/test/test_node.c @@ -14,8 +14,6 @@ #include <stdlib.h> #include <string.h> -#define USTR(s) ((const uint8_t*)(s)) - #ifndef INFINITY # define INFINITY (DBL_MAX + DBL_MAX) #endif @@ -82,10 +80,10 @@ test_double_to_node(void) for (size_t i = 0; i < sizeof(dbl_test_nums) / sizeof(double); ++i) { SerdNode node = serd_node_new_decimal(dbl_test_nums[i], 8); const bool pass = (node.buf && dbl_test_strs[i]) - ? !strcmp((const char*)node.buf, dbl_test_strs[i]) - : ((const char*)node.buf == dbl_test_strs[i]); + ? !strcmp(node.buf, dbl_test_strs[i]) + : (node.buf == dbl_test_strs[i]); assert(pass); - const size_t len = node.buf ? strlen((const char*)node.buf) : 0; + const size_t len = node.buf ? strlen(node.buf) : 0; assert(node.n_bytes == len); serd_node_free(&node); } @@ -103,9 +101,8 @@ test_integer_to_node(void) for (size_t i = 0; i < N_TEST_NUMS; ++i) { SerdNode node = serd_node_new_integer(int_test_nums[i]); - assert(!strcmp((const char*)node.buf, (const char*)int_test_strs[i])); - const size_t len = strlen((const char*)node.buf); - assert(node.n_bytes == len); + assert(!strcmp(node.buf, int_test_strs[i])); + assert(node.n_bytes == strlen(node.buf)); serd_node_free(&node); } @@ -123,7 +120,7 @@ test_blob_to_node(void) SerdNode blob = serd_node_new_blob(data, size, size % 5); - assert(blob.n_bytes == strlen((const char*)blob.buf)); + assert(blob.n_bytes == strlen(blob.buf)); size_t out_size = 0; uint8_t* out = @@ -144,11 +141,12 @@ static void test_node_equals(void) { const uint8_t replacement_char_str[] = {0xEF, 0xBF, 0xBD, 0}; - SerdNode lhs = serd_node_from_string(SERD_LITERAL, replacement_char_str); - SerdNode rhs = serd_node_from_string(SERD_LITERAL, USTR("123")); + SerdNode lhs = + serd_node_from_string(SERD_LITERAL, (const char*)replacement_char_str); + SerdNode rhs = serd_node_from_string(SERD_LITERAL, "123"); assert(!serd_node_equals(&lhs, &rhs)); - SerdNode qnode = serd_node_from_string(SERD_CURIE, USTR("foo:bar")); + SerdNode qnode = serd_node_from_string(SERD_CURIE, "foo:bar"); assert(!serd_node_equals(&lhs, &qnode)); assert(serd_node_equals(&lhs, &lhs)); @@ -159,8 +157,9 @@ test_node_equals(void) static void test_node_from_string(void) { - SerdNode node = - serd_node_from_string(SERD_LITERAL, (const uint8_t*)"hello\""); + SerdNode node = serd_node_from_string(SERD_LITERAL, "hello\""); + assert(node.n_bytes == 6 && node.flags == SERD_HAS_QUOTE && + !strcmp(node.buf, "hello\"")); assert(node.n_bytes == 6 && node.flags == SERD_HAS_QUOTE && !strcmp((const char*)node.buf, "hello\"")); @@ -175,13 +174,13 @@ test_node_from_substring(void) SerdNode empty = serd_node_from_substring(SERD_LITERAL, NULL, 32); assert(!empty.buf && !empty.n_bytes && !empty.flags && !empty.type); - SerdNode a_b = serd_node_from_substring(SERD_LITERAL, USTR("a\"bc"), 3); + SerdNode a_b = serd_node_from_substring(SERD_LITERAL, "a\"bc", 3); assert(a_b.n_bytes == 3 && a_b.flags == SERD_HAS_QUOTE && - !strncmp((const char*)a_b.buf, "a\"b", 3)); + !strncmp(a_b.buf, "a\"b", 3)); - a_b = serd_node_from_substring(SERD_LITERAL, USTR("a\"bc"), 10); + a_b = serd_node_from_substring(SERD_LITERAL, "a\"bc", 10); assert(a_b.n_bytes == 4 && a_b.flags == SERD_HAS_QUOTE && - !strncmp((const char*)a_b.buf, "a\"bc", 4)); + !strncmp(a_b.buf, "a\"bc", 4)); } int diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index cd7ca408..0555e7bb 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -17,8 +17,6 @@ #include <stdlib.h> #include <string.h> -#define USTR(s) ((const uint8_t*)(s)) - typedef struct { size_t n_written; size_t error_offset; @@ -353,10 +351,10 @@ test_read_string(void) assert(serd_reader_get_handle(reader) == rt); // Test reading a string that ends exactly at the end of input (no newline) - const SerdStatus st = serd_reader_read_string( - reader, - USTR("<http://example.org/s> <http://example.org/p> " - "<http://example.org/o> .")); + const SerdStatus st = + serd_reader_read_string(reader, + "<http://example.org/s> <http://example.org/p> " + "<http://example.org/o> ."); assert(!st); assert(rt->n_base == 0); @@ -424,7 +422,7 @@ test_write_errors(void) serd_reader_set_error_sink(reader, quiet_error_sink, NULL); serd_writer_set_error_sink(writer, quiet_error_sink, NULL); - const SerdStatus st = serd_reader_read_string(reader, USTR(doc_string)); + const SerdStatus st = serd_reader_read_string(reader, doc_string); assert(st == SERD_ERR_BAD_WRITE); serd_reader_free(reader); @@ -445,20 +443,20 @@ test_writer(const char* const path) serd_writer_new(SERD_TURTLE, (SerdStyle)0, env, NULL, serd_file_sink, fd); assert(writer); - serd_writer_chop_blank_prefix(writer, USTR("tmp")); + serd_writer_chop_blank_prefix(writer, "tmp"); serd_writer_chop_blank_prefix(writer, NULL); - const SerdNode lit = serd_node_from_string(SERD_LITERAL, USTR("hello")); + const SerdNode lit = serd_node_from_string(SERD_LITERAL, "hello"); assert(serd_writer_set_base_uri(writer, &lit)); assert(serd_writer_set_prefix(writer, &lit, &lit)); assert(serd_writer_end_anon(writer, NULL)); assert(serd_writer_get_env(writer) == env); - uint8_t buf[] = {0x80, 0, 0, 0, 0}; - SerdNode s = serd_node_from_string(SERD_URI, USTR("")); - SerdNode p = serd_node_from_string(SERD_URI, USTR("http://example.org/pred")); - SerdNode o = serd_node_from_string(SERD_LITERAL, buf); + uint8_t buf[] = {0xEF, 0xBF, 0xBD, 0}; + SerdNode s = serd_node_from_string(SERD_URI, ""); + SerdNode p = serd_node_from_string(SERD_URI, "http://example.org/pred"); + SerdNode o = serd_node_from_string(SERD_LITERAL, (char*)buf); // Write 3 invalid statements (should write nothing) const SerdNode* junk[][5] = {{&s, &p, &SERD_NODE_NULL, NULL, NULL}, @@ -478,8 +476,8 @@ test_writer(const char* const path) junk[i][4])); } - const SerdNode t = serd_node_from_string(SERD_URI, USTR("urn:Type")); - const SerdNode l = serd_node_from_string(SERD_LITERAL, USTR("en")); + const SerdNode t = serd_node_from_string(SERD_URI, "urn:Type"); + const SerdNode l = serd_node_from_string(SERD_LITERAL, "en"); const SerdNode* good[][5] = {{&s, &p, &o, NULL, NULL}, {&s, &p, &o, &SERD_NODE_NULL, &SERD_NODE_NULL}, {&s, &p, &o, &t, NULL}, @@ -503,15 +501,15 @@ test_writer(const char* const path) // Write statements with bad UTF-8 (should be replaced) const uint8_t bad_str[] = {0xFF, 0x90, 'h', 'i', 0}; - SerdNode bad_lit = serd_node_from_string(SERD_LITERAL, bad_str); - SerdNode bad_uri = serd_node_from_string(SERD_URI, bad_str); + SerdNode bad_lit = serd_node_from_string(SERD_LITERAL, (const char*)bad_str); + SerdNode bad_uri = serd_node_from_string(SERD_URI, (const char*)bad_str); assert(!serd_writer_write_statement( writer, 0, NULL, &s, &p, &bad_lit, NULL, NULL)); assert(!serd_writer_write_statement( writer, 0, NULL, &s, &p, &bad_uri, NULL, NULL)); // Write 1 valid statement - o = serd_node_from_string(SERD_LITERAL, USTR("hello")); + o = serd_node_from_string(SERD_LITERAL, "hello"); assert(!serd_writer_write_statement(writer, 0, NULL, &s, &p, &o, NULL, NULL)); serd_writer_free(writer); @@ -521,17 +519,17 @@ test_writer(const char* const path) writer = serd_writer_new( SERD_TURTLE, (SerdStyle)0, env, NULL, serd_chunk_sink, &chunk); - o = serd_node_from_string(SERD_URI, USTR("http://example.org/base")); + o = serd_node_from_string(SERD_URI, "http://example.org/base"); assert(!serd_writer_set_base_uri(writer, &o)); serd_writer_free(writer); - uint8_t* out = serd_chunk_sink_finish(&chunk); + char* out = serd_chunk_sink_finish(&chunk); - assert(!strcmp((const char*)out, "@base <http://example.org/base> .\n")); + assert(!strcmp(out, "@base <http://example.org/base> .\n")); serd_free(out); // Test writing empty node - SerdNode nothing = serd_node_from_string(SERD_NOTHING, USTR("")); + SerdNode nothing = serd_node_from_string(SERD_NOTHING, ""); chunk.buf = NULL; chunk.len = 0; @@ -571,9 +569,9 @@ test_reader(const char* path) assert(serd_reader_read_chunk(reader) == SERD_FAILURE); - SerdNode g = serd_node_from_string(SERD_URI, USTR("http://example.org/")); + SerdNode g = serd_node_from_string(SERD_URI, "http://example.org/"); serd_reader_set_default_graph(reader, &g); - serd_reader_add_blank_prefix(reader, USTR("tmp")); + serd_reader_add_blank_prefix(reader, "tmp"); #if defined(__GNUC__) # pragma GCC diagnostic push @@ -584,25 +582,25 @@ test_reader(const char* path) # pragma GCC diagnostic pop #endif - assert(serd_reader_read_file(reader, USTR("http://notafile"))); - assert(serd_reader_read_file(reader, USTR("file:///better/not/exist"))); - assert(serd_reader_read_file(reader, USTR("file://"))); + assert(serd_reader_read_file(reader, "http://notafile")); + assert(serd_reader_read_file(reader, "file:///better/not/exist")); + assert(serd_reader_read_file(reader, "file://")); - const SerdStatus st = serd_reader_read_file(reader, USTR(path)); + const SerdStatus st = serd_reader_read_file(reader, path); assert(!st); assert(rt->n_base == 0); assert(rt->n_prefix == 0); assert(rt->n_statement == 13); assert(rt->n_end == 0); assert(rt->graph && rt->graph->buf && - !strcmp((const char*)rt->graph->buf, "http://example.org/")); + !strcmp(rt->graph->buf, "http://example.org/")); - assert(serd_reader_read_string(reader, USTR("This isn't Turtle at all."))); + assert(serd_reader_read_string(reader, "This isn't Turtle at all.")); // A read of a big page hits EOF then fails to read chunks immediately { FILE* const in = fopen(path, "rb"); - serd_reader_start_stream(reader, in, (const uint8_t*)"test", true); + serd_reader_start_stream(reader, in, "test", true); assert(serd_reader_read_chunk(reader) == SERD_SUCCESS); assert(serd_reader_read_chunk(reader) == SERD_FAILURE); diff --git a/test/test_string.c b/test/test_string.c index 85dccd62..99db03f4 100644 --- a/test/test_string.c +++ b/test/test_string.c @@ -16,23 +16,23 @@ test_strlen(void) const uint8_t str[] = {'"', '5', 0xE2, 0x82, 0xAC, '"', '\n', 0}; SerdNodeFlags flags = 0; - size_t n_bytes = serd_strlen(str, &flags); + size_t n_bytes = serd_strlen((const char*)str, &flags); assert(n_bytes == 7 && flags == (SERD_HAS_QUOTE | SERD_HAS_NEWLINE)); - assert(serd_strlen(str, NULL) == 7); + assert(serd_strlen((const char*)str, NULL) == 7); } static void test_strerror(void) { - const uint8_t* msg = serd_strerror(SERD_SUCCESS); - assert(!strcmp((const char*)msg, "Success")); + const char* msg = serd_strerror(SERD_SUCCESS); + assert(!strcmp(msg, "Success")); for (int i = SERD_FAILURE; i <= SERD_ERR_BAD_TEXT; ++i) { msg = serd_strerror((SerdStatus)i); - assert(strcmp((const char*)msg, "Success")); + assert(strcmp(msg, "Success")); } msg = serd_strerror((SerdStatus)-1); - assert(!strcmp((const char*)msg, "Unknown error")); + assert(!strcmp(msg, "Unknown error")); } int diff --git a/test/test_uri.c b/test/test_uri.c index 2747d008..d22ece63 100644 --- a/test/test_uri.c +++ b/test/test_uri.c @@ -7,31 +7,28 @@ #include <assert.h> #include <stdbool.h> -#include <stdint.h> #include <stdio.h> #include <string.h> -#define USTR(s) ((const uint8_t*)(s)) - static void test_uri_string_has_scheme(void) { - assert(!serd_uri_string_has_scheme(USTR("relative"))); - assert(!serd_uri_string_has_scheme(USTR("http"))); - assert(!serd_uri_string_has_scheme(USTR("5nostartdigit"))); - assert(!serd_uri_string_has_scheme(USTR("+nostartplus"))); - assert(!serd_uri_string_has_scheme(USTR("-nostartminus"))); - assert(!serd_uri_string_has_scheme(USTR(".nostartdot"))); - assert(!serd_uri_string_has_scheme(USTR(":missing"))); - assert(!serd_uri_string_has_scheme(USTR("a/slash/is/not/a/scheme/char"))); - - assert(serd_uri_string_has_scheme(USTR("http://example.org/"))); - assert(serd_uri_string_has_scheme(USTR("https://example.org/"))); - assert(serd_uri_string_has_scheme(USTR("allapha:path"))); - assert(serd_uri_string_has_scheme(USTR("w1thd1g1t5:path"))); - assert(serd_uri_string_has_scheme(USTR("with.dot:path"))); - assert(serd_uri_string_has_scheme(USTR("with+plus:path"))); - assert(serd_uri_string_has_scheme(USTR("with-minus:path"))); + assert(!serd_uri_string_has_scheme("relative")); + assert(!serd_uri_string_has_scheme("http")); + assert(!serd_uri_string_has_scheme("5nostartdigit")); + assert(!serd_uri_string_has_scheme("+nostartplus")); + assert(!serd_uri_string_has_scheme("-nostartminus")); + assert(!serd_uri_string_has_scheme(".nostartdot")); + assert(!serd_uri_string_has_scheme(":missing")); + assert(!serd_uri_string_has_scheme("a/slash/is/not/a/scheme/char")); + + assert(serd_uri_string_has_scheme("http://example.org/")); + assert(serd_uri_string_has_scheme("https://example.org/")); + assert(serd_uri_string_has_scheme("allapha:path")); + assert(serd_uri_string_has_scheme("w1thd1g1t5:path")); + assert(serd_uri_string_has_scheme("with.dot:path")); + assert(serd_uri_string_has_scheme("with+plus:path")); + assert(serd_uri_string_has_scheme("with-minus:path")); } static void @@ -44,14 +41,13 @@ test_file_uri(const char* const hostname, expected_path = path; } - SerdNode node = serd_node_new_file_uri(USTR(path), USTR(hostname), 0); - - uint8_t* out_hostname = NULL; - uint8_t* out_path = serd_file_uri_parse(node.buf, &out_hostname); - assert(!strcmp((const char*)node.buf, expected_uri)); + SerdNode node = serd_node_new_file_uri(path, hostname, 0); + char* out_hostname = NULL; + char* out_path = serd_file_uri_parse(node.buf, &out_hostname); + assert(!strcmp(node.buf, expected_uri)); assert((hostname && out_hostname) || (!hostname && !out_hostname)); - assert(!hostname || !strcmp(hostname, (const char*)out_hostname)); - assert(!strcmp((const char*)out_path, (const char*)expected_path)); + assert(!hostname || !strcmp(hostname, out_hostname)); + assert(!strcmp(out_path, expected_path)); serd_free(out_path); serd_free(out_hostname); @@ -107,8 +103,8 @@ test_uri_parsing(void) // Test tolerance of parsing junk URI escapes - uint8_t* out_path = serd_file_uri_parse(USTR("file:///foo/%0Xbar"), NULL); - assert(!strcmp((const char*)out_path, "/foo/bar")); + char* out_path = serd_file_uri_parse("file:///foo/%0Xbar", NULL); + assert(!strcmp(out_path, "/foo/bar")); serd_free(out_path); } @@ -120,13 +116,13 @@ test_uri_from_string(void) SerdURI base_uri; SerdNode base = - serd_node_new_uri_from_string(USTR("http://example.org/"), NULL, &base_uri); + serd_node_new_uri_from_string("http://example.org/", NULL, &base_uri); SerdNode nil = serd_node_new_uri_from_string(NULL, &base_uri, NULL); - SerdNode nil2 = serd_node_new_uri_from_string(USTR(""), &base_uri, NULL); + SerdNode nil2 = serd_node_new_uri_from_string("", &base_uri, NULL); assert(nil.type == SERD_URI); - assert(!strcmp((const char*)nil.buf, (const char*)base.buf)); + assert(!strcmp(nil.buf, base.buf)); assert(nil2.type == SERD_URI); - assert(!strcmp((const char*)nil2.buf, (const char*)base.buf)); + assert(!strcmp(nil2.buf, base.buf)); serd_node_free(&nil); serd_node_free(&nil2); @@ -155,17 +151,14 @@ check_relative_uri(const char* const uri_string, SerdURI base = SERD_URI_NULL; SerdURI result = SERD_URI_NULL; - SerdNode uri_node = - serd_node_new_uri_from_string(USTR(uri_string), NULL, &uri); - - SerdNode base_node = - serd_node_new_uri_from_string(USTR(base_string), NULL, &base); + SerdNode uri_node = serd_node_new_uri_from_string(uri_string, NULL, &uri); + SerdNode base_node = serd_node_new_uri_from_string(base_string, NULL, &base); SerdNode result_node = SERD_NODE_NULL; if (root_string) { SerdURI root = SERD_URI_NULL; SerdNode root_node = - serd_node_new_uri_from_string(USTR(root_string), NULL, &root); + serd_node_new_uri_from_string(root_string, NULL, &root); result_node = serd_node_new_relative_uri(&uri, &base, &root, &result); serd_node_free(&root_node); @@ -176,7 +169,7 @@ check_relative_uri(const char* const uri_string, assert(!strcmp((const char*)result_node.buf, expected_string)); SerdURI expected = SERD_URI_NULL; - assert(!serd_uri_parse(USTR(expected_string), &expected)); + assert(!serd_uri_parse(expected_string, &expected)); assert(chunk_equals(&result.scheme, &expected.scheme)); assert(chunk_equals(&result.authority, &expected.authority)); assert(chunk_equals(&result.path_base, &expected.path_base)); diff --git a/test/test_writer.c b/test/test_writer.c index 0622dc82..7033db76 100644 --- a/test/test_writer.c +++ b/test/test_writer.c @@ -10,8 +10,6 @@ #include <stdio.h> #include <string.h> -#define USTR(s) ((const uint8_t*)(s)) - static void test_write_long_literal(void) { @@ -22,17 +20,16 @@ test_write_long_literal(void) assert(writer); - SerdNode s = serd_node_from_string(SERD_URI, USTR("http://example.org/s")); - SerdNode p = serd_node_from_string(SERD_URI, USTR("http://example.org/p")); - SerdNode o = - serd_node_from_string(SERD_LITERAL, USTR("hello \"\"\"world\"\"\"!")); + SerdNode s = serd_node_from_string(SERD_URI, "http://example.org/s"); + SerdNode p = serd_node_from_string(SERD_URI, "http://example.org/p"); + SerdNode o = serd_node_from_string(SERD_LITERAL, "hello \"\"\"world\"\"\"!"); assert(!serd_writer_write_statement(writer, 0, NULL, &s, &p, &o, NULL, NULL)); serd_writer_free(writer); serd_env_free(env); - uint8_t* out = serd_chunk_sink_finish(&chunk); + char* out = serd_chunk_sink_finish(&chunk); static const char* const expected = "<http://example.org/s>\n" @@ -59,9 +56,9 @@ test_writer_cleanup(void) SerdWriter* writer = serd_writer_new(SERD_TURTLE, (SerdStyle)0U, env, NULL, null_sink, NULL); - SerdNode s = serd_node_from_string(SERD_URI, USTR("http://example.org/s")); - SerdNode p = serd_node_from_string(SERD_URI, USTR("http://example.org/p")); - SerdNode o = serd_node_from_string(SERD_BLANK, USTR("http://example.org/o")); + SerdNode s = serd_node_from_string(SERD_URI, "http://example.org/s"); + SerdNode p = serd_node_from_string(SERD_URI, "http://example.org/p"); + SerdNode o = serd_node_from_string(SERD_BLANK, "http://example.org/o"); st = serd_writer_write_statement( writer, SERD_ANON_O_BEGIN, NULL, &s, &p, &o, NULL, NULL); @@ -73,7 +70,7 @@ test_writer_cleanup(void) char buf[12] = {0}; snprintf(buf, sizeof(buf), "b%u", i); - SerdNode next_o = serd_node_from_string(SERD_BLANK, USTR(buf)); + SerdNode next_o = serd_node_from_string(SERD_BLANK, buf); st = serd_writer_write_statement( writer, SERD_ANON_O_BEGIN, NULL, &o, &p, &next_o, NULL, NULL); @@ -107,11 +104,11 @@ test_strict_write(void) const uint8_t bad_str[] = {0xFF, 0x90, 'h', 'i', 0}; - SerdNode s = serd_node_from_string(SERD_URI, USTR("http://example.org/s")); - SerdNode p = serd_node_from_string(SERD_URI, USTR("http://example.org/p")); + SerdNode s = serd_node_from_string(SERD_URI, "http://example.org/s"); + SerdNode p = serd_node_from_string(SERD_URI, "http://example.org/p"); - SerdNode bad_lit = serd_node_from_string(SERD_LITERAL, bad_str); - SerdNode bad_uri = serd_node_from_string(SERD_URI, bad_str); + SerdNode bad_lit = serd_node_from_string(SERD_LITERAL, (const char*)bad_str); + SerdNode bad_uri = serd_node_from_string(SERD_URI, (const char*)bad_str); assert(serd_writer_write_statement( writer, 0, NULL, &s, &p, &bad_lit, NULL, NULL) == SERD_ERR_BAD_TEXT); @@ -142,7 +139,7 @@ test_write_error(void) SerdWriter* writer = NULL; SerdStatus st = SERD_SUCCESS; - SerdNode u = serd_node_from_string(SERD_URI, USTR("http://example.com/u")); + SerdNode u = serd_node_from_string(SERD_URI, "http://example.com/u"); writer = serd_writer_new(SERD_TURTLE, (SerdStyle)0, env, NULL, error_sink, NULL); |