uri.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #pragma once
  2. #include <string>
  3. #include <string_view>
  4. #include <jvalidate/detail/compare.h>
  5. #include <jvalidate/detail/expect.h>
  6. namespace jvalidate {
  7. class URI {
  8. private:
  9. std::string uri_;
  10. size_t scheme_{0};
  11. size_t resource_{0};
  12. public:
  13. URI() = default;
  14. explicit URI(std::string_view uri) : uri_(uri) {
  15. if (uri_.back() == '#') {
  16. uri_.pop_back();
  17. }
  18. if (size_t n = uri_.find("://"); n != std::string::npos) {
  19. scheme_ = n;
  20. resource_ = n + 3;
  21. }
  22. }
  23. URI parent() const { return URI(std::string_view(uri_).substr(0, uri_.rfind('/'))); }
  24. URI operator/(URI const & relative) const { return URI(uri_ + relative.uri_); }
  25. std::string_view scheme() const { return std::string_view(uri_).substr(0, scheme_); }
  26. std::string_view resource() const { return std::string_view(uri_).substr(resource_); }
  27. explicit operator std::string const &() const { return uri_; }
  28. bool empty() const { return uri_.empty(); }
  29. friend std::ostream & operator<<(std::ostream & os, URI const & self) { return os << self.uri_; }
  30. auto operator<=>(URI const & lhs) const = default;
  31. };
  32. }