diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2020-07-10 12:30:12 +0200 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2020-07-13 12:05:32 +0200 |
commit | e8d5000026dbad4f48dfed882f4b19d6c4b34c67 (patch) | |
tree | c73f3ada21e7fca6d89d063424157c1f4585f817 /src/corelib/text/qlocale_tools.cpp | |
parent | 72be9bdd376453dbce35663fd685290474cd1e39 (diff) |
Use numeric_limits instead of hand-coded equivalents
As a comment noted, the reason for QLocaleData rolling its own values
describing the ranges of digits and exponents in a double were all
about std::numeric_limits's constants not being constexpr - which they
have now been since C++11, so we can do away with our own.
One of the constants was used in two places in the same way; so
abstract that use out into an inline function in qlocale_tools, to
save duplication and give somewhere to document it.
Change-Id: I7e3740ece9b499c0ec434de18d70abe69e1fe079
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text/qlocale_tools.cpp')
-rw-r--r-- | src/corelib/text/qlocale_tools.cpp | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/src/corelib/text/qlocale_tools.cpp b/src/corelib/text/qlocale_tools.cpp index a1838add72..874f42c8e5 100644 --- a/src/corelib/text/qlocale_tools.cpp +++ b/src/corelib/text/qlocale_tools.cpp @@ -139,7 +139,7 @@ void qt_doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, cha if (precision > 999) precision = 999; else if (precision == QLocale::FloatingPointShortest) - precision = QLocaleData::DoubleMaxSignificant; // "shortest" mode not supported by snprintf + precision = std::numeric_limits<double>::max_digits10; // snprintf lacks "shortest" mode if (isZero(d)) { // Negative zero is expected as simple "0", not "-0". We cannot do d < 0, though. @@ -167,8 +167,8 @@ void qt_doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, cha switch (form) { case QLocaleData::DFDecimal: format[formatLength - 2] = 'f'; - // <anything> '.' <precision> '\0' - optimize for numbers smaller than 512k - extraChars = (d > (1 << 19) ? QLocaleData::DoubleMaxDigitsBeforeDecimal : 6) + 2; + // <anything> '.' <precision> '\0' + extraChars = wholePartSpace(d) + 2; break; case QLocaleData::DFExponent: format[formatLength - 2] = 'e'; @@ -581,9 +581,10 @@ QString qdtoa(qreal d, int *decpt, int *sign) int length = 0; // Some versions of libdouble-conversion like an extra digit, probably for '\0' - char result[QLocaleData::DoubleMaxSignificant + 1]; - qt_doubleToAscii(d, QLocaleData::DFSignificantDigits, QLocale::FloatingPointShortest, result, - QLocaleData::DoubleMaxSignificant + 1, nonNullSign, length, nonNullDecpt); + constexpr int digits = std::numeric_limits<double>::max_digits10 + 1; + char result[digits]; + qt_doubleToAscii(d, QLocaleData::DFSignificantDigits, QLocale::FloatingPointShortest, + result, digits, nonNullSign, length, nonNullDecpt); if (sign) *sign = nonNullSign ? 1 : 0; |