Coverage Report

Created: 2025-06-24 06:38

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