decode_view.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/url
  8. //
  9. #ifndef BOOST_URL_IMPL_PCT_ENCODED_VIEW_HPP
  10. #define BOOST_URL_IMPL_PCT_ENCODED_VIEW_HPP
  11. #include <boost/url/grammar/type_traits.hpp>
  12. #include <boost/static_assert.hpp>
  13. namespace boost {
  14. namespace urls {
  15. class decode_view::iterator
  16. {
  17. char const* begin_ = nullptr;
  18. char const* pos_ = nullptr;
  19. bool space_as_plus_ = true;
  20. friend decode_view;
  21. iterator(
  22. char const* str,
  23. bool space_as_plus) noexcept
  24. : begin_(str)
  25. , pos_(str)
  26. , space_as_plus_(
  27. space_as_plus)
  28. {
  29. }
  30. // end ctor
  31. iterator(
  32. char const* str,
  33. size_type n,
  34. bool space_as_plus) noexcept
  35. : begin_(str)
  36. , pos_(str + n)
  37. , space_as_plus_(space_as_plus)
  38. {
  39. }
  40. public:
  41. using value_type = char;
  42. using reference = char;
  43. using pointer = void const*;
  44. using const_reference = char;
  45. using size_type = std::size_t;
  46. using difference_type = std::ptrdiff_t;
  47. using iterator_category =
  48. std::bidirectional_iterator_tag;
  49. iterator() = default;
  50. iterator(iterator const&) = default;
  51. iterator&
  52. operator=(iterator const&) = default;
  53. BOOST_URL_DECL
  54. reference
  55. operator*() const noexcept;
  56. iterator&
  57. operator++() noexcept
  58. {
  59. BOOST_ASSERT(pos_ != nullptr);
  60. if (*pos_ != '%')
  61. ++pos_;
  62. else
  63. pos_ += 3;
  64. return *this;
  65. }
  66. iterator&
  67. operator--() noexcept
  68. {
  69. BOOST_ASSERT(pos_ != begin_);
  70. if (pos_ - begin_ < 3 ||
  71. pos_[-3] != '%')
  72. --pos_;
  73. else
  74. pos_ -= 3;
  75. return *this;
  76. }
  77. iterator
  78. operator++(int) noexcept
  79. {
  80. auto tmp = *this;
  81. ++*this;
  82. return tmp;
  83. }
  84. iterator
  85. operator--(int) noexcept
  86. {
  87. auto tmp = *this;
  88. --*this;
  89. return tmp;
  90. }
  91. char const*
  92. base()
  93. {
  94. return pos_;
  95. }
  96. bool
  97. operator==(
  98. iterator const& other) const noexcept
  99. {
  100. return pos_ == other.pos_;
  101. }
  102. bool
  103. operator!=(
  104. iterator const& other) const noexcept
  105. {
  106. return !(*this == other);
  107. }
  108. };
  109. //------------------------------------------------
  110. inline
  111. auto
  112. decode_view::
  113. begin() const noexcept ->
  114. const_iterator
  115. {
  116. return {p_, space_as_plus_};
  117. }
  118. inline
  119. auto
  120. decode_view::
  121. end() const noexcept ->
  122. const_iterator
  123. {
  124. return {p_, n_, space_as_plus_};
  125. }
  126. inline
  127. auto
  128. decode_view::
  129. front() const noexcept ->
  130. const_reference
  131. {
  132. BOOST_ASSERT( !empty() );
  133. return *begin();
  134. }
  135. inline
  136. auto
  137. decode_view::
  138. back() const noexcept ->
  139. const_reference
  140. {
  141. BOOST_ASSERT( !empty() );
  142. return *--end();
  143. }
  144. } // urls
  145. } // boost
  146. #endif