Port qt_doubleToAscii to qsizetype

Allows central handling of large buffers.

Adjust some callers.

Pick-to: 6.4 6.3
Task-number: QTBUG-103531
Change-Id: Ib55974c3de250883cd0f6d11a7eee051c7fd11bc
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2022-07-20 12:27:16 +02:00
parent ec35efd7f2
commit bd43c245e8
3 changed files with 12 additions and 5 deletions

View File

@ -3541,7 +3541,7 @@ QString QLocaleData::doubleToString(double d, int precision, DoubleForm form,
width = 0; width = 0;
int decpt; int decpt;
int bufSize = 1; qsizetype bufSize = 1;
if (precision == QLocale::FloatingPointShortest) if (precision == QLocale::FloatingPointShortest)
bufSize += std::numeric_limits<double>::max_digits10; bufSize += std::numeric_limits<double>::max_digits10;
else if (form == DFDecimal && qIsFinite(d)) else if (form == DFDecimal && qIsFinite(d))

View File

@ -39,7 +39,8 @@ QT_BEGIN_NAMESPACE
QT_CLOCALE_HOLDER 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) bool &sign, int &length, int &decpt)
{ {
if (bufSize == 0) { if (bufSize == 0) {
@ -93,7 +94,12 @@ void qt_doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, cha
} else { } else {
mode = double_conversion::DoubleToStringConverter::FIXED; 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); &sign, &length, &decpt);
#else // QT_NO_DOUBLECONVERSION || QT_BOOTSTRAPPED #else // QT_NO_DOUBLECONVERSION || QT_BOOTSTRAPPED
@ -595,7 +601,7 @@ QString qdtoa(qreal d, int *decpt, int *sign)
int length = 0; int length = 0;
// Some versions of libdouble-conversion like an extra digit, probably for '\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]; char result[digits];
qt_doubleToAscii(d, QLocaleData::DFSignificantDigits, QLocale::FloatingPointShortest, qt_doubleToAscii(d, QLocaleData::DFSignificantDigits, QLocale::FloatingPointShortest,
result, digits, nonNullSign, length, nonNullDecpt); result, digits, nonNullSign, length, nonNullDecpt);

View File

@ -29,7 +29,8 @@ enum StrayCharacterMode {
// API note: this function can't process a number with more than 2.1 billion digits // API note: this function can't process a number with more than 2.1 billion digits
[[nodiscard]] double qt_asciiToDouble(const char *num, qsizetype numLen, bool &ok, int &processed, [[nodiscard]] double qt_asciiToDouble(const char *num, qsizetype numLen, bool &ok, int &processed,
StrayCharacterMode strayCharMode = TrailingJunkProhibited); StrayCharacterMode strayCharMode = TrailingJunkProhibited);
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); bool &sign, int &length, int &decpt);
[[nodiscard]] QString qulltoBasicLatin(qulonglong l, int base, bool negative); [[nodiscard]] QString qulltoBasicLatin(qulonglong l, int base, bool negative);