Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:25

0001 //
0002 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/beast
0008 //
0009 
0010 #ifndef BOOST_BEAST_DETAIL_STATIC_STRING_HPP
0011 #define BOOST_BEAST_DETAIL_STATIC_STRING_HPP
0012 
0013 #include <boost/assert.hpp>
0014 #include <boost/core/ignore_unused.hpp>
0015 #include <string>
0016 #include <type_traits>
0017 
0018 namespace boost {
0019 namespace beast {
0020 namespace detail {
0021 
0022 // Maximum number of characters in the decimal
0023 // representation of a binary number. This includes
0024 // the potential minus sign.
0025 //
0026 inline
0027 std::size_t constexpr
0028 max_digits(std::size_t bytes)
0029 {
0030     return static_cast<std::size_t>(
0031         bytes * 2.41) + 1 + 1;
0032 }
0033 
0034 template<class CharT, class Integer, class Traits>
0035 CharT*
0036 raw_to_string(
0037     CharT* buf, Integer x, std::true_type)
0038 {
0039     if(x == 0)
0040     {
0041         Traits::assign(*--buf, '0');
0042         return buf;
0043     }
0044     if(x < 0)
0045     {
0046         x = -x;
0047         for(;x > 0; x /= 10)
0048             Traits::assign(*--buf ,
0049                 "0123456789"[x % 10]);
0050         Traits::assign(*--buf, '-');
0051         return buf;
0052     }
0053     for(;x > 0; x /= 10)
0054         Traits::assign(*--buf ,
0055             "0123456789"[x % 10]);
0056     return buf;
0057 }
0058 
0059 template<class CharT, class Integer, class Traits>
0060 CharT*
0061 raw_to_string(
0062     CharT* buf, Integer x, std::false_type)
0063 {
0064     if(x == 0)
0065     {
0066         *--buf = '0';
0067         return buf;
0068     }
0069     for(;x > 0; x /= 10)
0070         Traits::assign(*--buf ,
0071             "0123456789"[x % 10]);
0072     return buf;
0073 }
0074 
0075 template<
0076     class CharT,
0077     class Integer,
0078     class Traits = std::char_traits<CharT>>
0079 CharT*
0080 raw_to_string(CharT* last, std::size_t size, Integer i)
0081 {
0082     boost::ignore_unused(size);
0083     BOOST_ASSERT(size >= max_digits(sizeof(Integer)));
0084     return raw_to_string<CharT, Integer, Traits>(
0085         last, i, std::is_signed<Integer>{});
0086 }
0087 
0088 } // detail
0089 } // beast
0090 } // boost
0091 
0092 #endif