QmlDesigner: proper std::to_chars float fallback

On older toolchains (e.g., RHEL 8, macOS < 13.3), std::to_chars
for floating-point types is may be partially implemented.

Task-number: QDS-14932
Change-Id: I63b17d640efa240e223f506333538a54b121de2c
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
Tim Jenßen
2025-03-14 12:32:39 +01:00
committed by Tim Jenssen
parent d829f57706
commit 3f0f5e0ae4

View File

@@ -5,14 +5,17 @@
#include <utils/smallstringview.h> #include <utils/smallstringview.h>
#if !(defined(__cpp_lib_to_chars) && (__cpp_lib_to_chars >= 201611L))
# include <QLocale>
#endif
#include <array> #include <array>
#include <charconv> #include <charconv>
#include <limits> #include <limits>
#if !defined(__cpp_lib_to_chars) || (__cpp_lib_to_chars < 201611L) || \
(defined(__GNUC__) && __GNUC__ < 11) || \
(defined(_MSC_VER) && _MSC_VER < 1930) || \
(defined(__clang_major__) && __clang_major__ < 14)
#define NO_STD_FLOAT_TO_CHARS 1
#endif
namespace NanotraceHR { namespace NanotraceHR {
template<std::size_t Capacity> template<std::size_t Capacity>
@@ -58,10 +61,14 @@ public:
template<typename Type, typename std::enable_if_t<std::is_arithmetic_v<Type>, bool> = true> template<typename Type, typename std::enable_if_t<std::is_arithmetic_v<Type>, bool> = true>
void append(Type number) void append(Type number)
{ {
#if !(defined(__cpp_lib_to_chars) && (__cpp_lib_to_chars >= 201611L)) #if NO_STD_FLOAT_TO_CHARS
// Fallback using snprintf with sufficient precision.
if constexpr (std::is_floating_point_v<Type>) { if constexpr (std::is_floating_point_v<Type>) {
QLocale locale{QLocale::Language::C}; char buffer[std::numeric_limits<Type>::max_digits10 + 2];
append(locale.toString(number).toStdString()); auto size = std::snprintf(buffer, sizeof(buffer), "%.9f", number);
if (size >= 0)
append({buffer, size});
return; return;
} }
#endif #endif