pointer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #pragma once
  2. #include <algorithm>
  3. #include <cassert>
  4. #include <cstdint>
  5. #include <iostream>
  6. #include <string>
  7. #include <string_view>
  8. #include <variant>
  9. #include <vector>
  10. #include <jvalidate/detail/compare.h>
  11. #include <jvalidate/forward.h>
  12. namespace jvalidate::detail {
  13. struct parent_t {};
  14. constexpr parent_t parent;
  15. class Pointer {
  16. public:
  17. Pointer() = default;
  18. Pointer(std::vector<std::variant<std::string, size_t>> const & tokens) : tokens_(tokens) {}
  19. Pointer(std::string_view path) {
  20. if (path.empty()) {
  21. return;
  22. }
  23. auto append_with_parse = [this](std::string in) {
  24. if (not in.empty() && in.find_first_not_of("0123456789") == std::string::npos) {
  25. return tokens_.push_back(std::stoull(in));
  26. }
  27. for (size_t i = 0; i < in.size(); ++i) {
  28. if (in[i] == '%') {
  29. char const enc[3] = {in[i + 1], in[i + 2]};
  30. in.replace(i, 3, 1, char(std::stoi(enc, nullptr, 16)));
  31. } else if (in[i] != '~') {
  32. continue;
  33. }
  34. if (in[i + 1] == '0') {
  35. in.replace(i, 2, 1, '~');
  36. } else if (in[i + 1] == '1') {
  37. in.replace(i, 2, 1, '/');
  38. }
  39. }
  40. tokens_.push_back(std::move(in));
  41. };
  42. path.remove_prefix(1);
  43. size_t p = path.find('/');
  44. for (; p != std::string::npos; path.remove_prefix(p + 1), p = path.find('/')) {
  45. append_with_parse(std::string(path.substr(0, p)));
  46. }
  47. append_with_parse(std::string(path));
  48. }
  49. template <Adapter A> A walk(A document) const {
  50. for (auto const & token : tokens_) {
  51. document = std::visit([&document](auto const & next) { return document[next]; }, token);
  52. }
  53. return document;
  54. }
  55. std::string back() const {
  56. struct {
  57. std::string operator()(std::string const & in) const { return in; }
  58. std::string operator()(size_t in) const { return std::to_string(in); }
  59. } g_as_str;
  60. return tokens_.empty() ? "" : std::visit(g_as_str, tokens_.back());
  61. }
  62. bool empty() const { return tokens_.empty(); }
  63. bool starts_with(Pointer const & other) const {
  64. return other.tokens_.size() <= tokens_.size() &&
  65. std::equal(other.tokens_.begin(), other.tokens_.end(), tokens_.begin());
  66. }
  67. Pointer relative_to(Pointer const & other) const {
  68. assert(starts_with(other));
  69. return Pointer(std::vector(tokens_.begin() + other.tokens_.size(), tokens_.end()));
  70. }
  71. Pointer parent() const { return Pointer({tokens_.begin(), tokens_.end() - 1}); }
  72. Pointer & operator/=(Pointer const & relative) {
  73. tokens_.insert(tokens_.end(), relative.tokens_.begin(), relative.tokens_.end());
  74. return *this;
  75. }
  76. Pointer operator/(Pointer const & relative) const { return Pointer(*this) /= relative; }
  77. Pointer & operator/=(parent_t) {
  78. tokens_.pop_back();
  79. return *this;
  80. }
  81. Pointer operator/(parent_t) const { return parent(); }
  82. Pointer & operator/=(std::string_view key) {
  83. tokens_.emplace_back(std::string(key));
  84. return *this;
  85. }
  86. Pointer operator/(std::string_view key) const { return Pointer(*this) /= key; }
  87. Pointer & operator/=(size_t index) {
  88. tokens_.emplace_back(index);
  89. return *this;
  90. }
  91. Pointer operator/(size_t index) const { return Pointer(*this) /= index; }
  92. friend std::ostream & operator<<(std::ostream & os, Pointer const & self) {
  93. for (auto const & elem : self.tokens_) {
  94. std::visit([&os](auto const & v) { os << '/' << v; }, elem);
  95. }
  96. return os;
  97. }
  98. auto operator<=>(Pointer const &) const = default;
  99. private:
  100. std::vector<std::variant<std::string, size_t>> tokens_{};
  101. };
  102. }