diff options
author | David Robillard <d@drobilla.net> | 2016-03-16 16:21:20 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2021-03-07 15:32:23 -0500 |
commit | a29581d3ba664175c459e20e6c86be09707fde6e (patch) | |
tree | d11253ca88b09d7a768740b00332a4d780e3852d /src/uri.c | |
parent | 6e856d3e7a9c3162b9af350d5cec8a3f6bb94ee2 (diff) | |
download | serd-a29581d3ba664175c459e20e6c86be09707fde6e.tar.gz serd-a29581d3ba664175c459e20e6c86be09707fde6e.tar.bz2 serd-a29581d3ba664175c459e20e6c86be09707fde6e.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 | 61 |
1 files changed, 29 insertions, 32 deletions
@@ -25,24 +25,23 @@ #include <stdlib.h> #include <string.h> -uint8_t* -serd_file_uri_parse(const uint8_t* uri, uint8_t** hostname) +char* +serd_file_uri_parse(const char* uri, char** 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); + *hostname = (char*)calloc((size_t)(path - auth + 1), 1); memcpy(*hostname, auth, (size_t)(path - auth)); } } @@ -53,14 +52,14 @@ serd_file_uri_parse(const uint8_t* uri, uint8_t** hostname) } SerdBuffer buffer = {NULL, 0}; - for (const uint8_t* s = path; *s; ++s) { + for (const char* s = path; *s; ++s) { if (*s == '%') { if (*(s + 1) == '%') { serd_buffer_sink("%", 1, &buffer); ++s; } else if (is_hexdig(*(s + 1)) && is_hexdig(*(s + 2))) { - const uint8_t code[3] = {*(s + 1), *(s + 2), 0}; - const uint8_t c = (uint8_t)strtoul((const char*)code, NULL, 16); + const char code[3] = {*(s + 1), *(s + 2), 0}; + const char c = (char)strtoul((const char*)code, NULL, 16); serd_buffer_sink(&c, 1, &buffer); s += 2; } else { @@ -74,14 +73,14 @@ serd_file_uri_parse(const uint8_t* uri, uint8_t** 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; } @@ -95,11 +94,11 @@ serd_uri_string_has_scheme(const uint8_t* utf8) } SerdStatus -serd_uri_parse(const uint8_t* utf8, SerdURI* out) +serd_uri_parse(const char* utf8, SerdURI* 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 ] @@ -107,7 +106,7 @@ serd_uri_parse(const uint8_t* utf8, SerdURI* 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 '/': @@ -139,7 +138,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; @@ -169,7 +168,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; @@ -187,7 +186,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; } @@ -217,11 +216,11 @@ 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* path, size_t len, size_t* up) +static const char* +remove_dot_segments(const char* path, size_t len, size_t* up) { - const uint8_t* begin = path; - const uint8_t* const end = path + len; + const char* begin = path; + const char* const end = path + len; *up = 0; while (begin < end) { @@ -246,12 +245,12 @@ remove_dot_segments(const uint8_t* path, size_t len, size_t* up) } break; case '\0': - return ++begin; // Chop input "." + ++begin; // Chop input "." + // fallthru default: return begin; } break; - case '/': switch (begin[1]) { case '.': @@ -269,9 +268,7 @@ remove_dot_segments(const uint8_t* path, size_t len, size_t* up) default: return begin; } - } - return begin; - + } // else fall through default: return begin; // Finished chopping dot components } @@ -284,13 +281,13 @@ remove_dot_segments(const uint8_t* path, size_t len, size_t* up) static void merge(SerdChunk* base, SerdChunk* 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 == '/') { |