mirror of
https://github.com/bolero-MURAKAMI/Sprout
synced 2024-11-12 21:09:01 +00:00
fix to_string
This commit is contained in:
parent
08def386b5
commit
e5391d339c
2 changed files with 126 additions and 171 deletions
|
@ -2,14 +2,11 @@
|
||||||
#define SPROUT_STRING_FLOAT_TO_STRING_HPP
|
#define SPROUT_STRING_FLOAT_TO_STRING_HPP
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <sprout/config.hpp>
|
#include <sprout/config.hpp>
|
||||||
#include <sprout/string/string.hpp>
|
#include <sprout/string/string.hpp>
|
||||||
#include <sprout/string/make_string.hpp>
|
|
||||||
#include <sprout/integer/integer_digits.hpp>
|
|
||||||
#include <sprout/utility/enabler_if.hpp>
|
#include <sprout/utility/enabler_if.hpp>
|
||||||
#include <sprout/detail/char_conversion.hpp>
|
#include <sprout/detail/char_conversion.hpp>
|
||||||
|
|
||||||
|
@ -25,113 +22,84 @@ namespace sprout {
|
||||||
struct printed_float_digits
|
struct printed_float_digits
|
||||||
: public std::integral_constant<
|
: public std::integral_constant<
|
||||||
std::size_t,
|
std::size_t,
|
||||||
/*std::numeric_limits<floatFloatType>::max_exponent10 + sprout::detail::decimal_places_length + 3*/
|
std::numeric_limits<FloatType>::max_exponent10 + sprout::detail::decimal_places_length + 2
|
||||||
sprout::integer_digits<std::intmax_t>::value + sprout::detail::decimal_places_length + 3
|
|
||||||
>
|
>
|
||||||
{};
|
{};
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
template<typename FloatType>
|
template<typename FloatType>
|
||||||
inline SPROUT_CONSTEXPR unsigned float_extract_rounded_impl(FloatType val) {
|
inline SPROUT_CONSTEXPR FloatType
|
||||||
return static_cast<unsigned>(val * 1000000)
|
float_pow10(int exponent) {
|
||||||
+ (static_cast<unsigned>(val * 10000000) % 10 >= 5 ? 1 : 0)
|
return exponent ? FloatType(10) * sprout::detail::float_pow10<FloatType>(exponent - 1)
|
||||||
|
: FloatType(1)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename FloatType>
|
||||||
|
inline SPROUT_CONSTEXPR int
|
||||||
|
float_digits_impl(FloatType val) {
|
||||||
|
return !(val < 1 && val > -1) ? 1 + sprout::detail::float_digits_impl(val / 10)
|
||||||
|
: 0
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
template<typename FloatType>
|
template<typename FloatType>
|
||||||
inline SPROUT_CONSTEXPR unsigned float_extract_rounded(FloatType val) {
|
inline SPROUT_CONSTEXPR int
|
||||||
|
float_digits(FloatType val) {
|
||||||
|
return !(val < 1 && val > -1) ? 1 + sprout::detail::float_digits_impl(val / 10)
|
||||||
|
: 1
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename FloatType>
|
||||||
|
inline SPROUT_CONSTEXPR int
|
||||||
|
float_digit_of_impl(FloatType val) {
|
||||||
using std::floor;
|
using std::floor;
|
||||||
return sprout::detail::float_extract_rounded_impl(val - floor(val));
|
return static_cast<int>((val - floor(val)) * 10);
|
||||||
}
|
}
|
||||||
|
template<typename FloatType>
|
||||||
template<
|
inline SPROUT_CONSTEXPR int
|
||||||
typename Elem,
|
float_digit_of(FloatType val, int digits) {
|
||||||
typename FloatType,
|
return digits < 0 ? sprout::detail::float_digit_of_impl(val * sprout::detail::float_pow10<FloatType>(-digits - 1))
|
||||||
typename... Args,
|
: sprout::detail::float_digit_of_impl(val / sprout::detail::float_pow10<FloatType>(digits + 1))
|
||||||
typename sprout::enabler_if<(sizeof...(Args) == sprout::printed_float_digits<FloatType>::value - 1)>::type = sprout::enabler
|
|
||||||
>
|
|
||||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
|
|
||||||
float_to_string_impl_1(FloatType val, bool negative, Args... args) {
|
|
||||||
return negative ? sprout::make_string_as<Elem>(static_cast<Elem>('-'), args...)
|
|
||||||
: sprout::make_string_as<Elem>(args...)
|
|
||||||
;
|
|
||||||
}
|
|
||||||
template<
|
|
||||||
typename Elem,
|
|
||||||
typename FloatType,
|
|
||||||
typename... Args,
|
|
||||||
typename sprout::enabler_if<(sizeof...(Args) < sprout::printed_float_digits<FloatType>::value - 1)>::type = sprout::enabler
|
|
||||||
>
|
|
||||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
|
|
||||||
float_to_string_impl_1(FloatType val, bool negative, Args... args) {
|
|
||||||
return !(val < 1) ? sprout::detail::float_to_string_impl_1<Elem>(
|
|
||||||
val / 10,
|
|
||||||
negative,
|
|
||||||
sprout::detail::int_to_char<Elem>(static_cast<std::uintmax_t>(val) % 10),
|
|
||||||
args...
|
|
||||||
)
|
|
||||||
: negative ? sprout::make_string_as<Elem>(static_cast<Elem>('-'), args...)
|
|
||||||
: sprout::make_string_as<Elem>(args...)
|
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<
|
template<typename FloatType>
|
||||||
typename Elem,
|
inline SPROUT_CONSTEXPR FloatType
|
||||||
typename FloatType,
|
float_round_impl(FloatType val, FloatType p10) {
|
||||||
typename... Args,
|
using std::round;
|
||||||
typename sprout::enabler_if<(sizeof...(Args) == sprout::printed_float_digits<FloatType>::value - 3)>::type = sprout::enabler
|
return round(val * p10) / p10;
|
||||||
>
|
|
||||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
|
|
||||||
float_to_string_impl(FloatType val, bool negative, unsigned temp, std::size_t decimal_digits, Args... args) {
|
|
||||||
return negative ? sprout::make_string_as<Elem>(static_cast<Elem>('-'), static_cast<Elem>('0'), static_cast<Elem>('.'), args...)
|
|
||||||
: sprout::make_string_as<Elem>(static_cast<Elem>('0'), static_cast<Elem>('.'), args...)
|
|
||||||
;
|
|
||||||
}
|
}
|
||||||
template<
|
template<typename FloatType>
|
||||||
typename Elem,
|
inline SPROUT_CONSTEXPR FloatType
|
||||||
typename FloatType,
|
float_round(FloatType val, int digits) {
|
||||||
typename... Args,
|
return sprout::detail::float_round_impl(val, sprout::detail::float_pow10<FloatType>(digits));
|
||||||
typename sprout::enabler_if<(sizeof...(Args) < sprout::printed_float_digits<FloatType>::value - 3)>::type = sprout::enabler
|
|
||||||
>
|
|
||||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
|
|
||||||
float_to_string_impl(FloatType val, bool negative, unsigned temp, std::size_t decimal_digits, Args... args) {
|
|
||||||
return decimal_digits ? sprout::detail::float_to_string_impl<Elem>(
|
|
||||||
val,
|
|
||||||
negative,
|
|
||||||
temp / 10,
|
|
||||||
decimal_digits - 1,
|
|
||||||
sprout::detail::int_to_char<Elem>(temp % 10),
|
|
||||||
args...
|
|
||||||
)
|
|
||||||
: !(val < 1) ? sprout::detail::float_to_string_impl_1<Elem>(
|
|
||||||
val,
|
|
||||||
negative,
|
|
||||||
static_cast<Elem>('.'),
|
|
||||||
args...
|
|
||||||
)
|
|
||||||
: negative ? sprout::make_string_as<Elem>(static_cast<Elem>('-'), static_cast<Elem>('0'), static_cast<Elem>('.'), args...)
|
|
||||||
: sprout::make_string_as<Elem>(static_cast<Elem>('0'), static_cast<Elem>('.'), args...)
|
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<
|
template<typename Elem, typename FloatType, sprout::index_t... Indexes>
|
||||||
typename Elem,
|
|
||||||
typename FloatType
|
|
||||||
>
|
|
||||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
|
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
|
||||||
float_to_string(FloatType val) {
|
float_to_string(FloatType val, bool negative, int digits, sprout::index_tuple<Indexes...>) {
|
||||||
using std::floor;
|
return negative ? sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>{
|
||||||
return val < 0 ? sprout::detail::float_to_string_impl<Elem>(
|
{
|
||||||
-val,
|
static_cast<Elem>('-'),
|
||||||
true,
|
(Indexes < digits ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_of(val, digits - 1 - Indexes))
|
||||||
sprout::detail::float_extract_rounded(-val),
|
: Indexes == digits ? static_cast<Elem>('.')
|
||||||
sprout::detail::decimal_places_length
|
: Indexes < digits + 1 + sprout::detail::decimal_places_length ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_of(val, digits - Indexes))
|
||||||
)
|
: Elem()
|
||||||
: sprout::detail::float_to_string_impl<Elem>(
|
)...
|
||||||
val,
|
},
|
||||||
false,
|
static_cast<std::size_t>(digits + 2 + sprout::detail::decimal_places_length)
|
||||||
sprout::detail::float_extract_rounded(val),
|
}
|
||||||
sprout::detail::decimal_places_length
|
: sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>{
|
||||||
)
|
{
|
||||||
|
(Indexes < digits ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_of(val, digits - 1 - Indexes))
|
||||||
|
: Indexes == digits ? static_cast<Elem>('.')
|
||||||
|
: Indexes < digits + 1 + sprout::detail::decimal_places_length ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_of(val, digits - Indexes))
|
||||||
|
: Elem()
|
||||||
|
)...
|
||||||
|
},
|
||||||
|
static_cast<std::size_t>(digits + 1 + sprout::detail::decimal_places_length)
|
||||||
|
}
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
@ -146,7 +114,12 @@ namespace sprout {
|
||||||
>
|
>
|
||||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
|
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
|
||||||
float_to_string(FloatType val) {
|
float_to_string(FloatType val) {
|
||||||
return sprout::detail::float_to_string<Elem>(val);
|
return sprout::detail::float_to_string<Elem>(
|
||||||
|
sprout::detail::float_round(val < 0 ? -val : val, sprout::detail::decimal_places_length),
|
||||||
|
val < 0,
|
||||||
|
sprout::detail::float_digits(val),
|
||||||
|
sprout::index_range<0, sprout::printed_float_digits<FloatType>::value - 1>::make()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
@ -4,8 +4,8 @@
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <sprout/config.hpp>
|
#include <sprout/config.hpp>
|
||||||
|
#include <sprout/index_tuple.hpp>
|
||||||
#include <sprout/string/string.hpp>
|
#include <sprout/string/string.hpp>
|
||||||
#include <sprout/string/make_string.hpp>
|
|
||||||
#include <sprout/integer/integer_digits.hpp>
|
#include <sprout/integer/integer_digits.hpp>
|
||||||
#include <sprout/utility/enabler_if.hpp>
|
#include <sprout/utility/enabler_if.hpp>
|
||||||
#include <sprout/detail/char_conversion.hpp>
|
#include <sprout/detail/char_conversion.hpp>
|
||||||
|
@ -23,82 +23,34 @@ namespace sprout {
|
||||||
{};
|
{};
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
template<
|
template<typename IntType, int Base>
|
||||||
typename Elem,
|
inline SPROUT_CONSTEXPR IntType
|
||||||
int Base,
|
int_pow(int exponent) {
|
||||||
typename IntType,
|
return exponent ? Base * sprout::detail::int_pow<IntType, Base>(exponent - 1)
|
||||||
typename... Args,
|
: 1
|
||||||
typename sprout::enabler_if<(sizeof...(Args) == sprout::printed_integer_digits<IntType, Base>::value - 1)>::type = sprout::enabler
|
|
||||||
>
|
|
||||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_integer_digits<IntType, Base>::value>
|
|
||||||
int_to_string_impl_1(IntType val, bool negative, Args... args) {
|
|
||||||
return negative ? sprout::make_string_as<Elem>(static_cast<Elem>('-'), args...)
|
|
||||||
: sprout::make_string_as<Elem>(args...)
|
|
||||||
;
|
|
||||||
}
|
|
||||||
template<
|
|
||||||
typename Elem,
|
|
||||||
int Base,
|
|
||||||
typename IntType,
|
|
||||||
typename... Args,
|
|
||||||
typename sprout::enabler_if<(sizeof...(Args) < sprout::printed_integer_digits<IntType, Base>::value - 1)>::type = sprout::enabler
|
|
||||||
>
|
|
||||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_integer_digits<IntType, Base>::value>
|
|
||||||
int_to_string_impl_1(IntType val, bool negative, Args... args) {
|
|
||||||
return val == 0
|
|
||||||
? (negative ? sprout::make_string_as<Elem>(static_cast<Elem>('-'), args...)
|
|
||||||
: sprout::make_string_as<Elem>(args...)
|
|
||||||
)
|
|
||||||
: sprout::detail::int_to_string_impl_1<Elem, Base>(
|
|
||||||
val / Base,
|
|
||||||
negative,
|
|
||||||
sprout::detail::int_to_char<Elem>(negative ? -(val % Base) : val % Base, Base),
|
|
||||||
args...
|
|
||||||
)
|
|
||||||
;
|
|
||||||
}
|
|
||||||
template<typename Elem, int Base, typename IntType>
|
|
||||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_integer_digits<IntType, Base>::value>
|
|
||||||
int_to_string_impl(IntType val) {
|
|
||||||
return val == 0 ? sprout::make_string_as<Elem>(static_cast<Elem>('0'))
|
|
||||||
: sprout::detail::int_to_string_impl_1<Elem, Base>(val, val < 0)
|
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<
|
template<int Base, typename IntType>
|
||||||
typename Elem,
|
inline SPROUT_CONSTEXPR int
|
||||||
int Base,
|
int_digits_impl(IntType val) {
|
||||||
typename UIntType,
|
return val ? 1 + sprout::detail::int_digits_impl<Base>(val / Base)
|
||||||
typename... Args,
|
: 0
|
||||||
typename sprout::enabler_if<(sizeof...(Args) == sprout::printed_integer_digits<UIntType, Base>::value)>::type = sprout::enabler
|
|
||||||
>
|
|
||||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_integer_digits<UIntType, Base>::value>
|
|
||||||
uint_to_string_impl_1(UIntType val, Args... args) {
|
|
||||||
return sprout::make_string_as<Elem>(args...);
|
|
||||||
}
|
|
||||||
template<
|
|
||||||
typename Elem,
|
|
||||||
int Base,
|
|
||||||
typename UIntType,
|
|
||||||
typename... Args,
|
|
||||||
typename sprout::enabler_if<(sizeof...(Args) < sprout::printed_integer_digits<UIntType, Base>::value)>::type = sprout::enabler
|
|
||||||
>
|
|
||||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_integer_digits<UIntType, Base>::value>
|
|
||||||
uint_to_string_impl_1(UIntType val, Args... args) {
|
|
||||||
return val == 0
|
|
||||||
? sprout::make_string_as<Elem>(args...)
|
|
||||||
: sprout::detail::uint_to_string_impl_1<Elem, Base>(
|
|
||||||
val / Base,
|
|
||||||
sprout::detail::int_to_char<Elem>(val % Base, Base),
|
|
||||||
args...
|
|
||||||
)
|
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
template<typename Elem, int Base, typename UIntType>
|
template<int Base, typename IntType>
|
||||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_integer_digits<UIntType, Base>::value>
|
inline SPROUT_CONSTEXPR int
|
||||||
uint_to_string_impl(UIntType val) {
|
int_digits(IntType val) {
|
||||||
return val == 0 ? sprout::make_string_as<Elem>(static_cast<Elem>('0'))
|
return val ? 1 + sprout::detail::int_digits_impl<Base>(val / Base)
|
||||||
: sprout::detail::uint_to_string_impl_1<Elem, Base>(val)
|
: 1
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<int Base, typename IntType>
|
||||||
|
inline SPROUT_CONSTEXPR int
|
||||||
|
int_digit_of(IntType val, int digits) {
|
||||||
|
return val < 0 ? -((val / sprout::detail::int_pow<IntType, Base>(digits)) % Base)
|
||||||
|
: (val / sprout::detail::int_pow<IntType, Base>(digits)) % Base
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,21 +58,47 @@ namespace sprout {
|
||||||
typename Elem,
|
typename Elem,
|
||||||
int Base,
|
int Base,
|
||||||
typename IntType,
|
typename IntType,
|
||||||
|
sprout::index_t... Indexes,
|
||||||
typename sprout::enabler_if<std::is_signed<IntType>::value>::type = sprout::enabler
|
typename sprout::enabler_if<std::is_signed<IntType>::value>::type = sprout::enabler
|
||||||
>
|
>
|
||||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_integer_digits<IntType, Base>::value>
|
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_integer_digits<IntType, Base>::value>
|
||||||
int_to_string(IntType val) {
|
int_to_string(IntType val, int digits, sprout::index_tuple<Indexes...>) {
|
||||||
return sprout::detail::int_to_string_impl<Elem, Base>(val);
|
return val < 0 ? sprout::basic_string<Elem, sprout::printed_integer_digits<IntType, Base>::value>{
|
||||||
|
{
|
||||||
|
static_cast<Elem>('-'),
|
||||||
|
(Indexes < digits ? sprout::detail::int_to_char<Elem>(sprout::detail::int_digit_of<Base>(val, digits - 1 - Indexes))
|
||||||
|
: Elem()
|
||||||
|
)...
|
||||||
|
},
|
||||||
|
static_cast<std::size_t>(digits + 1)
|
||||||
|
}
|
||||||
|
: sprout::basic_string<Elem, sprout::printed_integer_digits<IntType, Base>::value>{
|
||||||
|
{
|
||||||
|
(Indexes < digits ? sprout::detail::int_to_char<Elem>(sprout::detail::int_digit_of<Base>(val, digits - 1 - Indexes))
|
||||||
|
: Elem()
|
||||||
|
)...
|
||||||
|
},
|
||||||
|
static_cast<std::size_t>(digits)
|
||||||
|
}
|
||||||
|
;
|
||||||
}
|
}
|
||||||
template<
|
template<
|
||||||
typename Elem,
|
typename Elem,
|
||||||
int Base,
|
int Base,
|
||||||
typename IntType,
|
typename IntType,
|
||||||
|
sprout::index_t... Indexes,
|
||||||
typename sprout::enabler_if<std::is_unsigned<IntType>::value>::type = sprout::enabler
|
typename sprout::enabler_if<std::is_unsigned<IntType>::value>::type = sprout::enabler
|
||||||
>
|
>
|
||||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_integer_digits<IntType, Base>::value>
|
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_integer_digits<IntType, Base>::value>
|
||||||
int_to_string(IntType val) {
|
int_to_string(IntType val, int digits, sprout::index_tuple<Indexes...>) {
|
||||||
return sprout::detail::uint_to_string_impl<Elem, Base>(val);
|
return sprout::basic_string<Elem, sprout::printed_integer_digits<IntType, Base>::value>{
|
||||||
|
{
|
||||||
|
(Indexes < digits ? sprout::detail::int_to_char<Elem>(sprout::detail::int_digit_of<Base>(val, digits - 1 - Indexes))
|
||||||
|
: Elem()
|
||||||
|
)...
|
||||||
|
},
|
||||||
|
static_cast<std::size_t>(digits)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
|
@ -135,7 +113,11 @@ namespace sprout {
|
||||||
>
|
>
|
||||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_integer_digits<IntType, Base>::value>
|
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_integer_digits<IntType, Base>::value>
|
||||||
int_to_string(IntType val) {
|
int_to_string(IntType val) {
|
||||||
return sprout::detail::int_to_string<Elem, Base>(val);
|
return sprout::detail::int_to_string<Elem, Base>(
|
||||||
|
val,
|
||||||
|
sprout::detail::int_digits<Base>(val),
|
||||||
|
sprout::index_range<0, sprout::integer_digits<IntType, Base>::value>::make()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
Loading…
Reference in a new issue