static_string.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_DETAIL_STATIC_STRING_HPP
  10. #define BOOST_BEAST_DETAIL_STATIC_STRING_HPP
  11. #include <boost/beast/core/string.hpp>
  12. #include <boost/assert.hpp>
  13. #include <iterator>
  14. #include <type_traits>
  15. namespace boost {
  16. namespace beast {
  17. namespace detail {
  18. // Because k-ballo said so
  19. template<class T>
  20. using is_input_iterator =
  21. std::integral_constant<bool,
  22. ! std::is_integral<T>::value>;
  23. template<class CharT, class Traits>
  24. int
  25. lexicographical_compare(
  26. CharT const* s1, std::size_t n1,
  27. CharT const* s2, std::size_t n2)
  28. {
  29. if(n1 < n2)
  30. return Traits::compare(
  31. s1, s2, n1) <= 0 ? -1 : 1;
  32. if(n1 > n2)
  33. return Traits::compare(
  34. s1, s2, n2) >= 0 ? 1 : -1;
  35. return Traits::compare(s1, s2, n1);
  36. }
  37. template<class CharT, class Traits>
  38. int
  39. lexicographical_compare(
  40. basic_string_view<CharT, Traits> s1,
  41. CharT const* s2, std::size_t n2)
  42. {
  43. return detail::lexicographical_compare<
  44. CharT, Traits>(s1.data(), s1.size(), s2, n2);
  45. }
  46. template<class CharT, class Traits>
  47. int
  48. lexicographical_compare(
  49. basic_string_view<CharT, Traits> s1,
  50. basic_string_view<CharT, Traits> s2)
  51. {
  52. return detail::lexicographical_compare<CharT, Traits>(
  53. s1.data(), s1.size(), s2.data(), s2.size());
  54. }
  55. // Maximum number of characters in the decimal
  56. // representation of a binary number. This includes
  57. // the potential minus sign.
  58. //
  59. inline
  60. std::size_t constexpr
  61. max_digits(std::size_t bytes)
  62. {
  63. return static_cast<std::size_t>(
  64. bytes * 2.41) + 1 + 1;
  65. }
  66. template<class CharT, class Integer, class Traits>
  67. CharT*
  68. raw_to_string(
  69. CharT* buf, Integer x, std::true_type)
  70. {
  71. if(x == 0)
  72. {
  73. Traits::assign(*--buf, '0');
  74. return buf;
  75. }
  76. if(x < 0)
  77. {
  78. x = -x;
  79. for(;x > 0; x /= 10)
  80. Traits::assign(*--buf ,
  81. "0123456789"[x % 10]);
  82. Traits::assign(*--buf, '-');
  83. return buf;
  84. }
  85. for(;x > 0; x /= 10)
  86. Traits::assign(*--buf ,
  87. "0123456789"[x % 10]);
  88. return buf;
  89. }
  90. template<class CharT, class Integer, class Traits>
  91. CharT*
  92. raw_to_string(
  93. CharT* buf, Integer x, std::false_type)
  94. {
  95. if(x == 0)
  96. {
  97. *--buf = '0';
  98. return buf;
  99. }
  100. for(;x > 0; x /= 10)
  101. Traits::assign(*--buf ,
  102. "0123456789"[x % 10]);
  103. return buf;
  104. }
  105. template<
  106. class CharT,
  107. class Integer,
  108. class Traits = std::char_traits<CharT>>
  109. CharT*
  110. raw_to_string(CharT* last, std::size_t size, Integer i)
  111. {
  112. boost::ignore_unused(size);
  113. BOOST_ASSERT(size >= max_digits(sizeof(Integer)));
  114. return raw_to_string<CharT, Integer, Traits>(
  115. last, i, std::is_signed<Integer>{});
  116. }
  117. } // detail
  118. } // beast
  119. } // boost
  120. #endif