| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- #pragma once
- #include <string>
- #include <string_view>
- #include <jvalidate/detail/compare.h>
- #include <jvalidate/detail/expect.h>
- namespace jvalidate {
- class URI {
- private:
- std::string uri_;
- size_t scheme_{0};
- size_t resource_{0};
- public:
- URI() = default;
- explicit URI(std::string_view uri) : uri_(uri) {
- if (uri_.back() == '#') {
- uri_.pop_back();
- }
- if (size_t n = uri_.find("://"); n != std::string::npos) {
- scheme_ = n;
- resource_ = n + 3;
- }
- }
- URI parent() const { return URI(std::string_view(uri_).substr(0, uri_.rfind('/'))); }
- URI operator/(URI const & relative) const { return URI(uri_ + relative.uri_); }
- std::string_view scheme() const { return std::string_view(uri_).substr(0, scheme_); }
- std::string_view resource() const { return std::string_view(uri_).substr(resource_); }
- explicit operator std::string const &() const { return uri_; }
- bool empty() const { return uri_.empty(); }
- friend std::ostream & operator<<(std::ostream & os, URI const & self) { return os << self.uri_; }
- auto operator<=>(URI const & lhs) const = default;
- };
- }
|