diff options
-rw-r--r-- | include/serd/serd.h | 78 | ||||
-rw-r--r-- | src/env.c | 14 | ||||
-rw-r--r-- | src/node.c | 32 | ||||
-rw-r--r-- | src/serdi.c | 4 | ||||
-rw-r--r-- | src/uri.c | 28 | ||||
-rw-r--r-- | src/uri_utils.h | 10 | ||||
-rw-r--r-- | src/writer.c | 28 | ||||
-rw-r--r-- | test/test_uri.c | 18 |
8 files changed, 107 insertions, 105 deletions
diff --git a/include/serd/serd.h b/include/serd/serd.h index d0811911..1a577ee4 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -214,10 +214,10 @@ typedef struct { } SerdError; /** - A parsed URI + A parsed URI. This struct directly refers to slices in other strings, it does not own any - memory itself. Thus, URIs can be parsed and/or resolved against a base URI + memory itself. This allows some URI operations like resolution to be done in-place without allocating memory. */ typedef struct { @@ -227,7 +227,7 @@ typedef struct { SerdStringView path; ///< Path suffix SerdStringView query; ///< Query SerdStringView fragment; ///< Fragment -} SerdURI; +} SerdURIView; /** Syntax style options. @@ -348,7 +348,7 @@ typedef size_t (*SerdSink)(const void* SERD_NONNULL buf, @{ */ -static const SerdURI SERD_URI_NULL = +static const SerdURIView SERD_URI_NULL = {{NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}}; /** @@ -373,7 +373,7 @@ serd_uri_string_has_scheme(const char* SERD_NULLABLE utf8); /// Parse `utf8`, writing result to `out` SERD_API SerdStatus -serd_uri_parse(const char* SERD_NONNULL utf8, SerdURI* SERD_NONNULL out); +serd_uri_parse(const char* SERD_NONNULL utf8, SerdURIView* SERD_NONNULL out); /** Set target `t` to reference `r` resolved against `base`. @@ -382,16 +382,16 @@ serd_uri_parse(const char* SERD_NONNULL utf8, SerdURI* SERD_NONNULL out); */ SERD_API void -serd_uri_resolve(const SerdURI* SERD_NONNULL r, - const SerdURI* SERD_NONNULL base, - SerdURI* SERD_NONNULL t); +serd_uri_resolve(const SerdURIView* SERD_NONNULL r, + const SerdURIView* SERD_NONNULL base, + SerdURIView* SERD_NONNULL t); /// Serialise `uri` with a series of calls to `sink` SERD_API size_t -serd_uri_serialise(const SerdURI* SERD_NONNULL uri, - SerdSink SERD_NONNULL sink, - void* SERD_NONNULL stream); +serd_uri_serialise(const SerdURIView* SERD_NONNULL uri, + SerdSink SERD_NONNULL sink, + void* SERD_NONNULL stream); /** Serialise `uri` relative to `base` with a series of calls to `sink` @@ -402,11 +402,11 @@ serd_uri_serialise(const SerdURI* SERD_NONNULL uri, */ SERD_API size_t -serd_uri_serialise_relative(const SerdURI* SERD_NONNULL uri, - const SerdURI* SERD_NULLABLE base, - const SerdURI* SERD_NULLABLE root, - SerdSink SERD_NONNULL sink, - void* SERD_NONNULL stream); +serd_uri_serialise_relative(const SerdURIView* SERD_NONNULL uri, + const SerdURIView* SERD_NULLABLE base, + const SerdURIView* SERD_NULLABLE root, + SerdSink SERD_NONNULL sink, + void* SERD_NONNULL stream); /** @} @@ -440,16 +440,16 @@ serd_node_from_substring(SerdType type, /// Simple wrapper for serd_node_new_uri() to resolve a URI node SERD_API SerdNode -serd_node_new_uri_from_node(const SerdNode* SERD_NONNULL uri_node, - const SerdURI* SERD_NULLABLE base, - SerdURI* SERD_NULLABLE out); +serd_node_new_uri_from_node(const SerdNode* SERD_NONNULL uri_node, + const SerdURIView* SERD_NULLABLE base, + SerdURIView* SERD_NULLABLE out); /// Simple wrapper for serd_node_new_uri() to resolve a URI string SERD_API SerdNode -serd_node_new_uri_from_string(const char* SERD_NULLABLE str, - const SerdURI* SERD_NULLABLE base, - SerdURI* SERD_NULLABLE out); +serd_node_new_uri_from_string(const char* SERD_NULLABLE str, + const SerdURIView* SERD_NULLABLE base, + SerdURIView* SERD_NULLABLE out); /** Create a new file URI node from a file system path and optional hostname. @@ -462,9 +462,9 @@ serd_node_new_uri_from_string(const char* SERD_NULLABLE str, */ SERD_API SerdNode -serd_node_new_file_uri(const char* SERD_NONNULL path, - const char* SERD_NULLABLE hostname, - SerdURI* SERD_NULLABLE out); +serd_node_new_file_uri(const char* SERD_NONNULL path, + const char* SERD_NULLABLE hostname, + SerdURIView* SERD_NULLABLE out); /** Create a new node by serialising `uri` into a new string. @@ -478,9 +478,9 @@ serd_node_new_file_uri(const char* SERD_NONNULL path, */ SERD_API SerdNode -serd_node_new_uri(const SerdURI* SERD_NONNULL uri, - const SerdURI* SERD_NULLABLE base, - SerdURI* SERD_NULLABLE out); +serd_node_new_uri(const SerdURIView* SERD_NONNULL uri, + const SerdURIView* SERD_NULLABLE base, + SerdURIView* SERD_NULLABLE out); /** Create a new node by serialising `uri` into a new relative URI. @@ -496,10 +496,10 @@ serd_node_new_uri(const SerdURI* SERD_NONNULL uri, */ SERD_API SerdNode -serd_node_new_relative_uri(const SerdURI* SERD_NONNULL uri, - const SerdURI* SERD_NULLABLE base, - const SerdURI* SERD_NULLABLE root, - SerdURI* SERD_NULLABLE out); +serd_node_new_relative_uri(const SerdURIView* SERD_NONNULL uri, + const SerdURIView* SERD_NULLABLE base, + const SerdURIView* SERD_NULLABLE root, + SerdURIView* SERD_NULLABLE out); /** Create a new node by serialising `d` into an xsd:decimal string @@ -641,7 +641,7 @@ serd_env_free(SerdEnv* SERD_NULLABLE env); SERD_API const SerdNode* SERD_NONNULL serd_env_get_base_uri(const SerdEnv* SERD_NONNULL env, - SerdURI* SERD_NULLABLE out); + SerdURIView* SERD_NULLABLE out); /// Set the current base URI SERD_API @@ -868,12 +868,12 @@ serd_reader_free(SerdReader* SERD_NULLABLE reader); /// Create a new RDF writer SERD_API SerdWriter* SERD_ALLOCATED -serd_writer_new(SerdSyntax syntax, - SerdStyle style, - SerdEnv* SERD_NONNULL env, - const SerdURI* SERD_NULLABLE base_uri, - SerdSink SERD_NONNULL ssink, - void* SERD_NULLABLE stream); +serd_writer_new(SerdSyntax syntax, + SerdStyle style, + SerdEnv* SERD_NONNULL env, + const SerdURIView* SERD_NULLABLE base_uri, + SerdSink SERD_NONNULL ssink, + void* SERD_NULLABLE stream); /// Free `writer` SERD_API @@ -30,7 +30,7 @@ struct SerdEnvImpl { SerdPrefix* prefixes; size_t n_prefixes; SerdNode base_uri_node; - SerdURI base_uri; + SerdURIView base_uri; }; SerdEnv* @@ -62,7 +62,7 @@ serd_env_free(SerdEnv* env) } const SerdNode* -serd_env_get_base_uri(const SerdEnv* env, SerdURI* out) +serd_env_get_base_uri(const SerdEnv* env, SerdURIView* out) { if (out) { *out = env->base_uri; @@ -86,8 +86,8 @@ serd_env_set_base_uri(SerdEnv* env, const SerdNode* uri) } // Resolve base URI and create a new node and URI for it - SerdURI base_uri; - SerdNode base_uri_node = + SerdURIView base_uri; + SerdNode base_uri_node = serd_node_new_uri_from_node(uri, &env->base_uri, &base_uri); // Replace the current base URI @@ -143,8 +143,8 @@ serd_env_set_prefix(SerdEnv* env, const SerdNode* name, const SerdNode* uri) serd_env_add(env, name, uri); } else { // Resolve relative URI and create a new node and URI for it - SerdURI abs_uri; - SerdNode abs_uri_node = + SerdURIView abs_uri; + SerdNode abs_uri_node = serd_node_new_uri_from_node(uri, &env->base_uri, &abs_uri); // Set prefix to resolved (absolute) URI @@ -218,7 +218,7 @@ serd_env_expand_node(const SerdEnv* env, const SerdNode* node) case SERD_LITERAL: break; case SERD_URI: { - SerdURI ignored; + SerdURIView ignored; return serd_node_new_uri_from_node(node, &env->base_uri, &ignored); } case SERD_CURIE: { @@ -87,7 +87,7 @@ serd_node_equals(const SerdNode* a, const SerdNode* b) } static size_t -serd_uri_string_length(const SerdURI* uri) +serd_uri_string_length(const SerdURIView* uri) { size_t len = uri->path_base.len; @@ -115,9 +115,9 @@ string_sink(const void* buf, size_t len, void* stream) } SerdNode -serd_node_new_uri_from_node(const SerdNode* uri_node, - const SerdURI* base, - SerdURI* out) +serd_node_new_uri_from_node(const SerdNode* uri_node, + const SerdURIView* base, + SerdURIView* out) { return (uri_node->type == SERD_URI && uri_node->buf) ? serd_node_new_uri_from_string(uri_node->buf, base, out) @@ -125,16 +125,16 @@ serd_node_new_uri_from_node(const SerdNode* uri_node, } SerdNode -serd_node_new_uri_from_string(const char* str, - const SerdURI* base, - SerdURI* out) +serd_node_new_uri_from_string(const char* str, + const SerdURIView* base, + SerdURIView* out) { if (!str || str[0] == '\0') { // Empty URI => Base URI, or nothing if no base is given return base ? serd_node_new_uri(base, NULL, out) : SERD_NODE_NULL; } - SerdURI uri; + SerdURIView uri; serd_uri_parse(str, &uri); return serd_node_new_uri(&uri, base, out); // Resolve/Serialise } @@ -176,7 +176,7 @@ is_uri_path_char(const char c) } SerdNode -serd_node_new_file_uri(const char* path, const char* hostname, SerdURI* out) +serd_node_new_file_uri(const char* path, const char* hostname, SerdURIView* out) { const size_t path_len = strlen(path); const size_t hostname_len = hostname ? strlen(hostname) : 0; @@ -224,9 +224,11 @@ serd_node_new_file_uri(const char* path, const char* hostname, SerdURI* out) } SerdNode -serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out) +serd_node_new_uri(const SerdURIView* uri, + const SerdURIView* base, + SerdURIView* out) { - SerdURI abs_uri = *uri; + SerdURIView abs_uri = *uri; if (base) { serd_uri_resolve(uri, base, &abs_uri); } @@ -248,10 +250,10 @@ serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out) } SerdNode -serd_node_new_relative_uri(const SerdURI* uri, - const SerdURI* base, - const SerdURI* root, - SerdURI* out) +serd_node_new_relative_uri(const SerdURIView* uri, + const SerdURIView* base, + const SerdURIView* root, + SerdURIView* out) { const size_t uri_len = serd_uri_string_length(uri); const size_t base_len = serd_uri_string_length(base); diff --git a/src/serdi.c b/src/serdi.c index 26577ef6..c6ca4fc3 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -308,8 +308,8 @@ main(int argc, char** argv) const SerdStyle output_style = choose_style(input_syntax, output_syntax, ascii, bulk_write, full_uris); - SerdURI base_uri = SERD_URI_NULL; - SerdNode base = SERD_NODE_NULL; + SerdURIView 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 char*)argv[a], NULL, &base_uri); } else if (from_file && in_fd != stdin) { // Use input file URI @@ -94,7 +94,7 @@ serd_uri_string_has_scheme(const char* utf8) } SerdStatus -serd_uri_parse(const char* utf8, SerdURI* out) +serd_uri_parse(const char* utf8, SerdURIView* out) { *out = SERD_URI_NULL; @@ -306,7 +306,7 @@ merge(SerdStringView* base, SerdStringView* path) /// See http://tools.ietf.org/html/rfc3986#section-5.2.2 void -serd_uri_resolve(const SerdURI* r, const SerdURI* base, SerdURI* t) +serd_uri_resolve(const SerdURIView* r, const SerdURIView* base, SerdURIView* t) { if (!base->scheme.len) { *t = *r; // Don't resolve against non-absolute URIs @@ -347,7 +347,7 @@ serd_uri_resolve(const SerdURI* r, const SerdURI* base, SerdURI* t) /** Write the path of `uri` starting at index `i` */ static size_t -write_path_tail(SerdSink sink, void* stream, const SerdURI* uri, size_t i) +write_path_tail(SerdSink sink, void* stream, const SerdURIView* uri, size_t i) { size_t len = 0; if (i < uri->path_base.len) { @@ -368,10 +368,10 @@ write_path_tail(SerdSink sink, void* stream, const SerdURI* uri, size_t i) /** Write the path of `uri` relative to the path of `base`. */ static size_t -write_rel_path(SerdSink sink, - void* stream, - const SerdURI* uri, - const SerdURI* base) +write_rel_path(SerdSink sink, + void* stream, + const SerdURIView* uri, + const SerdURIView* base) { const size_t path_len = uri_path_len(uri); const size_t base_len = uri_path_len(base); @@ -413,7 +413,7 @@ write_rel_path(SerdSink sink, } static uint8_t -serd_uri_path_starts_without_slash(const SerdURI* uri) +serd_uri_path_starts_without_slash(const SerdURIView* uri) { return ((uri->path_base.len || uri->path.len) && ((!uri->path_base.len || uri->path_base.buf[0] != '/') && @@ -422,11 +422,11 @@ serd_uri_path_starts_without_slash(const SerdURI* uri) /// See http://tools.ietf.org/html/rfc3986#section-5.3 size_t -serd_uri_serialise_relative(const SerdURI* uri, - const SerdURI* base, - const SerdURI* root, - SerdSink sink, - void* stream) +serd_uri_serialise_relative(const SerdURIView* uri, + const SerdURIView* base, + const SerdURIView* root, + SerdSink sink, + void* stream) { size_t len = 0; const bool relative = @@ -470,7 +470,7 @@ serd_uri_serialise_relative(const SerdURI* uri, /// See http://tools.ietf.org/html/rfc3986#section-5.3 size_t -serd_uri_serialise(const SerdURI* uri, SerdSink sink, void* stream) +serd_uri_serialise(const SerdURIView* uri, SerdSink sink, void* stream) { return serd_uri_serialise_relative(uri, NULL, NULL, sink, stream); } diff --git a/src/uri_utils.h b/src/uri_utils.h index 735555c4..03d397d2 100644 --- a/src/uri_utils.h +++ b/src/uri_utils.h @@ -31,13 +31,13 @@ slice_equals(const SerdStringView* a, const SerdStringView* b) } static inline size_t -uri_path_len(const SerdURI* uri) +uri_path_len(const SerdURIView* uri) { return uri->path_base.len + uri->path.len; } static inline char -uri_path_at(const SerdURI* uri, size_t i) +uri_path_at(const SerdURIView* uri, size_t i) { if (i < uri->path_base.len) { return uri->path_base.buf[i]; @@ -51,7 +51,7 @@ uri_path_at(const SerdURI* uri, size_t i) or zero if `uri` is not under `root`. */ static inline SERD_PURE_FUNC size_t -uri_rooted_index(const SerdURI* uri, const SerdURI* root) +uri_rooted_index(const SerdURIView* uri, const SerdURIView* root) { if (!root || !root->scheme.len || !slice_equals(&root->scheme, &uri->scheme) || @@ -81,14 +81,14 @@ uri_rooted_index(const SerdURI* uri, const SerdURI* root) /** Return true iff `uri` shares path components with `root` */ static inline SERD_PURE_FUNC bool -uri_is_related(const SerdURI* uri, const SerdURI* root) +uri_is_related(const SerdURIView* uri, const SerdURIView* root) { return uri_rooted_index(uri, root) > 0; } /** Return true iff `uri` is within the base of `root` */ static inline SERD_PURE_FUNC bool -uri_is_under(const SerdURI* uri, const SerdURI* root) +uri_is_under(const SerdURIView* uri, const SerdURIView* root) { const size_t index = uri_rooted_index(uri, root); return index > 0 && uri->path.len > index; diff --git a/src/writer.c b/src/writer.c index e97af301..c365f23c 100644 --- a/src/writer.c +++ b/src/writer.c @@ -96,8 +96,8 @@ struct SerdWriterImpl { SerdStyle style; SerdEnv* env; SerdNode root_node; - SerdURI root_uri; - SerdURI base_uri; + SerdURIView root_uri; + SerdURIView base_uri; SerdStack anon_stack; SerdByteSink byte_sink; SerdErrorSink error_sink; @@ -151,7 +151,7 @@ w_err(SerdWriter* writer, SerdStatus st, const char* fmt, ...) } static inline SERD_PURE_FUNC WriteContext* -anon_stack_top(SerdWriter* writer) + anon_stack_top(SerdWriter* writer) { assert(!serd_stack_is_empty(&writer->anon_stack)); return (WriteContext*)(writer->anon_stack.buf + writer->anon_stack.size - @@ -600,14 +600,14 @@ write_uri_node(SerdWriter* const writer, write_sep(writer, SEP_URI_BEGIN); if (writer->style & SERD_STYLE_RESOLVED) { - SerdURI in_base_uri; - SerdURI uri; - SerdURI abs_uri; + SerdURIView in_base_uri; + SerdURIView uri; + SerdURIView abs_uri; serd_env_get_base_uri(writer->env, &in_base_uri); serd_uri_parse(node->buf, &uri); serd_uri_resolve(&uri, &in_base_uri, &abs_uri); - bool rooted = uri_is_under(&writer->base_uri, &writer->root_uri); - SerdURI* root = rooted ? &writer->root_uri : &writer->base_uri; + bool rooted = uri_is_under(&writer->base_uri, &writer->root_uri); + SerdURIView* root = rooted ? &writer->root_uri : &writer->base_uri; if (!uri_is_under(&abs_uri, root) || writer->syntax == SERD_NTRIPLES || writer->syntax == SERD_NQUADS) { serd_uri_serialise(&abs_uri, uri_sink, writer); @@ -952,12 +952,12 @@ serd_writer_finish(SerdWriter* writer) } SerdWriter* -serd_writer_new(SerdSyntax syntax, - SerdStyle style, - SerdEnv* env, - const SerdURI* base_uri, - SerdSink ssink, - void* stream) +serd_writer_new(SerdSyntax syntax, + SerdStyle style, + SerdEnv* env, + const SerdURIView* base_uri, + SerdSink ssink, + void* stream) { const WriteContext context = WRITE_CONTEXT_NULL; SerdWriter* writer = (SerdWriter*)calloc(1, sizeof(SerdWriter)); diff --git a/test/test_uri.c b/test/test_uri.c index 8c3ec5a4..e6933759 100644 --- a/test/test_uri.c +++ b/test/test_uri.c @@ -69,8 +69,8 @@ test_uri_from_string(void) SerdNode nonsense = serd_node_new_uri_from_string(NULL, NULL, NULL); assert(nonsense.type == SERD_NOTHING); - SerdURI base_uri; - SerdNode base = + SerdURIView base_uri; + SerdNode base = 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("", &base_uri, NULL); @@ -87,16 +87,16 @@ test_uri_from_string(void) static void test_relative_uri(void) { - SerdURI base_uri; - SerdNode base = + SerdURIView base_uri; + SerdNode base = serd_node_new_uri_from_string("http://example.org/", NULL, &base_uri); SerdNode abs = serd_node_from_string(SERD_URI, "http://example.org/foo/bar"); - SerdURI abs_uri; + SerdURIView abs_uri; serd_uri_parse(abs.buf, &abs_uri); - SerdURI rel_uri; - SerdNode rel = + SerdURIView rel_uri; + SerdNode rel = serd_node_new_relative_uri(&abs_uri, &base_uri, NULL, &rel_uri); assert(!strcmp(rel.buf, "/foo/bar")); @@ -107,8 +107,8 @@ test_relative_uri(void) serd_node_new_relative_uri(&base_uri, &abs_uri, &abs_uri, NULL); assert(!strcmp(noup.buf, "http://example.org/")); - SerdNode x = serd_node_from_string(SERD_URI, "http://example.org/foo/x"); - SerdURI x_uri; + SerdNode x = serd_node_from_string(SERD_URI, "http://example.org/foo/x"); + SerdURIView x_uri; serd_uri_parse(x.buf, &x_uri); SerdNode x_rel = serd_node_new_relative_uri(&x_uri, &abs_uri, &abs_uri, NULL); |