pointer.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. bool empty() const { return tokens_.empty(); }
  56. bool starts_with(Pointer const & other) const {
  57. return other.tokens_.size() <= tokens_.size() &&
  58. std::equal(other.tokens_.begin(), other.tokens_.end(), tokens_.begin());
  59. }
  60. Pointer relative_to(Pointer const & other) const {
  61. assert(starts_with(other));
  62. return Pointer(std::vector(tokens_.begin() + other.tokens_.size(), tokens_.end()));
  63. }
  64. Pointer parent() const { return Pointer({tokens_.begin(), tokens_.end() - 1}); }
  65. Pointer & operator/=(Pointer const & relative) {
  66. tokens_.insert(tokens_.end(), relative.tokens_.begin(), relative.tokens_.end());
  67. return *this;
  68. }
  69. Pointer operator/(Pointer const & relative) const { return Pointer(*this) /= relative; }
  70. Pointer & operator/=(parent_t) {
  71. tokens_.pop_back();
  72. return *this;
  73. }
  74. Pointer operator/(parent_t) const { return parent(); }
  75. Pointer & operator/=(std::string_view key) {
  76. tokens_.emplace_back(std::string(key));
  77. return *this;
  78. }
  79. Pointer operator/(std::string_view key) const { return Pointer(*this) /= key; }
  80. Pointer & operator/=(size_t index) {
  81. tokens_.emplace_back(index);
  82. return *this;
  83. }
  84. Pointer operator/(size_t index) const { return Pointer(*this) /= index; }
  85. friend std::ostream & operator<<(std::ostream & os, Pointer const & self) {
  86. for (auto const & elem : self.tokens_) {
  87. std::visit([&os](auto const & v) { os << '/' << v; }, elem);
  88. }
  89. return os;
  90. }
  91. auto operator<=>(Pointer const &) const = default;
  92. private:
  93. std::vector<std::variant<std::string, size_t>> tokens_{};
  94. };
  95. }