39 using const_iterator = std::string_view::const_iterator;
60 [[nodiscard]]
constexpr std::optional<std::string>
const& userinfo()
const noexcept
65 constexpr authority_type& set_userinfo(std::optional<std::string>
const& rhs)
noexcept
71 [[nodiscard]]
constexpr std::string const& host()
const noexcept
79 _host = to_lower(rhs);
83 [[nodiscard]]
constexpr std::optional<std::string>
const& port()
const noexcept
88 constexpr authority_type& set_port(std::optional<std::string>
const& rhs)
102 [[nodiscard]]
constexpr friend size_t to_string_size(
authority_type const& rhs)
noexcept
106 size += rhs._userinfo->size() + 1;
108 size += rhs._host.size();
110 size += rhs._port->size() + 1;
120 r += URI::encode<HI_SUB_DELIM, ':'>(*rhs._userinfo);
124 if (rhs._host.empty() or rhs._host.front() !=
'[') {
125 r += URI::encode<HI_SUB_DELIM>(rhs._host);
127 r += URI::encode<HI_SUB_DELIM, '[', ']', ':'>(rhs._host);
139 std::optional<std::string> _userinfo = {};
141 std::optional<std::string> _port = {};
143 [[nodiscard]]
constexpr static void validate_host(std::string_view str)
145 if (str.starts_with(
'[') and not str.ends_with(
']')) {
146 throw uri_error(
"The host-component starts with '[' has missing ']' at end");
147 }
else if (str.ends_with(
']') and str.starts_with(
'[')) {
148 throw uri_error(
"The host-component ends with ']' has missing '[' at start");
152 [[nodiscard]]
constexpr static void validate_port(std::string_view str)
155 if (not(c >=
'0' and c <=
'9')) {
156 throw uri_error(
"The port-component contains a non-digit.");
168 [[nodiscard]]
constexpr const_iterator parse_userinfo(const_iterator first, const_iterator last)
noexcept
170 for (
auto it = first; it != last; ++it) {
171 if (
hilet c = *it; c ==
'@') {
187 [[nodiscard]]
constexpr const_iterator parse_host(const_iterator first, const_iterator last)
noexcept
205 for (; it != last; ++it) {
206 if (
hilet c = *it; c ==
':') {
219 [[nodiscard]]
constexpr const_iterator parse_port(const_iterator first, const_iterator last)
noexcept
225 constexpr void parse(std::string_view rhs)
noexcept
227 auto first = rhs.cbegin();
228 auto last = rhs.cend();
229 auto it = parse_userinfo(first, last);
230 it = parse_host(it, last);
232 if (it != last and *it ==
':') {
233 it = parse_port(++it, last);
270 constexpr path_type()
noexcept =
default;
278 auto r = make_vector<std::string>(std::views::transform(std::views::split(str, std::string_view{
"/"}), [](
auto&& x) {
279 return URI::decode(std::string_view{std::ranges::begin(x), std::ranges::end(x)});
282 if (r.size() == 1 and r.front().empty()) {
286 if (not r.empty() and (r.back() ==
"." or r.back() ==
".." or r.back() ==
"**")) {
299 [[nodiscard]]
constexpr bool absolute()
const noexcept
304 [[nodiscard]]
constexpr bool double_absolute()
const noexcept
306 return size() >= 3 and (*
this)[0].
empty() and (*
this)[1].empty();
309 [[nodiscard]]
constexpr bool is_root()
const noexcept
311 return size() == 2 and (*
this)[0].
empty() and (*
this)[1].empty();
314 [[nodiscard]]
constexpr std::optional<std::string> filename()
const noexcept
345 if (base_has_authority and base.empty()) {
351 if (not base.empty()) {
355 base.insert(base.cend(), ref.cbegin(), ref.cend());
357 if (base.size() == 1 and base.front().empty()) {
393 for (
auto it = path.cbegin(); it != path.cend();) {
398 }
else if (*it ==
"..") {
399 if (it == path.cbegin()) {
403 }
else if (it - 1 == path.cbegin() and (it - 1)->empty()) {
409 it = path.erase(it - 1, it + 1);
417 if (path.size() == 1 and path.front().empty()) {
426 [[nodiscard]]
constexpr bool holds_invariant() const noexcept
432 if (
back() ==
"." or
back() ==
".." or
back() ==
"**") {
439 [[nodiscard]]
constexpr friend size_t to_string_size(path_type
const& rhs)
noexcept
442 for (
hilet& segment : rhs) {
458 r.
reserve(to_string_size(rhs));
460 auto segment_is_empty =
false;
461 for (
auto it = rhs.
cbegin(); it != rhs.
cend(); ++it) {
462 segment_is_empty = it->empty();
464 if (it == rhs.
cbegin() and not has_scheme) {
465 r += URI::encode<HI_SUB_DELIM, '@'>(*it);
467 r += URI::encode<HI_PCHAR>(*it);
472 if (not r.empty() and not segment_is_empty) {
480 constexpr URI() noexcept = default;
481 constexpr
URI(
URI const&) noexcept = default;
482 constexpr
URI(
URI&&) noexcept = default;
483 constexpr
URI& operator=(
URI const&) noexcept = default;
484 constexpr
URI& operator=(
URI&&) noexcept = default;
491 constexpr explicit
URI(
std::string_view str)
498 constexpr explicit URI(
const char *str) :
URI(
std::string_view{str}) {}
500 [[nodiscard]]
constexpr bool empty() const noexcept
502 return not _scheme and not _authority and _path.
empty() and not _query and not _fragment;
505 constexpr operator bool() const noexcept
514 [[nodiscard]]
constexpr std::optional<std::string>
const&
scheme() const noexcept
525 validate_scheme(*rhs);
537 [[nodiscard]]
constexpr std::optional<authority_type>
const&
authority() const noexcept
542 constexpr URI& set_authority(std::optional<authority_type>
const& rhs)
noexcept
548 [[nodiscard]]
constexpr path_type
const& path() const noexcept
553 constexpr URI& set_path(path_type
const& rhs)
555 validate_path(rhs, to_bool(_authority));
560 [[nodiscard]]
constexpr std::optional<std::string> filename() const noexcept
562 return _path.filename();
580 [[nodiscard]]
constexpr std::optional<std::string>
const&
query() const noexcept
585 constexpr URI& set_query(std::optional<std::string>
const& rhs)
noexcept
595 [[nodiscard]]
constexpr std::optional<std::string>
const&
fragment() const noexcept
600 constexpr URI& set_fragment(std::optional<std::string>
const& rhs)
noexcept
606 [[nodiscard]]
constexpr friend std::string to_string(
URI const& rhs)
noexcept
609 r.
reserve(to_string_size(rhs));
616 if (rhs._authority) {
619 r += to_string(*rhs._authority);
622 r += to_string(rhs._path, to_bool(rhs._scheme));
631 r +=
URI::encode<HI_PCHAR,
'/',
'?'>(*rhs._fragment);
639 return lhs << to_string(rhs);
642 [[nodiscard]]
constexpr friend bool operator==(URI
const& lhs, URI
const& rhs)
noexcept =
default;
643 [[nodiscard]]
constexpr friend auto operator<=>(URI
const& lhs, URI
const& rhs)
noexcept =
default;
645 [[nodiscard]]
constexpr friend URI operator/(URI
const& base, URI
const& ref)
noexcept
650 target._scheme =
ref._scheme;
651 target._authority =
ref._authority;
652 target._path = remove_dot_segments(
ref._path);
653 target._query =
ref._query;
655 if (
ref._authority) {
656 target._authority =
ref._authority;
657 target._path = remove_dot_segments(
ref._path);
658 target._query =
ref._query;
661 if (
ref._path.empty()) {
662 target._path = base._path;
664 target._query =
ref._query;
666 target._query = base._query;
669 if (
ref._path.absolute()) {
670 target._path = remove_dot_segments(
ref._path);
672 target._path = remove_dot_segments(
merge(base._path,
ref._path, to_bool(base._authority)));
674 target._query =
ref._query;
676 target._authority = base._authority;
678 target._scheme = base._scheme;
681 target._fragment =
ref._fragment;
686 [[nodiscard]]
constexpr friend bool operator==(URI
const& lhs, std::string_view rhs)
noexcept
688 return lhs == URI(rhs);
691 [[nodiscard]]
constexpr friend auto operator<=>(URI
const& lhs, std::string_view rhs)
noexcept
693 return lhs == URI(rhs);
696 [[nodiscard]]
constexpr friend URI operator/(URI
const& base, std::string_view ref)
noexcept
698 return base / URI(ref);
710 for (
auto i = r.find(
'%'); i != std::string::npos; i = r.find(
'%', i)) {
712 auto c = from_string<uint8_t>(r.substr(i + 1, 2), 16);
715 r.replace(i, 3, 1, char_cast<char>(c));
732 return decode(std::string_view{first, last});
742 template<
char... Extras,
typename It,
typename ItEnd>
750 for (
auto it = first; it != last; ++it) {
754 (c >=
'a' and c <=
'z') or
755 (c >=
'A' and c <=
'Z') or
756 (c >=
'0' and c <=
'9') or
757 c ==
'-' or c ==
'.' or c ==
'_' or c ==
'~'
758 ) or ... or (c == Extras))) {
763 auto uc = char_cast<uint8_t>(c);
764 if (
auto nibble = narrow_cast<char>(uc >> 4); nibble <= 9) {
767 r +=
'A' + nibble - 10;
769 if (
auto nibble = narrow_cast<char>(uc & 0xf); nibble <= 9) {
772 r +=
'A' + nibble - 10;
787 template<
char... Extras,
typename Range>
790 return encode<Extras...>(std::ranges::begin(range), std::ranges::end(range));
794 std::optional<std::string> _scheme;
795 std::optional<authority_type> _authority;
797 std::optional<std::string> _query;
798 std::optional<std::string> _fragment;
800 [[nodiscard]]
constexpr friend size_t to_string_size(
URI const& rhs)
noexcept
804 size += rhs._scheme->
size() + 1;
806 if (rhs._authority) {
807 size += to_string_size(*rhs._authority) + 2;
809 size += to_string_size(rhs._path);
812 size += rhs._query->size() + 1;
815 size += rhs._fragment->size() + 1;
821 [[nodiscard]]
constexpr static bool is_scheme_start(
char c)
noexcept
823 return (c >=
'a' and c <=
'z') or (c >=
'A' and c <=
'Z');
826 [[nodiscard]]
constexpr static bool is_scheme(
char c)
noexcept
828 return is_scheme_start(c) or (c >=
'0' and c <=
'9') or c == '+' or c == '-' or c == '.';
831 [[nodiscard]] constexpr static
void validate_scheme(
std::string_view str)
834 throw uri_error(
"The scheme-component is not allowed to be empty (it is allowed to not exist).");
836 if (not is_scheme_start(str.front())) {
837 throw uri_error(
"The scheme-component does not start with [a-zA-Z].");
840 if (not is_scheme(c)) {
841 throw uri_error(
"The scheme-component contains a character outside [a-zA-Z0-9.+-].");
846 [[nodiscard]]
constexpr static void validate_path(path_type
const& path,
bool has_authority)
849 if (not(path.empty() or path.absolute())) {
850 throw uri_error(
"A path-component in a URI with an authority-component must be empty or absolute.");
852 }
else if (path.double_absolute()) {
853 throw uri_error(
"A path-component in a URI without an authority-component may not start with a double slash '//'.");
863 [[nodiscard]]
constexpr const_iterator parse_scheme(const_iterator first, const_iterator last)
865 for (
auto it = first; it != last; ++it) {
866 if (
hilet c = *it;
c ==
':') {
870 }
else if (c ==
'/' or c ==
'?' or c ==
'#') {
880 [[nodiscard]]
constexpr const_iterator parse_authority(const_iterator first, const_iterator last)
882 for (
auto it = first; it != last; it++) {
883 if (
hilet c = *it;
c ==
'/' or
c ==
'?' or
c ==
'#') {
884 set_authority(authority_type{std::string_view{first, it}});
889 set_authority(authority_type{std::string_view{first, last}});
893 [[nodiscard]]
constexpr const_iterator parse_path(const_iterator first, const_iterator last)
895 for (
auto it = first; it != last; it++) {
896 if (
hilet c = *it;
c ==
'?' or
c ==
'#') {
897 set_path(path_type{std::string_view{first, it}});
902 set_path(path_type{std::string_view{first, last}});
906 [[nodiscard]]
constexpr const_iterator parse_query(const_iterator first, const_iterator last)
908 for (
auto it = first; it != last; it++) {
909 if (
hilet c = *it;
c ==
'#') {
919 [[nodiscard]]
constexpr const_iterator parse_fragment(const_iterator first, const_iterator last)
925 constexpr void parse(std::string_view str)
927 auto first = str.cbegin();
928 auto last = str.cend();
929 auto it = parse_scheme(first, last);
931 if (
std::distance(it, last) >= 2 and it[0] ==
'/' and it[1] ==
'/') {
933 it = parse_authority(it, last);
936 it = parse_path(it, last);
938 if (it != last and *it ==
'?') {
939 it = parse_query(++it, last);
942 if (it != last and *it ==
'#') {
943 it = parse_fragment(++it, last);