anchor.h 1023 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #pragma once
  2. #include <algorithm>
  3. #include <cctype>
  4. #include <compare>
  5. #include <string>
  6. #include <string_view>
  7. #include <jvalidate/detail/compare.h>
  8. #include <jvalidate/detail/expect.h>
  9. namespace jvalidate::detail {
  10. class Anchor {
  11. private:
  12. std::string content_;
  13. public:
  14. Anchor() = default;
  15. explicit Anchor(std::string_view content) : content_(content) {
  16. EXPECT_M(content.empty() || content[0] == '_' || std::isalpha(content[0]),
  17. "First character of an Anchor must be alphabetic or '_'");
  18. EXPECT_M(
  19. std::all_of(content.begin(), content.end(),
  20. [](char c) { return std::isalnum(c) || c == '_' || c == '.' || c == '-'; }),
  21. "Illegal character(s) in anchor");
  22. }
  23. explicit operator std::string const &() const { return content_; }
  24. bool empty() const { return content_.empty(); }
  25. friend std::ostream & operator<<(std::ostream & os, Anchor const & self) {
  26. return os << self.content_;
  27. }
  28. auto operator<=>(Anchor const & lhs) const = default;
  29. };
  30. }