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