-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlimits.hpp
96 lines (65 loc) · 2.88 KB
/
limits.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2023 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_CHARCONV_LIMITS_HPP
#define BOOST_CHARCONV_LIMITS_HPP
#include <boost/charconv/detail/config.hpp>
#include <limits>
#include <type_traits>
namespace boost { namespace charconv {
// limits<T>::max_chars10: the minimum size of the buffer that needs to be
// passed to to_chars to guarantee successful conversion for all values of
// type T, when either no base is passed, or base 10 is passed
//
// limits<T>::max_chars: the minimum size of the buffer that needs to be
// passed to to_chars to guarantee successful conversion for all values of
// type T, for any value of base
namespace detail
{
constexpr int exp_digits( int exp )
{
return exp < 100? 2: exp < 1000? 3: exp < 10000? 4: 5;
}
#if defined(BOOST_HAS_INT128)
template<class T> struct is_int128: std::is_same<T, boost::int128_type> {};
template<class T> struct is_uint128: std::is_same<T, boost::int128_type> {};
#else
template<class T> struct is_int128: std::false_type {};
template<class T> struct is_uint128: std::false_type {};
#endif
} // namespace detail
template<typename T> struct limits
{
BOOST_ATTRIBUTE_UNUSED static constexpr int max_chars10 =
// int128_t
detail::is_int128<T>::value? 38+2: // digits10 + 1 + sign
// uint128_t
detail::is_uint128<T>::value? 38+1: // digits10 + 1
// integral
std::numeric_limits<T>::is_integer? std::numeric_limits<T>::digits10 + 1 + std::numeric_limits<T>::is_signed:
// floating point
std::numeric_limits<T>::max_digits10 + 3 + 2 + detail::exp_digits( std::numeric_limits<T>::max_exponent10 ); // -1.(max_digits10)e+(max_exp)
BOOST_ATTRIBUTE_UNUSED static constexpr int max_chars =
// int128_t
detail::is_int128<T>::value? 127+2: // digits + 1 + sign
// uint128_t
detail::is_uint128<T>::value? 128+1: // digits + 1
// integral
std::numeric_limits<T>::is_integer? std::numeric_limits<T>::digits + 1 + std::numeric_limits<T>::is_signed:
// floating point
std::numeric_limits<T>::max_digits10 + 3 + 2 + detail::exp_digits( std::numeric_limits<T>::max_exponent10 ); // as above
};
#ifdef BOOST_CHARCONV_HAS_FLOAT128
template <> struct limits<__float128>
{
BOOST_ATTRIBUTE_UNUSED static constexpr int max_chars10 = 33 + 3 + 2 + 5;
BOOST_ATTRIBUTE_UNUSED static constexpr int max_chars = max_chars10;
};
#endif
#if defined(BOOST_NO_CXX17_INLINE_VARIABLES)
// Definitions of in-class constexpr members are allowed but deprecated in C++17
template<typename T> BOOST_ATTRIBUTE_UNUSED constexpr int limits<T>::max_chars10;
template<typename T> BOOST_ATTRIBUTE_UNUSED constexpr int limits<T>::max_chars;
#endif // defined(BOOST_NO_CXX17_INLINE_VARIABLES)
}} // namespace boost::charconv
#endif // BOOST_CHARCONV_LIMITS_HPP