| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_FORMAT_DETAULT_FLOATINGPOINT_HPP |
| 4 |
#define MPT_FORMAT_DETAULT_FLOATINGPOINT_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect.hpp" |
| 9 |
|
| 10 |
#if !defined(MPT_LIBCXX_QUIRK_NO_TO_CHARS_FLOAT) |
| 11 |
#define MPT_FORMAT_FORMAT_DEFAULT_FLOAT_CXX17 1 |
| 12 |
#else |
| 13 |
#define MPT_FORMAT_FORMAT_DEFAULT_FLOAT_CXX17 0 |
| 14 |
#endif |
| 15 |
|
| 16 |
#if MPT_FORMAT_FORMAT_DEFAULT_FLOAT_CXX17 |
| 17 |
#include "mpt/base/algorithm.hpp" |
| 18 |
#endif |
| 19 |
#include "mpt/base/namespace.hpp" |
| 20 |
#include "mpt/format/helpers.hpp" |
| 21 |
#include "mpt/string_transcode/transcode.hpp" |
| 22 |
|
| 23 |
#if MPT_FORMAT_FORMAT_DEFAULT_FLOAT_CXX17 |
| 24 |
#include <charconv> |
| 25 |
#endif |
| 26 |
#if !MPT_FORMAT_FORMAT_DEFAULT_FLOAT_CXX17 |
| 27 |
#include <iomanip> |
| 28 |
#include <ios> |
| 29 |
#include <limits> |
| 30 |
#include <locale> |
| 31 |
#include <sstream> |
| 32 |
#endif |
| 33 |
#include <string> |
| 34 |
#if MPT_FORMAT_FORMAT_DEFAULT_FLOAT_CXX17 |
| 35 |
#include <system_error> |
| 36 |
#endif |
| 37 |
#include <type_traits> |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
namespace mpt { |
| 42 |
inline namespace MPT_INLINE_NS { |
| 43 |
|
| 44 |
|
| 45 |
#if MPT_FORMAT_FORMAT_DEFAULT_FLOAT_CXX17 |
| 46 |
template <typename Tstring, typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true> |
| 47 |
inline Tstring to_chars_string(const T & x) { |
| 48 |
std::string str(1, '\0'); |
| 49 |
bool done = false; |
| 50 |
while (!done) { |
| 51 |
std::to_chars_result result = std::to_chars(str.data(), str.data() + str.size(), x); |
| 52 |
if (result.ec != std::errc{}) { |
| 53 |
str.resize(mpt::exponential_grow(str.size()), '\0'); |
| 54 |
} else { |
| 55 |
str.resize(result.ptr - str.data()); |
| 56 |
done = true; |
| 57 |
} |
| 58 |
} |
| 59 |
return mpt::convert_formatted_simple<Tstring>(str); |
| 60 |
} |
| 61 |
|
| 62 |
template <typename Tstring, typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true> |
| 63 |
inline Tstring format_value_default(const T & x) { |
| 64 |
return mpt::transcode<Tstring>(mpt::to_chars_string<typename mpt::select_format_string_type<Tstring>::type>(x)); |
| 65 |
} |
| 66 |
#endif |
| 67 |
|
| 68 |
|
| 69 |
#if !MPT_FORMAT_FORMAT_DEFAULT_FLOAT_CXX17 |
| 70 |
template <typename Tstring, typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true> |
| 71 |
inline Tstring to_stream_string(const T & x) { |
| 72 |
using stream_char_type = typename mpt::select_format_char_type<typename Tstring::value_type>::type; |
| 73 |
std::basic_ostringstream<stream_char_type> s; |
| 74 |
s.imbue(std::locale::classic()); |
| 75 |
s << std::setprecision(std::numeric_limits<T>::max_digits10) << x; |
| 76 |
return mpt::convert_formatted_simple<Tstring>(s.str()); |
| 77 |
} |
| 78 |
|
| 79 |
template <typename Tstring, typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true> |
| 80 |
inline Tstring format_value_default(const T & x) { |
| 81 |
return mpt::transcode<Tstring>(mpt::to_stream_string<typename mpt::select_format_string_type<Tstring>::type>(x)); |
| 82 |
} |
| 83 |
#endif |
| 84 |
|
| 85 |
|
| 86 |
} // namespace MPT_INLINE_NS |
| 87 |
} // namespace mpt |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
#endif // MPT_FORMAT_DETAULT_FLOATINGPOINT_HPP |