IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
uri_tools.hxx
Go to the documentation of this file.
1#pragma once
2#include <ice/string.hxx>
3
4namespace ice::uri
5{
6
7 constexpr bool get_scheme_size(ice::String raw_uri, ice::u8& out_size) noexcept
8 {
9 ice::nindex const scheme_end = raw_uri.find_first_of(':');
10 if (scheme_end.is_valid())
11 {
12 out_size = scheme_end.u8() + 1;
13 }
14 return scheme_end.is_valid();
15 }
16
17 constexpr bool get_authority_sizes(
19 ice::u8& out_authority,
20 ice::u8& out_user,
21 ice::u8& out_host,
22 ice::u8& out_port
23 ) noexcept
24 {
25 if (uri[0] == '/' && uri[1] == '/')
26 {
27 ice::nindex const authority_end = uri.find_first_of('/', 2);
28 ICE_ASSERT_CORE(authority_end.is_valid());
29 if (authority_end.is_valid() == false)
30 {
31 return false;
32 }
33
34 out_authority = authority_end.u8();
35
36 ice::nindex offset = 0;
37 ice::String const authority_uri = uri.substr(2, authority_end.u32() - 2);
38 ice::nindex const authority_user = authority_uri.find_first_of('@', offset);
39 if (authority_user.is_valid())
40 {
41 // Include the '@' character in the size, since it can be easily removed in the 'userinfo()' method
42 // and helps with calculations.
43 offset = out_user = (authority_user - offset).u8() + 1;
44 }
45
46 ice::nindex const authority_port = authority_uri.find_first_of(':', offset);
47 if (authority_port.is_valid())
48 {
49 // If we have a port set the length of host to up to the ':' character
50 out_host = (authority_port - offset).u8();
51
52 // After that it's the 'port' value (without the ':') character
53 // Because the host needs to exist we can always add +1, and keep the port size the actual size.
54 out_port = (authority_uri.size() - (authority_port + 1)).u8();
55 }
56 else
57 {
58 // The rest of the authority string is the host
59 out_host = (authority_uri.size() - offset).u8();
60 }
61 }
62 return true;
63 }
64
67 ice::u8& out_path,
68 ice::u8& out_query,
69 ice::u8& out_fragment
70 ) noexcept
71 {
72 ice::nindex path_separator = uri.find_first_of("?#");
73 if (path_separator == nindex_none)
74 {
75 out_path = uri.size().u8();
76 return out_path > 0;
77 }
78
79 // We continue after assigning path length
80 out_path = path_separator.u8();
81
82 // Do we have a query?
83 if (uri[path_separator] == '?')
84 {
85 // Get the next separator if necessary
86 path_separator = uri.find_last_of('#');
87 if (path_separator == nindex_none)
88 {
89 // We take te remaining query with the starting '?' character
90 out_query = (uri.size() - out_path).u8();
91 return true;
92 }
93
94 // Take everything up to '#' including the '?' character.
95 out_query = (path_separator - out_path).u8();
96 }
97 else
98 {
99 // URI specification does not support queries after the '#fragment'
100 ICE_ASSERT_CORE(uri.find_first_of('?', path_separator + 1) == ice::nindex_none);
101 }
102
103 // Take everything remaining including the '#' character.
104 out_fragment = (uri.size() - path_separator).u8();
105 return true;
106 }
107
108} // namespace ice::uri
#define ICE_ASSERT_CORE(expression)
Definition assert_core.hxx:43
Definition uri_tools.hxx:5
constexpr bool get_path_query_fragment_sizes(ice::String uri, ice::u8 &out_path, ice::u8 &out_query, ice::u8 &out_fragment) noexcept
Definition uri_tools.hxx:65
constexpr bool get_scheme_size(ice::String raw_uri, ice::u8 &out_size) noexcept
Definition uri_tools.hxx:7
constexpr bool get_authority_sizes(ice::String uri, ice::u8 &out_authority, ice::u8 &out_user, ice::u8 &out_host, ice::u8 &out_port) noexcept
Definition uri_tools.hxx:17
ice::BasicString< char > String
Definition string.hxx:82
std::uint8_t u8
Definition types.hxx:24
static constexpr ice::nindex_invalid_t nindex_none
Definition nindex.hxx:56
constexpr auto size() const noexcept -> SizeType
Definition string.hxx:57
Definition nindex.hxx:13
constexpr auto u32() const noexcept
Definition nvalue.hxx:113
constexpr auto u8() const noexcept
Definition nvalue.hxx:111
constexpr bool is_valid(this Self self) noexcept
Definition nvalue.hxx:86
constexpr auto find_first_of(this Self const &self, typename Self::CharType character_value, ice::nindex start=0) noexcept -> ice::nindex
Definition readonly_operations.hxx:75