diff options
Diffstat (limited to 'src/corelib/text/qlocale_tools.cpp')
-rw-r--r-- | src/corelib/text/qlocale_tools.cpp | 787 |
1 files changed, 538 insertions, 249 deletions
diff --git a/src/corelib/text/qlocale_tools.cpp b/src/corelib/text/qlocale_tools.cpp index 9adb6e4a23..6b04bc92cd 100644 --- a/src/corelib/text/qlocale_tools.cpp +++ b/src/corelib/text/qlocale_tools.cpp @@ -1,48 +1,13 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2016 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qlocale_tools_p.h" #include "qdoublescanprint_p.h" #include "qlocale_p.h" #include "qstring.h" +#include <private/qtools_p.h> #include <private/qnumeric_p.h> #include <ctype.h> @@ -53,6 +18,9 @@ #include <stdlib.h> #include <time.h> +#include <limits> +#include <charconv> + #if defined(Q_OS_LINUX) && !defined(__UCLIBC__) # include <fenv.h> #endif @@ -70,12 +38,12 @@ QT_BEGIN_NAMESPACE -#include "../../3rdparty/freebsd/strtoull.c" -#include "../../3rdparty/freebsd/strtoll.c" +using namespace QtMiscUtils; QT_CLOCALE_HOLDER -void qt_doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, char *buf, int bufSize, +void qt_doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, + char *buf, qsizetype bufSize, bool &sign, int &length, int &decpt) { if (bufSize == 0) { @@ -86,9 +54,10 @@ void qt_doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, cha } // Detect special numbers (nan, +/-inf) - // We cannot use the high-level API of libdouble-conversion as we need to apply locale-specific - // formatting, such as decimal points, thousands-separators, etc. Because of this, we have to - // check for infinity and NaN before calling DoubleToAscii. + // We cannot use the high-level API of libdouble-conversion as we need to + // apply locale-specific formatting, such as decimal points, grouping + // separators, etc. Because of this, we have to check for infinity and NaN + // before calling DoubleToAscii. if (qt_is_inf(d)) { sign = d < 0; if (bufSize >= 3) { @@ -128,7 +97,12 @@ void qt_doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, cha } else { mode = double_conversion::DoubleToStringConverter::FIXED; } - double_conversion::DoubleToStringConverter::DoubleToAscii(d, mode, precision, buf, bufSize, + // libDoubleConversion is limited to 32-bit lengths. It's ok to cap the buffer size, + // though, because the library will never write 2GiB of chars as output + // (the length out-parameter is just an int, too). + const auto boundedBufferSize = static_cast<int>((std::min)(bufSize, qsizetype(INT_MAX))); + double_conversion::DoubleToStringConverter::DoubleToAscii(d, mode, precision, buf, + boundedBufferSize, &sign, &length, &decpt); #else // QT_NO_DOUBLECONVERSION || QT_BOOTSTRAPPED @@ -139,7 +113,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 +141,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'; @@ -220,11 +194,10 @@ void qt_doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, cha // which case the missing digits are zeroes. In the 'e' case decptInTarget is always 1, // as variants of snprintf always generate numbers with one digit before the '.' then. // This is why the final decimal point is offset by 1, relative to the number after 'e'. - bool ok; - const char *endptr; - decpt = qstrtoll(target.data() + eSign + 1, &endptr, 10, &ok) + 1; - Q_ASSERT(ok); - Q_ASSERT(endptr - target.data() <= length); + auto r = qstrntoll(target.data() + eSign + 1, length - eSign - 1, 10); + decpt = r.result + 1; + Q_ASSERT(r.ok()); + Q_ASSERT(r.used + eSign + 1 <= length); } else { // No 'e' found, so it's the 'f' form. Variants of snprintf generate numbers with // potentially multiple digits before the '.', but without decimal exponent then. So we @@ -277,43 +250,50 @@ void qt_doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, cha --length; } -double qt_asciiToDouble(const char *num, int numLen, bool &ok, int &processed, - StrayCharacterMode strayCharMode) +QSimpleParsedNumber<double> qt_asciiToDouble(const char *num, qsizetype numLen, + StrayCharacterMode strayCharMode) { - if (*num == '\0') { - ok = false; - processed = 0; - return 0.0; - } - - ok = true; + if (numLen <= 0) + return {}; // We have to catch NaN before because we need NaN as marker for "garbage" in the // libdouble-conversion case and, in contrast to libdouble-conversion or sscanf, we don't allow // "-nan" or "+nan" - if (qstrcmp(num, "nan") == 0) { - processed = 3; - return qt_qnan(); - } else if ((num[0] == '-' || num[0] == '+') && qstrcmp(num + 1, "nan") == 0) { - processed = 0; - ok = false; - return 0.0; - } + if (char c = *num; numLen >= 3 + && (c == '-' || c == '+' || c == 'I' || c == 'i' || c == 'N' || c == 'n')) { + bool negative = (c == '-'); + bool hasSign = negative || (c == '+'); + qptrdiff offset = 0; + if (hasSign) { + offset = 1; + c = num[offset]; + } - // Infinity values are implementation defined in the sscanf case. In the libdouble-conversion - // case we need infinity as overflow marker. - if (qstrcmp(num, "+inf") == 0) { - processed = 4; - return qt_inf(); - } else if (qstrcmp(num, "inf") == 0) { - processed = 3; - return qt_inf(); - } else if (qstrcmp(num, "-inf") == 0) { - processed = 4; - return -qt_inf(); + if (c > '9') { + auto lowered = [](char c) { + // this will mangle non-letters, but none can become a letter + return c | 0x20; + }; + + // Found a non-digit, so this MUST be either "inf", "+inf", "-inf" + // or "nan". Anything else is an invalid parse and we don't need to + // feed it to the converter below. + if (numLen != offset + 3) + return {}; + + c = lowered(c); + char c2 = lowered(num[offset + 1]); + char c3 = lowered(num[offset + 2]); + if (c == 'i' && c2 == 'n' && c3 == 'f') + return { negative ? -qt_inf() : qt_inf(), offset + 3 }; + else if (c == 'n' && c2 == 'a' && c3 == 'n' && !hasSign) + return { qt_qnan(), 3 }; + return {}; + } } double d = 0.0; + int processed; #if !defined(QT_NO_DOUBLECONVERSION) && !defined(QT_BOOTSTRAPPED) int conv_flags = double_conversion::StringToDoubleConverter::NO_FLAGS; if (strayCharMode == TrailingJunkAllowed) { @@ -323,44 +303,49 @@ double qt_asciiToDouble(const char *num, int numLen, bool &ok, int &processed, | double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES; } double_conversion::StringToDoubleConverter conv(conv_flags, 0.0, qt_qnan(), nullptr, nullptr); - d = conv.StringToDouble(num, numLen, &processed); + if (int(numLen) != numLen) { + // a number over 2 GB in length is silly, just assume it isn't valid + return {}; + } else { + d = conv.StringToDouble(num, int(numLen), &processed); + } - if (!qIsFinite(d)) { - ok = false; - if (qIsNaN(d)) { + if (!qt_is_finite(d)) { + if (qt_is_nan(d)) { // Garbage found. We don't accept it and return 0. - processed = 0; - return 0.0; + return {}; } else { // Overflow. That's not OK, but we still return infinity. - return d; + return { d, -processed }; } } #else - if (qDoubleSscanf(num, QT_CLOCALE, "%lf%n", &d, &processed) < 1) + // ::digits10 is 19, but ::max() is 18'446'744'073'709'551'615ULL - go, figure... + constexpr auto maxDigitsForULongLong = 1 + std::numeric_limits<unsigned long long>::digits10; + // need to ensure that we don't read more than numLen of input: + char fmt[1 + maxDigitsForULongLong + 4 + 1]; + qsnprintf(fmt, sizeof fmt, "%s%llu%s", "%", static_cast<unsigned long long>(numLen), "lf%n"); + + if (qDoubleSscanf(num, QT_CLOCALE, fmt, &d, &processed) < 1) processed = 0; - if ((strayCharMode == TrailingJunkProhibited && processed != numLen) || qIsNaN(d)) { + if ((strayCharMode == TrailingJunkProhibited && processed != numLen) || qt_is_nan(d)) { // Implementation defined nan symbol or garbage found. We don't accept it. - processed = 0; - ok = false; - return 0.0; + return {}; } - if (!qIsFinite(d)) { + if (!qt_is_finite(d)) { // Overflow. Check for implementation-defined infinity symbols and reject them. // We assume that any infinity symbol has to contain a character that cannot be part of a // "normal" number (that is 0-9, ., -, +, e). - ok = false; for (int i = 0; i < processed; ++i) { char c = num[i]; if ((c < '0' || c > '9') && c != '.' && c != '-' && c != '+' && c != 'e' && c != 'E') { // Garbage found - processed = 0; - return 0.0; + return {}; } } - return d; + return { d, -processed }; } #endif // !defined(QT_NO_DOUBLECONVERSION) && !defined(QT_BOOTSTRAPPED) @@ -372,60 +357,164 @@ double qt_asciiToDouble(const char *num, int numLen, bool &ok, int &processed, for (int i = 0; i < processed; ++i) { if (num[i] >= '1' && num[i] <= '9') { // if a digit before any 'e' is not 0, then a non-zero number was intended. - ok = false; - return 0.0; + return {d, -processed}; } else if (num[i] == 'e' || num[i] == 'E') { break; } } } - return d; + return { d, processed }; +} + +/* Detect base if 0 and, if base is hex or bin, skip over 0x/0b prefixes */ +static auto scanPrefix(const char *p, const char *stop, int base) +{ + struct R + { + const char *next; + int base; + }; + if (p < stop && isAsciiDigit(*p)) { + if (*p == '0') { + const char *x_or_b = p + 1; + if (x_or_b < stop) { + switch (*x_or_b) { + case 'b': + case 'B': + if (base == 0) + base = 2; + if (base == 2) + p += 2; + return R{p, base}; + case 'x': + case 'X': + if (base == 0) + base = 16; + if (base == 16) + p += 2; + return R{p, base}; + } + } + if (base == 0) + base = 8; + } else if (base == 0) { + base = 10; + } + Q_ASSERT(base); + } + return R{p, base}; } -unsigned long long -qstrtoull(const char * nptr, const char **endptr, int base, bool *ok) +static bool isDigitForBase(char d, int base) { - // strtoull accepts negative numbers. We don't. - // Use a different variable so we pass the original nptr to strtoul - // (we need that so endptr may be nptr in case of failure) - const char *begin = nptr; - while (ascii_isspace(*begin)) - ++begin; - if (*begin == '-') { - *ok = false; - return 0; + if (d < '0') + return false; + if (d - '0' < qMin(base, 10)) + return true; + if (base > 10) { + d |= 0x20; // tolower + return d >= 'a' && d < 'a' + base - 10; } + return false; +} - *ok = true; - errno = 0; - char *endptr2 = nullptr; - unsigned long long result = qt_strtoull(nptr, &endptr2, base); - if (endptr) - *endptr = endptr2; - if ((result == 0 || result == std::numeric_limits<unsigned long long>::max()) - && (errno || endptr2 == nptr)) { - *ok = false; - return 0; +QSimpleParsedNumber<qulonglong> qstrntoull(const char *begin, qsizetype size, int base) +{ + const char *p = begin, *const stop = begin + size; + while (p < stop && ascii_isspace(*p)) + ++p; + unsigned long long result = 0; + if (p >= stop || *p == '-') + return { }; + const auto prefix = scanPrefix(*p == '+' ? p + 1 : p, stop, base); + if (!prefix.base || prefix.next >= stop) + return { }; + + const auto res = std::from_chars(prefix.next, stop, result, prefix.base); + if (res.ec != std::errc{}) + return { }; + return { result, res.ptr == prefix.next ? 0 : res.ptr - begin }; +} + +QSimpleParsedNumber<qlonglong> qstrntoll(const char *begin, qsizetype size, int base) +{ + const char *p = begin, *const stop = begin + size; + while (p < stop && ascii_isspace(*p)) + ++p; + // Frustratingly, std::from_chars() doesn't cope with a 0x prefix that might + // be between the sign and digits, so we have to handle that for it, which + // means we can't use its ability to read LLONG_MIN directly; see below. + const bool negate = p < stop && *p == '-'; + if (negate || (p < stop && *p == '+')) + ++p; + + const auto prefix = scanPrefix(p, stop, base); + // Must check for digit, as from_chars() will accept a sign, which would be + // a second sign, that we should reject. + if (!prefix.base || prefix.next >= stop || !isDigitForBase(*prefix.next, prefix.base)) + return { }; + + long long result = 0; + auto res = std::from_chars(prefix.next, stop, result, prefix.base); + if (negate && res.ec == std::errc::result_out_of_range) { + // Maybe LLONG_MIN: + unsigned long long check = 0; + res = std::from_chars(prefix.next, stop, check, prefix.base); + if (res.ec == std::errc{} && check + std::numeric_limits<long long>::min() == 0) + return { std::numeric_limits<long long>::min(), res.ptr - begin }; + return { }; } - return result; + if (res.ec != std::errc{}) + return { }; + return { negate ? -result : result, res.ptr - begin }; } -long long -qstrtoll(const char * nptr, const char **endptr, int base, bool *ok) +template <typename Char> +static Q_ALWAYS_INLINE void qulltoString_helper(qulonglong number, int base, Char *&p) { - *ok = true; - errno = 0; - char *endptr2 = nullptr; - long long result = qt_strtoll(nptr, &endptr2, base); - if (endptr) - *endptr = endptr2; - if ((result == 0 || result == std::numeric_limits<long long>::min() - || result == std::numeric_limits<long long>::max()) - && (errno || nptr == endptr2)) { - *ok = false; - return 0; + // Performance-optimized code. Compiler can generate faster code when base is known. + switch (base) { +#define BIG_BASE_LOOP(b) \ + do { \ + const int r = number % b; \ + *--p = Char((r < 10 ? '0' : 'a' - 10) + r); \ + number /= b; \ + } while (number) +#ifndef __OPTIMIZE_SIZE__ +# define SMALL_BASE_LOOP(b) \ + do { \ + *--p = Char('0' + number % b); \ + number /= b; \ + } while (number) + + case 2: SMALL_BASE_LOOP(2); break; + case 8: SMALL_BASE_LOOP(8); break; + case 10: SMALL_BASE_LOOP(10); break; + case 16: BIG_BASE_LOOP(16); break; +#undef SMALL_BASE_LOOP +#endif + default: BIG_BASE_LOOP(base); break; +#undef BIG_BASE_LOOP } - return result; +} + +// This is technically "qulonglong to ascii", but that name's taken +QString qulltoBasicLatin(qulonglong number, int base, bool negative) +{ + if (number == 0) + return QStringLiteral("0"); + // Length of MIN_LLONG with the sign in front is 65; we never need surrogate pairs. + // We do not need a terminator. + const unsigned maxlen = 65; + static_assert(CHAR_BIT * sizeof(number) + 1 <= maxlen); + char16_t buff[maxlen]; + char16_t *const end = buff + maxlen, *p = end; + + qulltoString_helper<char16_t>(number, base, p); + if (negative) + *--p = u'-'; + + return QString(reinterpret_cast<QChar *>(p), end - p); } QString qulltoa(qulonglong number, int base, const QStringView zero) @@ -433,164 +522,364 @@ QString qulltoa(qulonglong number, int base, const QStringView zero) // Length of MAX_ULLONG in base 2 is 64; and we may need a surrogate pair // per digit. We do not need a terminator. const unsigned maxlen = 128; - Q_STATIC_ASSERT(CHAR_BIT * sizeof(number) <= maxlen); - ushort buff[maxlen]; - ushort *const end = buff + maxlen, *p = end; + static_assert(CHAR_BIT * sizeof(number) <= maxlen); + char16_t buff[maxlen]; + char16_t *const end = buff + maxlen, *p = end; if (base != 10 || zero == u"0") { - while (number != 0) { - int c = number % base; - *--p = c < 10 ? '0' + c : c - 10 + 'a'; - number /= base; - } + qulltoString_helper<char16_t>(number, base, p); } else if (zero.size() && !zero.at(0).isSurrogate()) { - const ushort zeroUcs4 = zero.at(0).unicode(); + const char16_t zeroUcs2 = zero.at(0).unicode(); while (number != 0) { - *(--p) = zeroUcs4 + number % base; + *(--p) = unicodeForDigit(number % base, zeroUcs2); number /= base; } } else if (zero.size() == 2 && zero.at(0).isHighSurrogate()) { - const uint zeroUcs4 = QChar::surrogateToUcs4(zero.at(0), zero.at(1)); + const char32_t zeroUcs4 = QChar::surrogateToUcs4(zero.at(0), zero.at(1)); while (number != 0) { - const uint digit = zeroUcs4 + number % base; + const char32_t digit = unicodeForDigit(number % base, zeroUcs4); *(--p) = QChar::lowSurrogate(digit); *(--p) = QChar::highSurrogate(digit); number /= base; } - } else { - return QString(); + } else { // zero should always be either a non-surrogate or a surrogate pair: + Q_UNREACHABLE_RETURN(QString()); } return QString(reinterpret_cast<QChar *>(p), end - p); } -QString &decimalForm(const QString &zero, const QString &decimal, const QString &group, - QString &digits, int decpt, int precision, - PrecisionMode pm, - bool always_show_decpt, - bool thousands_group) +char *qulltoa2(char *p, qulonglong n, int base) { - const auto digitWidth = zero.size(); - Q_ASSERT(digitWidth == 1 || digitWidth == 2); - Q_ASSERT(digits.size() % digitWidth == 0); - - if (decpt < 0) { - for (int i = 0; i < -decpt; ++i) - digits.prepend(zero); - decpt = 0; - } else { - for (int i = digits.length() / digitWidth; i < decpt; ++i) - digits.append(zero); +#if defined(QT_CHECK_RANGE) + if (base < 2 || base > 36) { + qWarning("QByteArray::setNum: Invalid base %d", base); + base = 10; } +#endif + qulltoString_helper(n, base, p); + return p; +} - switch (pm) { - case PMDecimalDigits: - for (int i = digits.length() / digitWidth - decpt; i < precision; ++i) - digits.append(zero); - break; - case PMSignificantDigits: - for (int i = digits.length() / digitWidth; i < precision; ++i) - digits.append(zero); - break; - case PMChopTrailingZeros: - break; - } +/*! + \internal - if (always_show_decpt || decpt < digits.length() / digitWidth) - digits.insert(decpt * digitWidth, decimal); + Converts the initial portion of the string pointed to by \a s00 to a double, + using the 'C' locale. The function sets the pointer pointed to by \a se to + point to the character past the last character converted. + */ +double qstrntod(const char *s00, qsizetype len, const char **se, bool *ok) +{ + auto r = qt_asciiToDouble(s00, len, TrailingJunkAllowed); + if (se) + *se = s00 + (r.used < 0 ? -r.used : r.used); + if (ok) + *ok = r.ok(); + return r.result; +} - // FIXME: they're not simply thousands separators ! - // Need to mirror IndianNumberGrouping code in QLocaleData::longLongToString() - if (thousands_group) { - for (int i = decpt - 3; i > 0; i -= 3) - digits.insert(i * digitWidth, group); - } +QString qdtoa(qreal d, int *decpt, int *sign) +{ + bool nonNullSign = false; + int nonNullDecpt = 0; + int length = 0; + + // Some versions of libdouble-conversion like an extra digit, probably for '\0' + constexpr qsizetype digits = std::numeric_limits<double>::max_digits10 + 1; + char result[digits]; + qt_doubleToAscii(d, QLocaleData::DFSignificantDigits, QLocale::FloatingPointShortest, + result, digits, nonNullSign, length, nonNullDecpt); - if (decpt == 0) - digits.prepend(zero); + if (sign) + *sign = nonNullSign ? 1 : 0; + if (decpt) + *decpt = nonNullDecpt; - return digits; + return QLatin1StringView(result, length); } -QString &exponentForm(const QString &zero, const QString &decimal, const QString &exponential, - const QString &group, const QString &plus, const QString &minus, - QString &digits, int decpt, int precision, - PrecisionMode pm, - bool always_show_decpt, - bool leading_zero_in_exponent) +static QLocaleData::DoubleForm resolveFormat(int precision, int decpt, qsizetype length) { - const auto digitWidth = zero.size(); - Q_ASSERT(digitWidth == 1 || digitWidth == 2); - Q_ASSERT(digits.size() % digitWidth == 0); - - switch (pm) { - case PMDecimalDigits: - for (int i = digits.length() / digitWidth; i < precision + 1; ++i) - digits.append(zero); - break; - case PMSignificantDigits: - for (int i = digits.length() / digitWidth; i < precision; ++i) - digits.append(zero); - break; - case PMChopTrailingZeros: - break; + bool useDecimal; + if (precision == QLocale::FloatingPointShortest) { + // Find out which representation is shorter. + // Set bias to everything added to exponent form but not + // decimal, minus the converse. + + // Exponent adds separator, sign and two exponents: + int bias = 2 + 2; + if (length <= decpt && length > 1) + ++bias; + else if (length == 1 && decpt <= 0) + --bias; + + // When 0 < decpt <= length, the forms have equal digit + // counts, plus things bias has taken into account; + // otherwise decimal form's digit count is right-padded with + // zeros to decpt, when decpt is positive, otherwise it's + // left-padded with 1 - decpt zeros. + if (decpt <= 0) + useDecimal = 1 - decpt <= bias; + else if (decpt <= length) + useDecimal = true; + else + useDecimal = decpt <= length + bias; + } else { + // X == decpt - 1, POSIX's P; -4 <= X < P iff -4 < decpt <= P + Q_ASSERT(precision >= 0); + useDecimal = decpt > -4 && decpt <= (precision ? precision : 1); + } + return useDecimal ? QLocaleData::DFDecimal : QLocaleData::DFExponent; +} + +static constexpr int digits(int number) +{ + Q_ASSERT(number >= 0); + if (Q_LIKELY(number < 1000)) + return number < 10 ? 1 : number < 100 ? 2 : 3; + int i = 3; + for (number /= 1000; number; number /= 10) + ++i; + return i; +} + +// Used generically for both QString and QByteArray +template <typename T> +static T dtoString(double d, QLocaleData::DoubleForm form, int precision, bool uppercase) +{ + // Undocumented: aside from F.P.Shortest, precision < 0 is treated as + // default, 6 - same as printf(). + if (precision != QLocale::FloatingPointShortest && precision < 0) + precision = 6; + + using D = std::numeric_limits<double>; + // 1 is for the null-terminator + constexpr int MaxDigits = 1 + qMax(D::max_exponent10, D::digits10 - D::min_exponent10); + + // "maxDigits" above is a reasonable estimate, though we may need more due to extra precision + int bufSize = 1; + if (precision == QLocale::FloatingPointShortest) + bufSize += D::max_digits10; + else if (form == QLocaleData::DFDecimal && qt_is_finite(d)) + bufSize += wholePartSpace(qAbs(d)) + precision; + else // Add extra digit due to different interpretations of precision. + bufSize += qMax(2, precision) + 1; // Must also be big enough for "nan" or "inf" + + // Reserve `MaxDigits` on the stack, which is a reasonable estimate; + // but we may need more due to extra precision, which we cannot know at compile-time. + QVarLengthArray<char, MaxDigits> buffer(bufSize); + bool negative = false; + int length = 0; + int decpt = 0; + qt_doubleToAscii(d, form, precision, buffer.data(), buffer.size(), negative, length, decpt); + QLatin1StringView view(buffer.data(), length); + const bool succinct = form == QLocaleData::DFSignificantDigits; + qsizetype total = (negative ? 1 : 0) + length; + if (qt_is_finite(d)) { + if (succinct) + form = resolveFormat(precision, decpt, view.size()); + + switch (form) { + case QLocaleData::DFExponent: + total += 3; // (.e+) The '.' may not be needed, but we would only overestimate by 1 char + // Exponents: we guarantee at least 2 + total += std::max(2, digits(std::abs(decpt - 1))); + // "length - 1" because one of the digits will always be before the decimal point + if (int extraPrecision = precision - (length - 1); extraPrecision > 0 && !succinct) + total += extraPrecision; // some requested zero-padding + break; + case QLocaleData::DFDecimal: + if (decpt <= 0) // leading "0." and zeros + total += 2 - decpt; + else if (decpt < length) // just the dot + total += 1; + else // trailing zeros (and no dot, unless we require extra precision): + total += decpt - length; + + if (precision > 0 && !succinct) { + // May need trailing zeros to satisfy precision: + if (decpt < length) + total += std::max(0, precision - length + decpt); + else // and a dot to separate them: + total += 1 + precision; + } + break; + case QLocaleData::DFSignificantDigits: + Q_UNREACHABLE(); // Handled earlier + } } - if (always_show_decpt || digits.length() > digitWidth) - digits.insert(digitWidth, decimal); + constexpr bool IsQString = std::is_same_v<T, QString>; + using Char = std::conditional_t<IsQString, char16_t, char>; - digits.append(exponential); - digits.append(QLocaleData::longLongToString(zero, group, plus, minus, decpt - 1, - leading_zero_in_exponent ? 2 : 1, - 10, -1, QLocaleData::AlwaysShowSign)); + T result; + result.reserve(total); - return digits; + if (negative && !isZero(d)) // We don't return "-0" + result.append(Char('-')); + if (!qt_is_finite(d)) { + result.append(view); + if (uppercase) + result = std::move(result).toUpper(); + } else { + switch (form) { + case QLocaleData::DFExponent: { + result.append(view.first(1)); + view = view.sliced(1); + if (!view.isEmpty() || (!succinct && precision > 0)) { + result.append(Char('.')); + result.append(view); + if (qsizetype pad = precision - view.size(); !succinct && pad > 0) { + for (int i = 0; i < pad; ++i) + result.append(Char('0')); + } + } + int exponent = decpt - 1; + result.append(Char(uppercase ? 'E' : 'e')); + result.append(Char(exponent < 0 ? '-' : '+')); + exponent = std::abs(exponent); + Q_ASSERT(exponent <= D::max_exponent10 + D::max_digits10); + int exponentDigits = digits(exponent); + // C's printf guarantees a two-digit exponent, and so do we: + if (exponentDigits == 1) + result.append(Char('0')); + result.resize(result.size() + exponentDigits); + auto location = reinterpret_cast<Char *>(result.end()); + qulltoString_helper<Char>(exponent, 10, location); + break; + } + case QLocaleData::DFDecimal: + if (decpt < 0) { + if constexpr (IsQString) + result.append(u"0.0"); + else + result.append("0.0"); + while (++decpt < 0) + result.append(Char('0')); + result.append(view); + if (!succinct) { + auto numDecimals = result.size() - 2 - (negative ? 1 : 0); + for (qsizetype i = numDecimals; i < precision; ++i) + result.append(Char('0')); + } + } else { + if (decpt > view.size()) { + result.append(view); + const int sign = negative ? 1 : 0; + while (result.size() - sign < decpt) + result.append(Char('0')); + view = {}; + } else if (decpt) { + result.append(view.first(decpt)); + view = view.sliced(decpt); + } else { + result.append(Char('0')); + } + if (!view.isEmpty() || (!succinct && view.size() < precision)) { + result.append(Char('.')); + result.append(view); + if (!succinct) { + for (qsizetype i = view.size(); i < precision; ++i) + result.append(Char('0')); + } + } + } + break; + case QLocaleData::DFSignificantDigits: + Q_UNREACHABLE(); // taken care of earlier + break; + } + } + Q_ASSERT(total >= result.size()); // No reallocations are needed + return result; } -double qstrtod(const char *s00, const char **se, bool *ok) +QString qdtoBasicLatin(double d, QLocaleData::DoubleForm form, int precision, bool uppercase) { - const int len = static_cast<int>(strlen(s00)); - Q_ASSERT(len >= 0); - return qstrntod(s00, len, se, ok); + return dtoString<QString>(d, form, precision, uppercase); } -/*! - \internal +QByteArray qdtoAscii(double d, QLocaleData::DoubleForm form, int precision, bool uppercase) +{ + return dtoString<QByteArray>(d, form, precision, uppercase); +} - Converts the initial portion of the string pointed to by \a s00 to a double, using the 'C' locale. - */ -double qstrntod(const char *s00, int len, const char **se, bool *ok) +#if defined(QT_SUPPORTS_INT128) || defined(QT_USE_MSVC_INT128) +static inline quint64 toUInt64(qinternaluint128 v) { - int processed = 0; - bool nonNullOk = false; - double d = qt_asciiToDouble(s00, len, nonNullOk, processed, TrailingJunkAllowed); - if (se) - *se = s00 + processed; - if (ok) - *ok = nonNullOk; - return d; +#if defined(QT_USE_MSVC_INT128) + return quint64(v._Word[0]); +#else + return quint64(v); +#endif } -QString qdtoa(qreal d, int *decpt, int *sign) +QString quint128toBasicLatin(qinternaluint128 number, int base) { - bool nonNullSign = false; - int nonNullDecpt = 0; - int length = 0; + // We divide our 128-bit number into parts that we can do text + // concatenation with. This list is the maximum power of the + // base that is less than 2^64. + static constexpr auto dividers = []() constexpr { + std::array<quint64, 35> bases {}; + for (int base = 2; base <= 36; ++base) { + quint64 v = base; + while (v * base > v) + v *= base; + bases[base - 2] = v; + } + return bases; + }(); + static constexpr auto digitCounts = []() constexpr { + std::array<quint8, 35> digits{}; + for (int base = 2; base <= 36; ++base) { + quint64 v = base; + int i = 0; + for (i = 0; v * base > v; ++i) + v *= base; + digits[base - 2] = i; + } + return digits; + }(); - // 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); + QString result; - if (sign) - *sign = nonNullSign ? 1 : 0; - if (decpt) - *decpt = nonNullDecpt; + constexpr unsigned flags = QLocaleData::NoFlags; + const QLocaleData *dd = QLocaleData::c(); + + // special base cases: + constexpr int Width = -1; + if (base == 2 || base == 4 || base == 16) { + // 2^64 is a power of 2, 4 and 16 + result = dd->unsLongLongToString(quint64(number), 64, base, Width, flags); + result.prepend(dd->unsLongLongToString(quint64(number >> 64), -1, base, Width, flags)); + } else { + int digitCount = digitCounts[base - 2]; + quint64 divider = dividers[base - 2]; + quint64 lower = toUInt64(number % divider); + number /= divider; + while (number) { + result.prepend(dd->unsLongLongToString(lower, digitCount, base, Width, flags)); + lower = toUInt64(number % divider); + number /= divider; + } + result.prepend(dd->unsLongLongToString(lower, -1, base, Width, flags)); + } + return result; +} - return QLatin1String(result, length); +QString qint128toBasicLatin(qinternalint128 number, int base) +{ + const bool negative = number < 0; + if (negative) + number *= -1; + QString result = quint128toBasicLatin(qinternaluint128(number), base); + if (negative) + result.prepend(u'-'); + return result; } +#endif // defined(QT_SUPPORTS_INT128) || defined(QT_USE_MSVC_INT128) QT_END_NAMESPACE |