normalize.hpp 3.4 KB

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