/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ #ifndef MPT_FORMAT_DEFAULT_INTEGER_HPP #define MPT_FORMAT_DEFAULT_INTEGER_HPP #include "mpt/base/detect.hpp" #if !defined(MPT_LIBCXX_QUIRK_NO_TO_CHARS_INT) #define MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17 1 #else // MPT_LIBCXX_QUIRK_NO_TO_CHARS_INT #define MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17 0 #endif // !MPT_LIBCXX_QUIRK_NO_TO_CHARS_INT #if MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17 #include "mpt/base/algorithm.hpp" #endif // MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17 #include "mpt/base/namespace.hpp" #include "mpt/base/utility.hpp" #include "mpt/format/helpers.hpp" #include "mpt/string_transcode/transcode.hpp" #if MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17 #include #endif // MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17 #if !MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17 #include #include #endif // !MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17 #include #if MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17 #include #endif // MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17 #include namespace mpt { inline namespace MPT_INLINE_NS { #if MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17 template ::value, bool> = true> inline Tstring to_chars_string(const T & x) { std::string str(1, '\0'); bool done = false; while (!done) { if constexpr (std::is_same::value) { std::to_chars_result result = std::to_chars(str.data(), str.data() + str.size(), static_cast(x)); if (result.ec != std::errc{}) { str.resize(mpt::exponential_grow(str.size()), '\0'); } else { str.resize(result.ptr - str.data()); done = true; } } else { std::to_chars_result result = std::to_chars(str.data(), str.data() + str.size(), x); if (result.ec != std::errc{}) { str.resize(mpt::exponential_grow(str.size()), '\0'); } else { str.resize(result.ptr - str.data()); done = true; } } } return mpt::convert_formatted_simple(str); } template ::value, bool> = true> inline Tstring format_value_default(const T & x) { return mpt::transcode(mpt::to_chars_string::type>(x)); } #endif // MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17 #if !MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17 template ::value, bool> = true> inline Tstring to_stream_string(const T & x) { using stream_char_type = typename mpt::select_format_char_type::type; std::basic_ostringstream s; s.imbue(std::locale::classic()); if constexpr (std::is_same::value) { s << static_cast(x); } else if constexpr (mpt::is_character::value) { s << (x + 0); // force integral promotion } else { s << x; } return mpt::convert_formatted_simple(s.str()); } template ::value, bool> = true> inline Tstring format_value_default(const T & x) { return mpt::transcode(mpt::to_stream_string::type>(x)); } #endif // !MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17 template ::value, bool> = true> inline Tstring format_value_default(const T & x) { return mpt::format_value_default(mpt::to_underlying(x)); } } // namespace MPT_INLINE_NS } // namespace mpt #endif // MPT_FORMAT_DEFAULT_INTEGER_HPP