pointer.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. class Pointer {
  14. public:
  15. Pointer() = default;
  16. Pointer(std::vector<std::variant<std::string, size_t>> const & tokens) : tokens_(tokens) {}
  17. Pointer(std::string_view path) {
  18. if (path.empty()) {
  19. return;
  20. }
  21. auto append_with_parse = [this](std::string in) {
  22. if (not in.empty() && in.find_first_not_of("0123456789") == std::string::npos) {
  23. return tokens_.push_back(std::stoull(in));
  24. }
  25. for (size_t i = 0; i < in.size(); ++i) {
  26. if (in[i] == '%') {
  27. char const enc[3] = {in[i + 1], in[i + 2]};
  28. in.replace(i, 3, 1, char(std::stoi(enc, nullptr, 16)));
  29. } else if (in[i] != '~') {
  30. continue;
  31. }
  32. if (in[i + 1] == '0') {
  33. in.replace(i, 2, 1, '~');
  34. } else if (in[i + 1] == '1') {
  35. in.replace(i, 2, 1, '/');
  36. }
  37. }
  38. tokens_.push_back(std::move(in));
  39. };
  40. path.remove_prefix(1);
  41. size_t p = path.find('/');
  42. for (; p != std::string::npos; path.remove_prefix(p + 1), p = path.find('/')) {
  43. append_with_parse(std::string(path.substr(0, p)));
  44. }
  45. append_with_parse(std::string(path));
  46. }
  47. template <Adapter A> A walk(A document) const {
  48. for (auto const & token : tokens_) {
  49. document = std::visit([&document](auto const & next) { return document[next]; }, token);
  50. }
  51. return document;
  52. }
  53. bool empty() const { return tokens_.empty(); }
  54. bool starts_with(Pointer const & other) const {
  55. return other.tokens_.size() <= tokens_.size() &&
  56. std::equal(other.tokens_.begin(), other.tokens_.end(), tokens_.begin());
  57. }
  58. Pointer relative_to(Pointer const & other) const {
  59. assert(starts_with(other));
  60. return Pointer(std::vector(tokens_.begin() + other.tokens_.size(), tokens_.end()));
  61. }
  62. Pointer parent() const { return Pointer({tokens_.begin(), tokens_.end() - 1}); }
  63. Pointer & operator/=(Pointer const & relative) {
  64. tokens_.insert(tokens_.end(), relative.tokens_.begin(), relative.tokens_.end());
  65. return *this;
  66. }
  67. Pointer operator/(Pointer const & relative) const { return Pointer(*this) /= relative; }
  68. Pointer & operator/=(std::string_view key) {
  69. tokens_.emplace_back(std::string(key));
  70. return *this;
  71. }
  72. Pointer operator/(std::string_view key) const { return Pointer(*this) /= key; }
  73. Pointer & operator/=(size_t index) {
  74. tokens_.emplace_back(index);
  75. return *this;
  76. }
  77. Pointer operator/(size_t index) const { return Pointer(*this) /= index; }
  78. friend std::ostream & operator<<(std::ostream & os, Pointer const & self) {
  79. for (auto const & elem : self.tokens_) {
  80. std::visit([&os](auto const & v) { os << '/' << v; }, elem);
  81. }
  82. return os;
  83. }
  84. auto operator<=>(Pointer const &) const = default;
  85. private:
  86. std::vector<std::variant<std::string, size_t>> tokens_{};
  87. };
  88. }