• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 #ifndef BOOST_BEAST_DETAIL_STATIC_STRING_HPP
11 #define BOOST_BEAST_DETAIL_STATIC_STRING_HPP
12 
13 #include <boost/beast/core/string.hpp>
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
lexicographical_compare(CharT const * s1,std::size_t n1,CharT const * s2,std::size_t n2)30 lexicographical_compare(
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 int
lexicographical_compare(basic_string_view<CharT,Traits> s1,CharT const * s2,std::size_t n2)45 lexicographical_compare(
46     basic_string_view<CharT, Traits> s1,
47     CharT const* s2, std::size_t n2)
48 {
49     return detail::lexicographical_compare<
50         CharT, Traits>(s1.data(), s1.size(), s2, n2);
51 }
52 
53 template<class CharT, class Traits>
54 int
lexicographical_compare(basic_string_view<CharT,Traits> s1,basic_string_view<CharT,Traits> s2)55 lexicographical_compare(
56     basic_string_view<CharT, Traits> s1,
57     basic_string_view<CharT, Traits> s2)
58 {
59     return detail::lexicographical_compare<CharT, Traits>(
60         s1.data(), s1.size(), s2.data(), s2.size());
61 }
62 
63 // Maximum number of characters in the decimal
64 // representation of a binary number. This includes
65 // the potential minus sign.
66 //
67 inline
68 std::size_t constexpr
max_digits(std::size_t bytes)69 max_digits(std::size_t bytes)
70 {
71     return static_cast<std::size_t>(
72         bytes * 2.41) + 1 + 1;
73 }
74 
75 template<class CharT, class Integer, class Traits>
76 CharT*
raw_to_string(CharT * buf,Integer x,std::true_type)77 raw_to_string(
78     CharT* buf, Integer x, std::true_type)
79 {
80     if(x == 0)
81     {
82         Traits::assign(*--buf, '0');
83         return buf;
84     }
85     if(x < 0)
86     {
87         x = -x;
88         for(;x > 0; x /= 10)
89             Traits::assign(*--buf ,
90                 "0123456789"[x % 10]);
91         Traits::assign(*--buf, '-');
92         return buf;
93     }
94     for(;x > 0; x /= 10)
95         Traits::assign(*--buf ,
96             "0123456789"[x % 10]);
97     return buf;
98 }
99 
100 template<class CharT, class Integer, class Traits>
101 CharT*
raw_to_string(CharT * buf,Integer x,std::false_type)102 raw_to_string(
103     CharT* buf, Integer x, std::false_type)
104 {
105     if(x == 0)
106     {
107         *--buf = '0';
108         return buf;
109     }
110     for(;x > 0; x /= 10)
111         Traits::assign(*--buf ,
112             "0123456789"[x % 10]);
113     return buf;
114 }
115 
116 template<
117     class CharT,
118     class Integer,
119     class Traits = std::char_traits<CharT>>
120 CharT*
121 raw_to_string(CharT* last, std::size_t size, Integer i)
122 {
123     boost::ignore_unused(size);
124     BOOST_ASSERT(size >= max_digits(sizeof(Integer)));
125     return raw_to_string<CharT, Integer, Traits>(
126         last, i, std::is_signed<Integer>{});
127 }
128 
129 } // detail
130 } // beast
131 } // boost
132 
133 #endif
134