7#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_STRINGS_FORMAT_TO_INLINE_STRING_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_STRINGS_FORMAT_TO_INLINE_STRING_HPP_
17#include "arene/base/detail/raw_c_string.hpp"
19#include "arene/base/stdlib_choice/climits.hpp"
20#include "arene/base/stdlib_choice/cstddef.hpp"
21#include "arene/base/stdlib_choice/cstdint.hpp"
22#include "arene/base/stdlib_choice/enable_if.hpp"
23#include "arene/base/stdlib_choice/ignore.hpp"
24#include "arene/base/stdlib_choice/integral_constant.hpp"
25#include "arene/base/stdlib_choice/is_integral.hpp"
26#include "arene/base/stdlib_choice/is_signed.hpp"
27#include "arene/base/stdlib_choice/is_unsigned.hpp"
28#include "arene/base/stdlib_choice/make_unsigned.hpp"
29#include "arene/base/stdlib_choice/numeric_limits.hpp"
38namespace to_string_detail {
44inline constexpr auto digits_for_integral_value() ->
std::size_t {
48 constexpr std::size_t log2_of_10_scaled_by_2_to_25{111465410U};
50 return ((
sizeof(T) *
static_cast<std::size_t>(CHAR_BIT) - signed_digit_size) * two_to_the_25th +
51 log2_of_10_scaled_by_2_to_25 - 1U) /
52 log2_of_10_scaled_by_2_to_25 +
58template <
typename T,
typename = constra
ints<>>
59class required_decimal_digits;
66 :
public std::integral_constant<std::size_t, digits_for_integral_value<T>()> {};
71class required_decimal_digits<
bool> :
public std::integral_constant<std::size_t, 5> {};
76extern constexpr auto required_decimal_digits_v = required_decimal_digits<T>::value;
82template <
typename T, std::
size_t S>
83extern constexpr bool is_large_enough_for{S >= required_decimal_digits_v<T>};
88template <typename T, constraints<std::enable_if_t<std::is_unsigned<T>::value>> =
nullptr>
89constexpr auto is_negative_value(T)
noexcept ->
bool {
97template <typename T, constraints<std::enable_if_t<std::is_signed<T>::value>> =
nullptr>
98constexpr auto is_negative_value(T value)
noexcept ->
bool {
106inline constexpr auto digit(
std::uintmax_t const value)
noexcept -> arene::base::detail::character {
108 constexpr array<arene::base::detail::character, 10> digits{{
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9'}};
118extern constexpr T zero_v{0};
130 std::size_t S = to_string_detail::required_decimal_digits_v<T>,
132 std::enable_if_t<std::is_integral<T>::value>,
135 if (value == to_string_detail::zero_v<T>) {
138 bool const is_negative{to_string_detail::is_negative_value(value)};
140 unsigned_t unsigned_value{0U};
142 unsigned_value =
static_cast<unsigned_t
>(
power_of_2((
sizeof(T) *
static_cast<std::size_t>(CHAR_BIT)) - 1U));
143 }
else if (is_negative) {
144 unsigned_value =
static_cast<unsigned_t
>(-value);
146 unsigned_value =
static_cast<unsigned_t
>(value);
148 constexpr unsigned_t base_divisor{10U};
149 constexpr std::size_t num_digits{to_string_detail::required_decimal_digits_v<T>};
152 result.resize(num_digits);
154 while (unsigned_value != 0U) {
155 result[pos] = to_string_detail::digit(unsigned_value % base_divisor);
156 unsigned_value = unsigned_value / base_divisor;
176 std::size_t S = to_string_detail::required_decimal_digits_v<T>,
A string type with a fixed maximum capacity, where the data is stored internally without allocations.
Definition inline_string.hpp:162
A sum type which can hold either a value or an error. Similar to std::expected.
Definition result.hpp:104
static constexpr value_type value
Definition integral_constant.hpp:34
static constexpr auto min() noexcept -> T
The minimum finite value; the minimum positive normalized value for floating point types.
Definition numeric_limits.hpp:235
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition accumulate.hpp:37
constexpr auto power_of_2(std::uint64_t const value) noexcept -> std::uint64_t
Raises 2 to the power of a value.
Definition power_of_2.hpp:54
constexpr auto to_inline_string(T value) noexcept -> inline_string< S >
Produces a string representation of an input integral value in base-10.
Definition to_inline_string.hpp:134
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
bool
Check if element Idx and all subsequent elements of the two tuples are equal.
Definition tuple.hpp:649
::uintmax_t uintmax_t
A maximum-width unsigned integer type.
Definition cstdint.hpp:51
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
static constexpr auto const & ignore
An object to which anything can be assigned, which ignores the assigned value.
Definition ignore.hpp:72
typename make_unsigned< Type >::type make_unsigned_t
A type alias for the equivalent unsigned type for integral and enumeration types other than bool.
Definition make_unsigned.hpp:252