aboutsummaryrefslogtreecommitdiffstats
path: root/src/uri_utils.h
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-04-29 14:07:29 +0200
committerDavid Robillard <d@drobilla.net>2018-05-27 18:21:57 +0200
commitaa41376304f135bfc54d2b5c16fa8ecd7302ad24 (patch)
tree76f1eba1a5f7dd5ee1e56c74840b8babc4a0e474 /src/uri_utils.h
parent9252a3f52012e6199b7a27b5c329c226614cc127 (diff)
downloadserd-aa41376304f135bfc54d2b5c16fa8ecd7302ad24.tar.gz
serd-aa41376304f135bfc54d2b5c16fa8ecd7302ad24.tar.bz2
serd-aa41376304f135bfc54d2b5c16fa8ecd7302ad24.zip
Clean up and separate internal headers
Diffstat (limited to 'src/uri_utils.h')
-rw-r--r--src/uri_utils.h81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/uri_utils.h b/src/uri_utils.h
new file mode 100644
index 00000000..35430250
--- /dev/null
+++ b/src/uri_utils.h
@@ -0,0 +1,81 @@
+/*
+ Copyright 2011-2018 David Robillard <http://drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#ifndef SERD_URI_UTILS_H
+#define SERD_URI_UTILS_H
+
+#include "string_utils.h"
+
+static inline bool
+slice_equals(const SerdSlice* a, const SerdSlice* b)
+{
+ return a->len == b->len
+ && !strncmp((const char*)a->buf, (const char*)b->buf, a->len);
+}
+
+static inline size_t
+uri_path_len(const SerdURI* uri)
+{
+ return uri->path_base.len + uri->path.len;
+}
+
+static inline char
+uri_path_at(const SerdURI* uri, size_t i)
+{
+ if (i < uri->path_base.len) {
+ return uri->path_base.buf[i];
+ } else {
+ return uri->path.buf[i - uri->path_base.len];
+ }
+}
+
+/** Return true iff `uri` is within the base of `root` */
+static inline bool
+uri_is_under(const SerdURI* uri, const SerdURI* root)
+{
+ if (!root || !root->scheme.len ||
+ !slice_equals(&root->scheme, &uri->scheme) ||
+ !slice_equals(&root->authority, &uri->authority)) {
+ return false;
+ }
+
+ bool differ = false;
+ const size_t path_len = uri_path_len(uri);
+ const size_t root_len = uri_path_len(root);
+ for (size_t i = 0; i < path_len && i < root_len; ++i) {
+ if (uri_path_at(uri, i) != uri_path_at(root, i)) {
+ differ = true;
+ }
+ if (differ && uri_path_at(root, i) == '/') {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+static inline bool
+is_uri_scheme_char(const uint8_t c)
+{
+ switch (c) {
+ case ':': case '+': case '-': case '.':
+ return true;
+ default:
+ return is_alpha(c) || is_digit(c);
+ }
+}
+
+#endif // SERD_URI_UTILS_H