normalize.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot 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_DETAIL_NORMALIZED_HPP
  11. #define BOOST_URL_DETAIL_NORMALIZED_HPP
  12. #include <boost/core/detail/string_view.hpp>
  13. #include <boost/url/segments_encoded_view.hpp>
  14. #include <boost/url/detail/normalize.hpp>
  15. namespace boost {
  16. namespace urls {
  17. namespace detail {
  18. class fnv_1a
  19. {
  20. public:
  21. using digest_type = std::size_t;
  22. #if BOOST_URL_ARCH == 64
  23. static constexpr std::size_t const prime =
  24. static_cast<std::size_t>(0x100000001B3ULL);
  25. static constexpr std::size_t init_hash =
  26. static_cast<std::size_t>(0xcbf29ce484222325ULL);
  27. #else
  28. static constexpr std::size_t const prime =
  29. static_cast<std::size_t>(0x01000193UL);
  30. static constexpr std::size_t init_hash =
  31. static_cast<std::size_t>(0x811C9DC5UL);
  32. #endif
  33. explicit
  34. fnv_1a(std::size_t salt) noexcept
  35. : h_(init_hash + salt)
  36. {
  37. }
  38. void
  39. put(char c) noexcept
  40. {
  41. h_ ^= c;
  42. h_ *= prime;
  43. }
  44. void
  45. put(core::string_view s) noexcept
  46. {
  47. for (char c: s)
  48. {
  49. put(c);
  50. }
  51. }
  52. digest_type
  53. digest() const noexcept
  54. {
  55. return h_;
  56. }
  57. private:
  58. std::size_t h_;
  59. };
  60. void
  61. pop_encoded_front(
  62. core::string_view& s,
  63. char& c,
  64. std::size_t& n) noexcept;
  65. // compare two core::string_views as if they are both
  66. // percent-decoded
  67. int
  68. compare_encoded(
  69. core::string_view lhs,
  70. core::string_view rhs) noexcept;
  71. // digest a core::string_view as if it were
  72. // percent-decoded
  73. void
  74. digest_encoded(
  75. core::string_view s,
  76. fnv_1a& hasher) noexcept;
  77. void
  78. digest(
  79. core::string_view s,
  80. fnv_1a& hasher) noexcept;
  81. // check if core::string_view lhs starts with core::string_view
  82. // rhs as if they are both percent-decoded. If
  83. // lhs starts with rhs, return number of chars
  84. // matched in the encoded core::string_view
  85. std::size_t
  86. path_starts_with(
  87. core::string_view lhs,
  88. core::string_view rhs) noexcept;
  89. // check if core::string_view lhs ends with core::string_view
  90. // rhs as if they are both percent-decoded. If
  91. // lhs ends with rhs, return number of chars
  92. // matched in the encoded core::string_view
  93. std::size_t
  94. path_ends_with(
  95. core::string_view lhs,
  96. core::string_view rhs) noexcept;
  97. // compare two core::string_views as if they are both
  98. // percent-decoded and lowercase
  99. int
  100. ci_compare_encoded(
  101. core::string_view lhs,
  102. core::string_view rhs) noexcept;
  103. // digest a core::string_view as if it were decoded
  104. // and lowercase
  105. void
  106. ci_digest_encoded(
  107. core::string_view s,
  108. fnv_1a& hasher) noexcept;
  109. // compare two ascii core::string_views
  110. int
  111. compare(
  112. core::string_view lhs,
  113. core::string_view rhs) noexcept;
  114. // compare two core::string_views as if they are both
  115. // lowercase
  116. int
  117. ci_compare(
  118. core::string_view lhs,
  119. core::string_view rhs) noexcept;
  120. // digest a core::string_view as if it were lowercase
  121. void
  122. ci_digest(
  123. core::string_view s,
  124. fnv_1a& hasher) noexcept;
  125. BOOST_URL_DECL
  126. std::size_t
  127. remove_dot_segments(
  128. char* dest,
  129. char const* end,
  130. core::string_view s) noexcept;
  131. void
  132. pop_last_segment(
  133. core::string_view& s,
  134. core::string_view& c,
  135. std::size_t& level,
  136. bool r) noexcept;
  137. char
  138. path_pop_back( core::string_view& s );
  139. void
  140. normalized_path_digest(
  141. core::string_view s,
  142. bool remove_unmatched,
  143. fnv_1a& hasher) noexcept;
  144. int
  145. segments_compare(
  146. segments_encoded_view seg0,
  147. segments_encoded_view seg1) noexcept;
  148. } // detail
  149. } // urls
  150. } // boost
  151. #endif