]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/beast/core/detail/static_string.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / beast / core / detail / static_string.hpp
CommitLineData
b32b8144 1//
92f5a8d4 2// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
b32b8144
FG
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
18namespace boost {
19namespace beast {
20namespace detail {
21
22// Because k-ballo said so
23template<class T>
24using is_input_iterator =
25 std::integral_constant<bool,
26 ! std::is_integral<T>::value>;
27
28template<class CharT, class Traits>
29int
30lexicographical_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
43template<class CharT, class Traits>
b32b8144
FG
44int
45lexicographical_compare(
46 basic_string_view<CharT, Traits> s1,
47 CharT const* s2, std::size_t n2)
48{
92f5a8d4
TL
49 return detail::lexicographical_compare<
50 CharT, Traits>(s1.data(), s1.size(), s2, n2);
b32b8144
FG
51}
52
53template<class CharT, class Traits>
b32b8144
FG
54int
55lexicographical_compare(
56 basic_string_view<CharT, Traits> s1,
57 basic_string_view<CharT, Traits> s2)
58{
92f5a8d4 59 return detail::lexicographical_compare<CharT, Traits>(
b32b8144
FG
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//
67inline
68std::size_t constexpr
69max_digits(std::size_t bytes)
70{
71 return static_cast<std::size_t>(
72 bytes * 2.41) + 1 + 1;
73}
74
75template<class CharT, class Integer, class Traits>
76CharT*
77raw_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
100template<class CharT, class Integer, class Traits>
101CharT*
102raw_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
116template<
117 class CharT,
118 class Integer,
119 class Traits = std::char_traits<CharT>>
120CharT*
121raw_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