diff options
-rw-r--r-- | include/serd/serd.h | 78 | ||||
-rw-r--r-- | src/env.c | 14 | ||||
-rw-r--r-- | src/node.c | 38 | ||||
-rw-r--r-- | src/serdi.c | 4 | ||||
-rw-r--r-- | src/uri.c | 40 | ||||
-rw-r--r-- | src/uri_utils.h | 10 | ||||
-rw-r--r-- | src/writer.c | 24 | ||||
-rw-r--r-- | test/test_uri.c | 16 |
8 files changed, 113 insertions, 111 deletions
diff --git a/include/serd/serd.h b/include/serd/serd.h index 3dacf6a9..bf0740b6 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -217,8 +217,8 @@ typedef size_t (*SerdSink)(const void* SERD_NONNULL buf, /** A parsed URI. - This struct directly refers to chunks in other strings, it does not own any - memory itself. Thus, URIs can be parsed and/or resolved against a base URI + This struct directly refers to slices in other strings, it does not own any + memory itself. This allows some URI operations like resolution to be done in-place without allocating memory. */ typedef struct { @@ -228,9 +228,9 @@ typedef struct { SerdStringView path; ///< Path suffix SerdStringView query; ///< Query SerdStringView fragment; ///< Fragment -} SerdURI; +} SerdURIView; -static const SerdURI SERD_URI_NULL = +static const SerdURIView SERD_URI_NULL = {{NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}}; /** @@ -253,7 +253,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`. @@ -261,15 +261,15 @@ serd_uri_parse(const char* SERD_NONNULL utf8, SerdURI* SERD_NONNULL out); @see [RFC3986 5.2.2](http://tools.ietf.org/html/rfc3986#section-5.2.2) */ 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` @@ -279,11 +279,11 @@ serd_uri_serialise(const SerdURI* SERD_NONNULL uri, used keep up-references ("../") within a certain namespace. */ 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); /** @} @@ -376,15 +376,15 @@ 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. @@ -396,9 +396,9 @@ serd_node_new_uri_from_string(const char* 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 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. @@ -411,9 +411,9 @@ serd_node_new_file_uri(const char* SERD_NONNULL path, memory owned by the new returned node). */ 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. @@ -428,10 +428,10 @@ serd_node_new_uri(const SerdURI* SERD_NONNULL uri, memory owned by the new returned node). */ 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. @@ -593,7 +593,7 @@ serd_env_free(SerdEnv* SERD_NULLABLE env); /// Get the current base URI 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 SerdStatus @@ -832,12 +832,12 @@ typedef enum { /// 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 void @@ -17,7 +17,7 @@ struct SerdEnvImpl { SerdPrefix* prefixes; size_t n_prefixes; SerdNode base_uri_node; - SerdURI base_uri; + SerdURIView base_uri; }; SerdEnv* @@ -52,7 +52,7 @@ serd_env_free(SerdEnv* const env) } const SerdNode* -serd_env_get_base_uri(const SerdEnv* const env, SerdURI* const out) +serd_env_get_base_uri(const SerdEnv* const env, SerdURIView* const out) { if (out) { *out = env->base_uri; @@ -76,8 +76,8 @@ serd_env_set_base_uri(SerdEnv* const env, const SerdNode* const 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 @@ -142,8 +142,8 @@ serd_env_set_prefix(SerdEnv* const env, 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 @@ -229,7 +229,7 @@ serd_env_expand_node(const SerdEnv* const env, const SerdNode* const 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: { @@ -26,7 +26,7 @@ #endif static size_t -serd_uri_string_length(const SerdURI* const uri) +serd_uri_string_length(const SerdURIView* const uri) { size_t len = uri->path_base.len; @@ -104,9 +104,9 @@ serd_node_equals(const SerdNode* const a, const SerdNode* const b) } SerdNode -serd_node_new_uri_from_node(const SerdNode* const uri_node, - const SerdURI* const base, - SerdURI* const out) +serd_node_new_uri_from_node(const SerdNode* const uri_node, + const SerdURIView* const base, + SerdURIView* const out) { return (uri_node->type == SERD_URI && uri_node->buf) ? serd_node_new_uri_from_string(uri_node->buf, base, out) @@ -114,16 +114,16 @@ serd_node_new_uri_from_node(const SerdNode* const uri_node, } SerdNode -serd_node_new_uri_from_string(const char* const str, - const SerdURI* const base, - SerdURI* const out) +serd_node_new_uri_from_string(const char* const str, + const SerdURIView* const base, + SerdURIView* const 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 } @@ -175,9 +175,9 @@ is_dir_sep(const char c) } SerdNode -serd_node_new_file_uri(const char* const path, - const char* const hostname, - SerdURI* const out) +serd_node_new_file_uri(const char* const path, + const char* const hostname, + SerdURIView* const out) { const size_t path_len = strlen(path); const size_t hostname_len = hostname ? strlen(hostname) : 0; @@ -226,11 +226,11 @@ serd_node_new_file_uri(const char* const path, } SerdNode -serd_node_new_uri(const SerdURI* const uri, - const SerdURI* const base, - SerdURI* const out) +serd_node_new_uri(const SerdURIView* const uri, + const SerdURIView* const base, + SerdURIView* const out) { - SerdURI abs_uri = *uri; + SerdURIView abs_uri = *uri; if (base) { serd_uri_resolve(uri, base, &abs_uri); } @@ -252,10 +252,10 @@ serd_node_new_uri(const SerdURI* const uri, } SerdNode -serd_node_new_relative_uri(const SerdURI* const uri, - const SerdURI* const base, - const SerdURI* const root, - SerdURI* const out) +serd_node_new_relative_uri(const SerdURIView* const uri, + const SerdURIView* const base, + const SerdURIView* const root, + SerdURIView* const 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 cb5bbfa0..0ec6366f 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -316,8 +316,8 @@ main(int argc, char** argv) const SerdStyle output_style = choose_style( input_syntax, output_syntax, ascii, bulk_write, full_uris, lax); - 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 @@ -83,7 +83,7 @@ serd_uri_string_has_scheme(const char* utf8) } SerdStatus -serd_uri_parse(const char* const utf8, SerdURI* const out) +serd_uri_parse(const char* const utf8, SerdURIView* const out) { *out = SERD_URI_NULL; @@ -259,9 +259,9 @@ merge(SerdStringView* const base, SerdStringView* const path) /// See http://tools.ietf.org/html/rfc3986#section-5.2.2 void -serd_uri_resolve(const SerdURI* const r, - const SerdURI* const base, - SerdURI* const t) +serd_uri_resolve(const SerdURIView* const r, + const SerdURIView* const base, + SerdURIView* const t) { if (!base->scheme.len) { *t = *r; // Don't resolve against non-absolute URIs @@ -302,10 +302,10 @@ serd_uri_resolve(const SerdURI* const r, /** Write the path of `uri` starting at index `i` */ static size_t -write_path_tail(SerdSink sink, - void* const stream, - const SerdURI* const uri, - const size_t i) +write_path_tail(SerdSink sink, + void* const stream, + const SerdURIView* const uri, + const size_t i) { size_t len = 0; if (i < uri->path_base.len) { @@ -326,10 +326,10 @@ write_path_tail(SerdSink sink, /** Write the path of `uri` relative to the path of `base`. */ static size_t -write_rel_path(SerdSink sink, - void* const stream, - const SerdURI* const uri, - const SerdURI* const base) +write_rel_path(SerdSink sink, + void* const stream, + const SerdURIView* const uri, + const SerdURIView* const base) { const size_t path_len = uri_path_len(uri); const size_t base_len = uri_path_len(base); @@ -367,7 +367,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] != '/') && @@ -376,11 +376,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* const uri, - const SerdURI* const base, - const SerdURI* const root, - SerdSink sink, - void* const stream) +serd_uri_serialise_relative(const SerdURIView* const uri, + const SerdURIView* const base, + const SerdURIView* const root, + SerdSink sink, + void* const stream) { size_t len = 0; const bool relative = @@ -428,7 +428,9 @@ serd_uri_serialise_relative(const SerdURI* const uri, /// See http://tools.ietf.org/html/rfc3986#section-5.3 size_t -serd_uri_serialise(const SerdURI* const uri, SerdSink sink, void* const stream) +serd_uri_serialise(const SerdURIView* const uri, + SerdSink sink, + void* const stream) { return serd_uri_serialise_relative(uri, NULL, NULL, sink, stream); } diff --git a/src/uri_utils.h b/src/uri_utils.h index 9146ee79..b275cf5f 100644 --- a/src/uri_utils.h +++ b/src/uri_utils.h @@ -23,13 +23,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]; @@ -47,7 +47,7 @@ uri_path_at(const SerdURI* uri, size_t i) otherwise it may merely share some leading path components). */ static inline SERD_PURE_FUNC SlashIndexes -uri_rooted_index(const SerdURI* uri, const SerdURI* root) +uri_rooted_index(const SerdURIView* uri, const SerdURIView* root) { SlashIndexes indexes = {SIZE_MAX, SIZE_MAX}; @@ -85,14 +85,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).shared != SIZE_MAX; } /** 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 SlashIndexes indexes = uri_rooted_index(uri, root); return indexes.shared && indexes.shared != SIZE_MAX && diff --git a/src/writer.c b/src/writer.c index 8070392f..19f5d269 100644 --- a/src/writer.c +++ b/src/writer.c @@ -118,8 +118,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; @@ -752,14 +752,14 @@ write_uri_node(SerdWriter* const writer, TRY(st, esink("<", 1, writer)); 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; + SerdURIView* root = rooted ? &writer->root_uri : &writer->base_uri; UriSinkContext ctx = {writer, SERD_SUCCESS}; if (!uri_is_under(&abs_uri, root) || writer->syntax == SERD_NTRIPLES || writer->syntax == SERD_NQUADS) { @@ -1118,12 +1118,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 2a42e5f6..5f1a7f16 100644 --- a/test/test_uri.c +++ b/test/test_uri.c @@ -114,8 +114,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); @@ -147,17 +147,17 @@ check_relative_uri(const char* const uri_string, assert(base_string); assert(expected_string); - SerdURI uri = SERD_URI_NULL; - SerdURI base = SERD_URI_NULL; - SerdURI result = SERD_URI_NULL; + SerdURIView uri = SERD_URI_NULL; + SerdURIView base = SERD_URI_NULL; + SerdURIView result = SERD_URI_NULL; 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 = + SerdURIView root = SERD_URI_NULL; + SerdNode root_node = serd_node_new_uri_from_string(root_string, NULL, &root); result_node = serd_node_new_relative_uri(&uri, &base, &root, &result); @@ -168,7 +168,7 @@ check_relative_uri(const char* const uri_string, assert(!strcmp((const char*)result_node.buf, expected_string)); - SerdURI expected = SERD_URI_NULL; + SerdURIView expected = SERD_URI_NULL; assert(!serd_uri_parse(expected_string, &expected)); assert(chunk_equals(&result.scheme, &expected.scheme)); assert(chunk_equals(&result.authority, &expected.authority)); |