charset.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/url
  8. //
  9. #ifndef BOOST_URL_GRAMMAR_DETAIL_CHARSET_HPP
  10. #define BOOST_URL_GRAMMAR_DETAIL_CHARSET_HPP
  11. #include <boost/core/bit.hpp>
  12. #include <type_traits>
  13. #ifdef BOOST_URL_USE_SSE2
  14. # include <emmintrin.h>
  15. # include <xmmintrin.h>
  16. # ifdef _MSC_VER
  17. # include <intrin.h>
  18. # endif
  19. #endif
  20. #ifdef _MSC_VER
  21. #pragma warning(push)
  22. #pragma warning(disable: 4127) // conditional expression is constant
  23. #endif
  24. namespace boost {
  25. namespace urls {
  26. namespace grammar {
  27. namespace detail {
  28. template<class T, class = void>
  29. struct has_find_if : std::false_type {};
  30. template<class T>
  31. struct has_find_if<T, void_t<
  32. decltype(
  33. std::declval<char const*&>() =
  34. std::declval<T const&>().find_if(
  35. std::declval<char const*>(),
  36. std::declval<char const*>())
  37. )>> : std::true_type
  38. {
  39. };
  40. template<class T, class = void>
  41. struct has_find_if_not : std::false_type {};
  42. template<class T>
  43. struct has_find_if_not<T, void_t<
  44. decltype(
  45. std::declval<char const*&>() =
  46. std::declval<T const&>().find_if_not(
  47. std::declval<char const*>(),
  48. std::declval<char const*>())
  49. )>> : std::true_type
  50. {
  51. };
  52. template<class Pred>
  53. char const*
  54. find_if(
  55. char const* first,
  56. char const* const last,
  57. Pred const& pred,
  58. std::false_type) noexcept
  59. {
  60. while(first != last)
  61. {
  62. if(pred(*first))
  63. break;
  64. ++first;
  65. }
  66. return first;
  67. }
  68. template<class Pred>
  69. char const*
  70. find_if(
  71. char const* first,
  72. char const* const last,
  73. Pred const& pred,
  74. std::true_type) noexcept
  75. {
  76. return pred.find_if(
  77. first, last);
  78. }
  79. template<class Pred>
  80. char const*
  81. find_if_not(
  82. char const* first,
  83. char const* const last,
  84. Pred const& pred,
  85. std::false_type) noexcept
  86. {
  87. while(first != last)
  88. {
  89. if(! pred(*first))
  90. break;
  91. ++first;
  92. }
  93. return first;
  94. }
  95. template<class Pred>
  96. char const*
  97. find_if_not(
  98. char const* first,
  99. char const* const last,
  100. Pred const& pred,
  101. std::true_type) noexcept
  102. {
  103. return pred.find_if_not(
  104. first, last);
  105. }
  106. #ifdef BOOST_URL_USE_SSE2
  107. // by Peter Dimov
  108. template<class Pred>
  109. char const*
  110. find_if_pred(
  111. Pred const& pred,
  112. char const* first,
  113. char const* last ) noexcept
  114. {
  115. while( last - first >= 16 )
  116. {
  117. unsigned char r[ 16 ] = {};
  118. for( int i = 0; i < 16; ++i )
  119. r[ i ] = pred( first[ i ] )? 0xFF: 0x00;
  120. __m128i r2 = _mm_loadu_si128( (__m128i const*)r );
  121. unsigned r3 = _mm_movemask_epi8( r2 );
  122. if( r3 )
  123. return first + boost::core::countr_zero( r3 );
  124. first += 16;
  125. }
  126. while(
  127. first != last &&
  128. ! pred(*first))
  129. {
  130. ++first;
  131. }
  132. return first;
  133. }
  134. // by Peter Dimov
  135. template<class Pred>
  136. char const*
  137. find_if_not_pred(
  138. Pred const& pred,
  139. char const* first,
  140. char const* last ) noexcept
  141. {
  142. while( last - first >= 16 )
  143. {
  144. unsigned char r[ 16 ] = {};
  145. for( int i = 0; i < 16; ++i )
  146. r[ i ] = pred( first[ i ] )? 0x00: 0xFF;
  147. __m128i r2 = _mm_loadu_si128( (__m128i const*)r );
  148. unsigned r3 = _mm_movemask_epi8( r2 );
  149. if( r3 )
  150. return first + boost::core::countr_zero( r3 );
  151. first += 16;
  152. }
  153. while(
  154. first != last &&
  155. pred(*first))
  156. {
  157. ++first;
  158. }
  159. return first;
  160. }
  161. #endif
  162. } // detail
  163. } // grammar
  164. } // urls
  165. } // boost
  166. #ifdef _MSC_VER
  167. #pragma warning(pop)
  168. #endif
  169. #endif