43 using const_iterator = std::string_view::const_iterator;
64 [[nodiscard]]
constexpr std::optional<std::string>
const& userinfo()
const noexcept
69 constexpr authority_type& set_userinfo(std::optional<std::string>
const& rhs)
noexcept
75 [[nodiscard]]
constexpr std::string const& host()
const noexcept
83 _host = to_lower(rhs);
87 [[nodiscard]]
constexpr std::optional<std::string>
const& port()
const noexcept
92 constexpr authority_type& set_port(std::optional<std::string>
const& rhs)
106 [[nodiscard]]
constexpr friend size_t to_string_size(
authority_type const& rhs)
noexcept
110 size += rhs._userinfo->size() + 1;
112 size += rhs._host.size();
114 size += rhs._port->size() + 1;
124 r += URI::encode<HI_SUB_DELIM, ':'>(*rhs._userinfo);
128 if (rhs._host.empty() or rhs._host.front() !=
'[') {
129 r += URI::encode<HI_SUB_DELIM>(rhs._host);
131 r += URI::encode<HI_SUB_DELIM, '[', ']', ':'>(rhs._host);
143 std::optional<std::string> _userinfo = {};
145 std::optional<std::string> _port = {};
147 [[nodiscard]]
constexpr static void validate_host(std::string_view str)
149 if (str.starts_with(
'[') and not str.ends_with(
']')) {
150 throw uri_error(
"The host-component starts with '[' has missing ']' at end");
151 }
else if (str.ends_with(
']') and str.starts_with(
'[')) {
152 throw uri_error(
"The host-component ends with ']' has missing '[' at start");
156 [[nodiscard]]
constexpr static void validate_port(std::string_view str)
159 if (not(c >=
'0' and c <=
'9')) {
160 throw uri_error(
"The port-component contains a non-digit.");
172 [[nodiscard]]
constexpr const_iterator parse_userinfo(const_iterator first, const_iterator last)
noexcept
174 for (
auto it = first; it != last; ++it) {
175 if (
hilet c = *it; c ==
'@') {
191 [[nodiscard]]
constexpr const_iterator parse_host(const_iterator first, const_iterator last)
noexcept
209 for (; it != last; ++it) {
210 if (
hilet c = *it; c ==
':') {
223 [[nodiscard]]
constexpr const_iterator parse_port(const_iterator first, const_iterator last)
noexcept
229 constexpr void parse(std::string_view rhs)
noexcept
231 auto first = rhs.cbegin();
232 auto last = rhs.cend();
233 auto it = parse_userinfo(first, last);
234 it = parse_host(it, last);
236 if (it != last and *it ==
':') {
237 it = parse_port(++it, last);
274 constexpr path_type()
noexcept =
default;
282 auto r = make_vector<std::string>(std::views::transform(std::views::split(str, std::string_view{
"/"}), [](
auto&& x) {
283 return URI::decode(std::string_view{std::ranges::begin(x), std::ranges::end(x)});
286 if (r.size() == 1 and r.front().empty()) {
290 if (not r.empty() and (r.back() ==
"." or r.back() ==
".." or r.back() ==
"**")) {
303 [[nodiscard]]
constexpr bool absolute()
const noexcept
308 [[nodiscard]]
constexpr bool double_absolute()
const noexcept
310 return size() >= 3 and (*
this)[0].
empty() and (*
this)[1].empty();
313 [[nodiscard]]
constexpr bool is_root()
const noexcept
315 return size() == 2 and (*
this)[0].
empty() and (*
this)[1].empty();
318 [[nodiscard]]
constexpr std::optional<std::string> filename()
const noexcept
349 if (base_has_authority and base.empty()) {
355 if (not base.empty()) {
359 base.insert(base.cend(), ref.cbegin(), ref.cend());
361 if (base.size() == 1 and base.front().empty()) {
397 for (
auto it = path.cbegin(); it != path.cend();) {
402 }
else if (*it ==
"..") {
403 if (it == path.cbegin()) {
407 }
else if (it - 1 == path.cbegin() and (it - 1)->empty()) {
413 it = path.erase(it - 1, it + 1);
421 if (path.size() == 1 and path.front().empty()) {
430 [[nodiscard]]
constexpr bool holds_invariant() const noexcept
436 if (
back() ==
"." or
back() ==
".." or
back() ==
"**") {
443 [[nodiscard]]
constexpr friend size_t to_string_size(path_type
const& rhs)
noexcept
446 for (
hilet& segment : rhs) {
462 r.
reserve(to_string_size(rhs));
464 auto segment_is_empty =
false;
465 for (
auto it = rhs.
cbegin(); it != rhs.
cend(); ++it) {
466 segment_is_empty = it->empty();
468 if (it == rhs.
cbegin() and not has_scheme) {
469 r += URI::encode<HI_SUB_DELIM, '@'>(*it);
471 r += URI::encode<HI_PCHAR>(*it);
476 if (not r.empty() and not segment_is_empty) {
484 constexpr URI() noexcept = default;
485 constexpr
URI(
URI const&) noexcept = default;
486 constexpr
URI(
URI&&) noexcept = default;
487 constexpr
URI& operator=(
URI const&) noexcept = default;
488 constexpr
URI& operator=(
URI&&) noexcept = default;
495 constexpr explicit
URI(
std::string_view str)
502 constexpr explicit URI(
const char *str) :
URI(
std::string_view{str}) {}
504 [[nodiscard]]
constexpr bool empty() const noexcept
506 return not _scheme and not _authority and _path.
empty() and not _query and not _fragment;
509 constexpr operator bool() const noexcept
518 [[nodiscard]]
constexpr std::optional<std::string>
const&
scheme() const noexcept
529 validate_scheme(*rhs);
541 [[nodiscard]]
constexpr std::optional<authority_type>
const&
authority() const noexcept
546 constexpr URI& set_authority(std::optional<authority_type>
const& rhs)
noexcept
552 [[nodiscard]]
constexpr path_type
const& path() const noexcept
557 constexpr URI& set_path(path_type
const& rhs)
559 validate_path(rhs, to_bool(_authority));
564 [[nodiscard]]
constexpr std::optional<std::string> filename() const noexcept
566 return _path.filename();
584 [[nodiscard]]
constexpr std::optional<std::string>
const&
query() const noexcept
589 constexpr URI& set_query(std::optional<std::string>
const& rhs)
noexcept
599 [[nodiscard]]
constexpr std::optional<std::string>
const&
fragment() const noexcept
604 constexpr URI& set_fragment(std::optional<std::string>
const& rhs)
noexcept
610 [[nodiscard]]
constexpr friend std::string to_string(
URI const& rhs)
noexcept
613 r.
reserve(to_string_size(rhs));
620 if (rhs._authority) {
623 r += to_string(*rhs._authority);
626 r += to_string(rhs._path, to_bool(rhs._scheme));
635 r +=
URI::encode<HI_PCHAR,
'/',
'?'>(*rhs._fragment);
643 return lhs << to_string(rhs);
646 [[nodiscard]]
constexpr friend bool operator==(URI
const& lhs, URI
const& rhs)
noexcept =
default;
647 [[nodiscard]]
constexpr friend auto operator<=>(URI
const& lhs, URI
const& rhs)
noexcept =
default;
649 [[nodiscard]]
constexpr friend URI operator/(URI
const& base, URI
const& ref)
noexcept
654 target._scheme =
ref._scheme;
655 target._authority =
ref._authority;
656 target._path = remove_dot_segments(
ref._path);
657 target._query =
ref._query;
659 if (
ref._authority) {
660 target._authority =
ref._authority;
661 target._path = remove_dot_segments(
ref._path);
662 target._query =
ref._query;
665 if (
ref._path.empty()) {
666 target._path = base._path;
668 target._query =
ref._query;
670 target._query = base._query;
673 if (
ref._path.absolute()) {
674 target._path = remove_dot_segments(
ref._path);
676 target._path = remove_dot_segments(
merge(base._path,
ref._path, to_bool(base._authority)));
678 target._query =
ref._query;
680 target._authority = base._authority;
682 target._scheme = base._scheme;
685 target._fragment =
ref._fragment;
690 [[nodiscard]]
constexpr friend bool operator==(URI
const& lhs, std::string_view rhs)
noexcept
692 return lhs == URI(rhs);
695 [[nodiscard]]
constexpr friend auto operator<=>(URI
const& lhs, std::string_view rhs)
noexcept
697 return lhs == URI(rhs);
700 [[nodiscard]]
constexpr friend URI operator/(URI
const& base, std::string_view ref)
noexcept
702 return base / URI(ref);
714 for (
auto i = r.find(
'%'); i != std::string::npos; i = r.find(
'%', i)) {
716 auto c = from_string<uint8_t>(r.substr(i + 1, 2), 16);
719 r.replace(i, 3, 1, char_cast<char>(c));
736 return decode(std::string_view{first, last});
746 template<
char... Extras,
typename It,
typename ItEnd>
754 for (
auto it = first; it != last; ++it) {
758 (c >=
'a' and c <=
'z') or
759 (c >=
'A' and c <=
'Z') or
760 (c >=
'0' and c <=
'9') or
761 c ==
'-' or c ==
'.' or c ==
'_' or c ==
'~'
762 ) or ... or (c == Extras))) {
767 auto uc = char_cast<uint8_t>(c);
768 if (
auto nibble = narrow_cast<char>(uc >> 4); nibble <= 9) {
771 r +=
'A' + nibble - 10;
773 if (
auto nibble = narrow_cast<char>(uc & 0xf); nibble <= 9) {
776 r +=
'A' + nibble - 10;
791 template<
char... Extras,
typename Range>
794 return encode<Extras...>(std::ranges::begin(range), std::ranges::end(range));
798 std::optional<std::string> _scheme;
799 std::optional<authority_type> _authority;
801 std::optional<std::string> _query;
802 std::optional<std::string> _fragment;
804 [[nodiscard]]
constexpr friend size_t to_string_size(
URI const& rhs)
noexcept
808 size += rhs._scheme->
size() + 1;
810 if (rhs._authority) {
811 size += to_string_size(*rhs._authority) + 2;
813 size += to_string_size(rhs._path);
816 size += rhs._query->size() + 1;
819 size += rhs._fragment->size() + 1;
825 [[nodiscard]]
constexpr static bool is_scheme_start(
char c)
noexcept
827 return (c >=
'a' and c <=
'z') or (c >=
'A' and c <=
'Z');
830 [[nodiscard]]
constexpr static bool is_scheme(
char c)
noexcept
832 return is_scheme_start(c) or (c >=
'0' and c <=
'9') or c == '+' or c == '-' or c == '.';
835 [[nodiscard]] constexpr static
void validate_scheme(
std::string_view str)
838 throw uri_error(
"The scheme-component is not allowed to be empty (it is allowed to not exist).");
840 if (not is_scheme_start(str.front())) {
841 throw uri_error(
"The scheme-component does not start with [a-zA-Z].");
844 if (not is_scheme(c)) {
845 throw uri_error(
"The scheme-component contains a character outside [a-zA-Z0-9.+-].");
850 [[nodiscard]]
constexpr static void validate_path(path_type
const& path,
bool has_authority)
853 if (not(path.empty() or path.absolute())) {
854 throw uri_error(
"A path-component in a URI with an authority-component must be empty or absolute.");
856 }
else if (path.double_absolute()) {
857 throw uri_error(
"A path-component in a URI without an authority-component may not start with a double slash '//'.");
867 [[nodiscard]]
constexpr const_iterator parse_scheme(const_iterator first, const_iterator last)
869 for (
auto it = first; it != last; ++it) {
870 if (
hilet c = *it;
c ==
':') {
874 }
else if (c ==
'/' or c ==
'?' or c ==
'#') {
884 [[nodiscard]]
constexpr const_iterator parse_authority(const_iterator first, const_iterator last)
886 for (
auto it = first; it != last; it++) {
887 if (
hilet c = *it;
c ==
'/' or
c ==
'?' or
c ==
'#') {
888 set_authority(authority_type{std::string_view{first, it}});
893 set_authority(authority_type{std::string_view{first, last}});
897 [[nodiscard]]
constexpr const_iterator parse_path(const_iterator first, const_iterator last)
899 for (
auto it = first; it != last; it++) {
900 if (
hilet c = *it;
c ==
'?' or
c ==
'#') {
901 set_path(path_type{std::string_view{first, it}});
906 set_path(path_type{std::string_view{first, last}});
910 [[nodiscard]]
constexpr const_iterator parse_query(const_iterator first, const_iterator last)
912 for (
auto it = first; it != last; it++) {
913 if (
hilet c = *it;
c ==
'#') {
923 [[nodiscard]]
constexpr const_iterator parse_fragment(const_iterator first, const_iterator last)
929 constexpr void parse(std::string_view str)
931 auto first = str.cbegin();
932 auto last = str.cend();
933 auto it = parse_scheme(first, last);
935 if (
std::distance(it, last) >= 2 and it[0] ==
'/' and it[1] ==
'/') {
937 it = parse_authority(it, last);
940 it = parse_path(it, last);
942 if (it != last and *it ==
'?') {
943 it = parse_query(++it, last);
946 if (it != last and *it ==
'#') {
947 it = parse_fragment(++it, last);