params_base.ipp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. // Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Official repository: https://github.com/boostorg/url
  9. //
  10. #ifndef BOOST_URL_IMPL_PARAMS_BASE_IPP
  11. #define BOOST_URL_IMPL_PARAMS_BASE_IPP
  12. #include <boost/url/params_base.hpp>
  13. #include <ostream>
  14. namespace boost {
  15. namespace urls {
  16. params_base::
  17. iterator::
  18. iterator(
  19. detail::query_ref const& ref,
  20. encoding_opts opt) noexcept
  21. : it_(ref)
  22. , space_as_plus_(opt.space_as_plus)
  23. {
  24. }
  25. params_base::
  26. iterator::
  27. iterator(
  28. detail::query_ref const& ref,
  29. encoding_opts opt,
  30. int) noexcept
  31. : it_(ref, 0)
  32. , space_as_plus_(opt.space_as_plus)
  33. {
  34. }
  35. //------------------------------------------------
  36. auto
  37. params_base::
  38. iterator::
  39. operator*() const ->
  40. reference
  41. {
  42. encoding_opts opt;
  43. opt.space_as_plus =
  44. space_as_plus_;
  45. param_pct_view p =
  46. it_.dereference();
  47. return reference(
  48. p.key.decode(opt),
  49. p.value.decode(opt),
  50. p.has_value);
  51. }
  52. //------------------------------------------------
  53. //
  54. // params_base
  55. //
  56. //------------------------------------------------
  57. params_base::
  58. params_base(
  59. detail::query_ref const& ref,
  60. encoding_opts opt) noexcept
  61. : ref_(ref)
  62. , opt_(opt)
  63. {
  64. }
  65. pct_string_view
  66. params_base::
  67. buffer() const noexcept
  68. {
  69. return ref_.buffer();
  70. }
  71. bool
  72. params_base::
  73. empty() const noexcept
  74. {
  75. return ref_.nparam() == 0;
  76. }
  77. std::size_t
  78. params_base::
  79. size() const noexcept
  80. {
  81. return ref_.nparam();
  82. }
  83. auto
  84. params_base::
  85. begin() const noexcept ->
  86. iterator
  87. {
  88. return iterator(ref_, opt_);
  89. }
  90. auto
  91. params_base::
  92. end() const noexcept ->
  93. iterator
  94. {
  95. return iterator(ref_, opt_, 0);
  96. }
  97. //------------------------------------------------
  98. std::size_t
  99. params_base::
  100. count(
  101. string_view key,
  102. ignore_case_param ic) const noexcept
  103. {
  104. std::size_t n = 0;
  105. auto it = find(key, ic);
  106. auto const end_ = end();
  107. while(it != end_)
  108. {
  109. ++n;
  110. ++it;
  111. it = find(it, key, ic);
  112. }
  113. return n;
  114. }
  115. //------------------------------------------------
  116. //
  117. // (implementation)
  118. //
  119. //------------------------------------------------
  120. detail::params_iter_impl
  121. params_base::
  122. find_impl(
  123. detail::params_iter_impl it,
  124. string_view key,
  125. ignore_case_param ic) const noexcept
  126. {
  127. detail::params_iter_impl end_(ref_, 0);
  128. if(! ic)
  129. {
  130. for(;;)
  131. {
  132. if(it.equal(end_))
  133. return it;
  134. if(*it.key() == key)
  135. return it;
  136. it.increment();
  137. }
  138. }
  139. for(;;)
  140. {
  141. if(it.equal(end_))
  142. return it;
  143. if( grammar::ci_is_equal(
  144. *it.key(), key))
  145. return it;
  146. it.increment();
  147. }
  148. }
  149. detail::params_iter_impl
  150. params_base::
  151. find_last_impl(
  152. detail::params_iter_impl it,
  153. string_view key,
  154. ignore_case_param ic) const noexcept
  155. {
  156. detail::params_iter_impl begin_(ref_);
  157. if(! ic)
  158. {
  159. for(;;)
  160. {
  161. if(it.equal(begin_))
  162. return { ref_, 0 };
  163. it.decrement();
  164. if(*it.key() == key)
  165. return it;
  166. }
  167. }
  168. for(;;)
  169. {
  170. if(it.equal(begin_))
  171. return { ref_, 0 };
  172. it.decrement();
  173. if(grammar::ci_is_equal(
  174. *it.key(), key))
  175. return it;
  176. }
  177. }
  178. //------------------------------------------------
  179. std::ostream&
  180. operator<<(
  181. std::ostream& os,
  182. params_base const& qp)
  183. {
  184. os << qp.buffer();
  185. return os;
  186. }
  187. } // urls
  188. } // boost
  189. #endif