diff options
author | David Robillard <d@drobilla.net> | 2021-02-28 14:24:24 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:07 -0500 |
commit | b7cce4a24dbc544129a9fabc44cb22025767f10b (patch) | |
tree | f085e200dc7b4b8a67ffebc191e752d0be366d53 | |
parent | 154e33ef21d175bb97263a8318dc0cc461a64321 (diff) | |
download | serd-b7cce4a24dbc544129a9fabc44cb22025767f10b.tar.gz serd-b7cce4a24dbc544129a9fabc44cb22025767f10b.tar.bz2 serd-b7cce4a24dbc544129a9fabc44cb22025767f10b.zip |
Make serd_uri_string_length() precise and add it to public API
-rw-r--r-- | include/serd/uri.h | 15 | ||||
-rw-r--r-- | src/node.c | 27 | ||||
-rw-r--r-- | src/uri.c | 38 |
3 files changed, 58 insertions, 22 deletions
diff --git a/include/serd/uri.h b/include/serd/uri.h index 93409075..3170fa89 100644 --- a/include/serd/uri.h +++ b/include/serd/uri.h @@ -123,6 +123,17 @@ SERD_PURE_API bool serd_uri_is_within(SerdURIView r, SerdURIView base); /** + Return the length of `uri` as a string. + + This can be used to get the expected number of bytes that will be written by + serd_write_uri(). + + @return A string length in bytes, not including the null terminator. +*/ +SERD_PURE_API size_t +serd_uri_string_length(SerdURIView uri); + +/** Write `uri` as a string to `sink`. This will call `sink` several times to emit the URI. @@ -130,7 +141,9 @@ serd_uri_is_within(SerdURIView r, SerdURIView base); @param uri URI to write as a string. @param sink Sink to write string output to. @param stream Opaque user argument to pass to `sink`. - @return The number of bytes written. + + @return The number of bytes written, which is less than + `serd_uri_string_length(uri)` on error. */ SERD_API size_t serd_write_uri(SerdURIView uri, @@ -52,25 +52,6 @@ static SerdNode* serd_new_from_uri(SerdURIView uri, SerdURIView base); static size_t -serd_uri_string_length(const SerdURIView* const uri) -{ - size_t len = uri->path_prefix.length; - -#define ADD_LEN(field, n_delims) \ - if ((field).length) { \ - len += (field).length + (n_delims); \ - } - - ADD_LEN(uri->path, 1) // + possible leading '/' - ADD_LEN(uri->scheme, 1) // + trailing ':' - ADD_LEN(uri->authority, 2) // + leading '//' - ADD_LEN(uri->query, 1) // + leading '?' - ADD_LEN(uri->fragment, 1) // + leading '#' - - return len + 2; // + 2 for authority '//' -} - -static size_t string_sink(const void* const buf, const size_t size, const size_t nmemb, @@ -492,11 +473,13 @@ serd_new_uri(const SerdStringView string) SerdNode* serd_new_parsed_uri(const SerdURIView uri) { - const size_t len = serd_uri_string_length(&uri); + const size_t len = serd_uri_string_length(uri); SerdNode* const node = serd_node_malloc(len, 0, SERD_URI); char* ptr = serd_node_buffer(node); const size_t actual_len = serd_write_uri(uri, string_sink, &ptr); + assert(actual_len == len); + serd_node_buffer(node)[actual_len] = '\0'; node->length = actual_len; @@ -508,11 +491,13 @@ static SerdNode* serd_new_from_uri(const SerdURIView uri, const SerdURIView base) { const SerdURIView abs_uri = serd_resolve_uri(uri, base); - const size_t len = serd_uri_string_length(&abs_uri); + const size_t len = serd_uri_string_length(abs_uri); SerdNode* node = serd_node_malloc(len, 0, SERD_URI); char* ptr = serd_node_buffer(node); const size_t actual_len = serd_write_uri(abs_uri, string_sink, &ptr); + assert(actual_len == len); + serd_node_buffer(node)[actual_len] = '\0'; node->length = actual_len; @@ -396,6 +396,44 @@ serd_uri_is_within(const SerdURIView uri, const SerdURIView base) return true; } +size_t +serd_uri_string_length(const SerdURIView uri) +{ + size_t len = 0; + + if (uri.scheme.data) { + len += uri.scheme.length + 1; + } + + if (uri.authority.data) { + const bool needs_extra_slash = + (uri.authority.length > 0 && uri_path_len(&uri) > 0 && + uri_path_at(&uri, 0) != '/'); + + len += 2 + uri.authority.length + needs_extra_slash; + } + + if (uri.path_prefix.data) { + len += uri.path_prefix.length; + } else if (uri.path_prefix.length) { + len += 3 * uri.path_prefix.length; + } + + if (uri.path.data) { + len += uri.path.length; + } + + if (uri.query.data) { + len += uri.query.length + 1; + } + + if (uri.fragment.data) { + len += uri.fragment.length; + } + + return len; +} + /// See http://tools.ietf.org/html/rfc3986#section-5.3 size_t serd_write_uri(const SerdURIView uri, |