diff options
author | David Robillard <d@drobilla.net> | 2016-03-16 16:21:20 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 16:27:02 -0500 |
commit | 84bab08585dec858807a6130bd2d71f304b889f0 (patch) | |
tree | ea78338414095c2c871944a0135028e1399b12fe /src/uri.c | |
parent | caa74939cba8b1cd357e553efca9bec5074b1c53 (diff) | |
download | serd-84bab08585dec858807a6130bd2d71f304b889f0.tar.gz serd-84bab08585dec858807a6130bd2d71f304b889f0.tar.bz2 serd-84bab08585dec858807a6130bd2d71f304b889f0.zip |
Use char* for strings in public API
The constant casting just makes user code a mess, for no benefit.
Diffstat (limited to 'src/uri.c')
-rw-r--r-- | src/uri.c | 58 |
1 files changed, 28 insertions, 30 deletions
@@ -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 == '/') { |