LMMS
Loading...
Searching...
No Matches
uri_utils.h
Go to the documentation of this file.
1/*
2 Copyright 2011-2020 David Robillard <d@drobilla.net>
3
4 Permission to use, copy, modify, and/or distribute this software for any
5 purpose with or without fee is hereby granted, provided that the above
6 copyright notice and this permission notice appear in all copies.
7
8 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*/
16
17#ifndef SERD_URI_UTILS_H
18#define SERD_URI_UTILS_H
19
20#include "serd/serd.h"
21
22#include "string_utils.h"
23
24#include <stdbool.h>
25#include <stdint.h>
26#include <string.h>
27
28static inline bool
30{
31 return a->len == b->len &&
32 !strncmp((const char*)a->buf, (const char*)b->buf, a->len);
33}
34
35static inline size_t
37{
38 return uri->path_base.len + uri->path.len;
39}
40
41static inline uint8_t
42uri_path_at(const SerdURI* uri, size_t i)
43{
44 return (i < uri->path_base.len) ? uri->path_base.buf[i]
45 : uri->path.buf[i - uri->path_base.len];
46}
47
52static inline SERD_PURE_FUNC size_t
53uri_rooted_index(const SerdURI* uri, const SerdURI* root)
54{
55 if (!root || !root->scheme.len ||
56 !chunk_equals(&root->scheme, &uri->scheme) ||
57 !chunk_equals(&root->authority, &uri->authority)) {
58 return 0;
59 }
60
61 bool differ = false;
62 const size_t path_len = uri_path_len(uri);
63 const size_t root_len = uri_path_len(root);
64 size_t last_root_slash = 0;
65 for (size_t i = 0; i < path_len && i < root_len; ++i) {
66 const uint8_t u = uri_path_at(uri, i);
67 const uint8_t r = uri_path_at(root, i);
68
69 differ = differ || u != r;
70 if (r == '/') {
71 last_root_slash = i;
72 if (differ) {
73 return 0;
74 }
75 }
76 }
77
78 return last_root_slash + 1;
79}
80
82static inline SERD_PURE_FUNC bool
83uri_is_related(const SerdURI* uri, const SerdURI* root)
84{
85 return uri_rooted_index(uri, root) > 0;
86}
87
89static inline SERD_PURE_FUNC bool
90uri_is_under(const SerdURI* uri, const SerdURI* root)
91{
92 const size_t index = uri_rooted_index(uri, root);
93 return index > 0 && uri->path.len > index;
94}
95
96static inline bool
98{
99 switch (c) {
100 case ':':
101 case '+':
102 case '-':
103 case '.':
104 return true;
105 default:
106 return is_alpha(c) || is_digit(c);
107 }
108}
109
110#endif // SERD_URI_UTILS_H
uint8_t a
Definition Spc_Cpu.h:141
struct huft * u[BMAX]
Definition inflate.c:1583
register unsigned i
Definition inflate.c:1575
#define SERD_PURE_FUNC
Definition serd.h:42
static SordNode * uri(SordWorld *world, int num)
Definition sord_test.c:47
unsigned char uint8_t
Definition mid.cpp:98
static bool is_digit(const int c)
Definition string_utils.h:45
static bool is_alpha(const int c)
Definition string_utils.h:38
An unterminated string fragment.
Definition serd.h:208
size_t len
Length of chunk in bytes.
Definition serd.h:210
Definition serd.h:230
SerdChunk authority
Authority.
Definition serd.h:232
SerdChunk scheme
Scheme.
Definition serd.h:231
return c
Definition crypt.c:175
int r
Definition crypt.c:458
b
Definition crypt.c:628
static SERD_PURE_FUNC size_t uri_rooted_index(const SerdURI *uri, const SerdURI *root)
Definition uri_utils.h:53
static uint8_t uri_path_at(const SerdURI *uri, size_t i)
Definition uri_utils.h:42
static SERD_PURE_FUNC bool uri_is_under(const SerdURI *uri, const SerdURI *root)
Definition uri_utils.h:90
static size_t uri_path_len(const SerdURI *uri)
Definition uri_utils.h:36
static SERD_PURE_FUNC bool uri_is_related(const SerdURI *uri, const SerdURI *root)
Definition uri_utils.h:83
static bool is_uri_scheme_char(const int c)
Definition uri_utils.h:97
static bool chunk_equals(const SerdChunk *a, const SerdChunk *b)
Definition uri_utils.h:29