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