forked from qt-creator/qt-creator
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:
@@ -5,14 +5,17 @@
|
||||
|
||||
#include <utils/smallstringview.h>
|
||||
|
||||
#if !(defined(__cpp_lib_to_chars) && (__cpp_lib_to_chars >= 201611L))
|
||||
# include <QLocale>
|
||||
#endif
|
||||
|
||||
#include <array>
|
||||
#include <charconv>
|
||||
#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 {
|
||||
|
||||
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>
|
||||
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>) {
|
||||
QLocale locale{QLocale::Language::C};
|
||||
append(locale.toString(number).toStdString());
|
||||
char buffer[std::numeric_limits<Type>::max_digits10 + 2];
|
||||
auto size = std::snprintf(buffer, sizeof(buffer), "%.9f", number);
|
||||
|
||||
if (size >= 0)
|
||||
append({buffer, size});
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user