string.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot 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/beast
  8. //
  9. #ifndef BOOST_BEAST_STRING_HPP
  10. #define BOOST_BEAST_STRING_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/version.hpp>
  13. #if defined(BOOST_BEAST_USE_STD_STRING_VIEW)
  14. #include <string_view>
  15. #else
  16. #include <boost/utility/string_view.hpp>
  17. #endif
  18. #include <algorithm>
  19. namespace boost {
  20. namespace beast {
  21. #if defined(BOOST_BEAST_USE_STD_STRING_VIEW)
  22. /// The type of string view used by the library
  23. using string_view = std::string_view;
  24. /// The type of basic string view used by the library
  25. template<class CharT, class Traits>
  26. using basic_string_view =
  27. std::basic_string_view<CharT, Traits>;
  28. #else
  29. /// The type of string view used by the library
  30. using string_view = boost::string_view;
  31. /// The type of basic string view used by the library
  32. template<class CharT, class Traits>
  33. using basic_string_view =
  34. boost::basic_string_view<CharT, Traits>;
  35. #endif
  36. namespace detail {
  37. inline
  38. char
  39. ascii_tolower(char c)
  40. {
  41. return ((static_cast<unsigned>(c) - 65U) < 26) ?
  42. c + 'a' - 'A' : c;
  43. }
  44. template<class = void>
  45. bool
  46. iequals(
  47. beast::string_view lhs,
  48. beast::string_view rhs)
  49. {
  50. auto n = lhs.size();
  51. if(rhs.size() != n)
  52. return false;
  53. auto p1 = lhs.data();
  54. auto p2 = rhs.data();
  55. char a, b;
  56. // fast loop
  57. while(n--)
  58. {
  59. a = *p1++;
  60. b = *p2++;
  61. if(a != b)
  62. goto slow;
  63. }
  64. return true;
  65. slow:
  66. do
  67. {
  68. if(ascii_tolower(a) != ascii_tolower(b))
  69. return false;
  70. a = *p1++;
  71. b = *p2++;
  72. }
  73. while(n--);
  74. return true;
  75. }
  76. } // detail
  77. /** Returns `true` if two strings are equal, using a case-insensitive comparison.
  78. The case-comparison operation is defined only for low-ASCII characters.
  79. @param lhs The string on the left side of the equality
  80. @param rhs The string on the right side of the equality
  81. */
  82. inline
  83. bool
  84. iequals(
  85. beast::string_view lhs,
  86. beast::string_view rhs)
  87. {
  88. return detail::iequals(lhs, rhs);
  89. }
  90. /** A case-insensitive less predicate for strings.
  91. The case-comparison operation is defined only for low-ASCII characters.
  92. */
  93. struct iless
  94. {
  95. bool
  96. operator()(
  97. string_view lhs,
  98. string_view rhs) const
  99. {
  100. using std::begin;
  101. using std::end;
  102. return std::lexicographical_compare(
  103. begin(lhs), end(lhs), begin(rhs), end(rhs),
  104. [](char c1, char c2)
  105. {
  106. return detail::ascii_tolower(c1) < detail::ascii_tolower(c2);
  107. }
  108. );
  109. }
  110. };
  111. /** A case-insensitive equality predicate for strings.
  112. The case-comparison operation is defined only for low-ASCII characters.
  113. */
  114. struct iequal
  115. {
  116. bool
  117. operator()(
  118. string_view lhs,
  119. string_view rhs) const
  120. {
  121. return iequals(lhs, rhs);
  122. }
  123. };
  124. } // beast
  125. } // boost
  126. #endif