diff options
Diffstat (limited to 'src/corelib/text/qlocale_tools.cpp')
-rw-r--r-- | src/corelib/text/qlocale_tools.cpp | 211 |
1 files changed, 96 insertions, 115 deletions
diff --git a/src/corelib/text/qlocale_tools.cpp b/src/corelib/text/qlocale_tools.cpp index 6964550b2d..b6639bcb71 100644 --- a/src/corelib/text/qlocale_tools.cpp +++ b/src/corelib/text/qlocale_tools.cpp @@ -7,6 +7,7 @@ #include "qlocale_p.h" #include "qstring.h" +#include <private/qtools_p.h> #include <private/qnumeric_p.h> #include <ctype.h> @@ -37,9 +38,12 @@ QT_BEGIN_NAMESPACE +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) { @@ -93,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 @@ -185,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 = qstrntoll(target.data() + eSign + 1, length - 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 @@ -242,48 +250,50 @@ void qt_doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, cha --length; } -double qt_asciiToDouble(const char *num, qsizetype numLen, bool &ok, int &processed, - StrayCharacterMode strayCharMode) +QSimpleParsedNumber<double> qt_asciiToDouble(const char *num, qsizetype numLen, + StrayCharacterMode strayCharMode) { - auto string_equals = [](const char *needle, const char *haystack, qsizetype haystackLen) { - qsizetype needleLen = strlen(needle); - return needleLen == haystackLen && memcmp(needle, haystack, haystackLen) == 0; - }; - - if (numLen <= 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 (string_equals("nan", num, numLen)) { - processed = 3; - return qt_qnan(); - } else if (string_equals("+nan", num, numLen) || string_equals("-nan", num, numLen)) { - 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 (string_equals("+inf", num, numLen)) { - processed = 4; - return qt_inf(); - } else if (string_equals("inf", num, numLen)) { - processed = 3; - return qt_inf(); - } else if (string_equals("-inf", num, numLen)) { - 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) { @@ -295,22 +305,18 @@ double qt_asciiToDouble(const char *num, qsizetype numLen, bool &ok, int &proces double_conversion::StringToDoubleConverter conv(conv_flags, 0.0, qt_qnan(), nullptr, nullptr); if (int(numLen) != numLen) { // a number over 2 GB in length is silly, just assume it isn't valid - ok = false; - processed = 0; - return 0.0; + return {}; } else { - d = conv.StringToDouble(num, numLen, &processed); + 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 @@ -318,32 +324,28 @@ double qt_asciiToDouble(const char *num, qsizetype numLen, bool &ok, int &proces 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]; - sprintf(fmt, "%s%llu%s", "%", static_cast<unsigned long long>(numLen), "lf%n"); + 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) @@ -355,14 +357,13 @@ double qt_asciiToDouble(const char *num, qsizetype numLen, bool &ok, int &proces 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 */ @@ -373,7 +374,7 @@ static auto scanPrefix(const char *p, const char *stop, int base) const char *next; int base; }; - if (p < stop && *p >= '0' && *p <= '9') { + if (p < stop && isAsciiDigit(*p)) { if (*p == '0') { const char *x_or_b = p + 1; if (x_or_b < stop) { @@ -417,36 +418,25 @@ static bool isDigitForBase(char d, int base) return false; } -unsigned long long -qstrntoull(const char *begin, qsizetype size, const char **endptr, int base, bool *ok) +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 == '-') { - *ok = false; - if (endptr) - *endptr = begin; - return result; - } + if (p >= stop || *p == '-') + return { }; const auto prefix = scanPrefix(*p == '+' ? p + 1 : p, stop, base); - if (!prefix.base || prefix.next >= stop) { - if (endptr) - *endptr = begin; - *ok = false; - return 0; - } + if (!prefix.base || prefix.next >= stop) + return { }; const auto res = std::from_chars(prefix.next, stop, result, prefix.base); - *ok = res.ec == std::errc{}; - if (endptr) - *endptr = res.ptr == prefix.next ? begin : res.ptr; - return result; + if (res.ec != std::errc{}) + return { }; + return { result, res.ptr == prefix.next ? 0 : res.ptr - begin }; } -long long -qstrntoll(const char *begin, qsizetype size, const char **endptr, int base, bool *ok) +QSimpleParsedNumber<qlonglong> qstrntoll(const char *begin, qsizetype size, int base) { const char *p = begin, *const stop = begin + size; while (p < stop && ascii_isspace(*p)) @@ -461,30 +451,22 @@ qstrntoll(const char *begin, qsizetype size, const char **endptr, int base, bool 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)) { - if (endptr) - *endptr = begin; - *ok = false; - return 0; - } + 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); - *ok = res.ec == std::errc{}; 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) { - *ok = true; - if (endptr) - *endptr = res.ptr; - return std::numeric_limits<long long>::min(); - } + if (res.ec == std::errc{} && check + std::numeric_limits<long long>::min() == 0) + return { std::numeric_limits<long long>::min(), res.ptr - begin }; + return { }; } - if (endptr) - *endptr = res.ptr == prefix.next ? begin : res.ptr; - return negate && *ok ? -result : result; + if (res.ec != std::errc{}) + return { }; + return { negate ? -result : result, res.ptr - begin }; } template <typename Char> @@ -564,8 +546,7 @@ QString qulltoa(qulonglong number, int base, const QStringView zero) number /= base; } } else { // zero should always be either a non-surrogate or a surrogate pair: - Q_UNREACHABLE(); - return QString(); + Q_UNREACHABLE_RETURN(QString()); } return QString(reinterpret_cast<QChar *>(p), end - p); @@ -574,18 +555,18 @@ QString qulltoa(qulonglong number, int base, const QStringView zero) /*! \internal - Converts the initial portion of the string pointed to by \a s00 to a double, using the 'C' locale. + 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) { - int processed = 0; - bool nonNullOk = false; - double d = qt_asciiToDouble(s00, len, nonNullOk, processed, TrailingJunkAllowed); + auto r = qt_asciiToDouble(s00, len, TrailingJunkAllowed); if (se) - *se = s00 + processed; + *se = s00 + (r.used < 0 ? -r.used : r.used); if (ok) - *ok = nonNullOk; - return d; + *ok = r.ok(); + return r.result; } QString qdtoa(qreal d, int *decpt, int *sign) @@ -595,7 +576,7 @@ QString qdtoa(qreal d, int *decpt, int *sign) int length = 0; // Some versions of libdouble-conversion like an extra digit, probably for '\0' - constexpr int digits = std::numeric_limits<double>::max_digits10 + 1; + 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); @@ -670,7 +651,7 @@ static T dtoString(double d, QLocaleData::DoubleForm form, int precision, bool u int bufSize = 1; if (precision == QLocale::FloatingPointShortest) bufSize += D::max_digits10; - else if (form == QLocaleData::DFDecimal && qIsFinite(d)) + 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" @@ -681,11 +662,11 @@ static T dtoString(double d, QLocaleData::DoubleForm form, int precision, bool u bool negative = false; int length = 0; int decpt = 0; - qt_doubleToAscii(d, form, precision, buffer.data(), buffer.length(), negative, length, decpt); + 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 (qIsFinite(d)) { + if (qt_is_finite(d)) { if (succinct) form = resolveFormat(precision, decpt, view.size()); @@ -727,7 +708,7 @@ static T dtoString(double d, QLocaleData::DoubleForm form, int precision, bool u if (negative && !isZero(d)) // We don't return "-0" result.append(Char('-')); - if (!qIsFinite(d)) { + if (!qt_is_finite(d)) { result.append(view); if (uppercase) result = std::move(result).toUpper(); @@ -748,7 +729,7 @@ static T dtoString(double d, QLocaleData::DoubleForm form, int precision, bool u result.append(Char(uppercase ? 'E' : 'e')); result.append(Char(exponent < 0 ? '-' : '+')); exponent = std::abs(exponent); - Q_ASSUME(exponent <= D::max_exponent10 + D::max_digits10); + 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) |