11#include <private/qtools_p.h>
12#include <private/qnumeric_p.h>
27#if defined(Q_OS_LINUX) && !defined(__UCLIBC__)
33# define LLONG_MAX Q_INT64_C(0x7fffffffffffffff
)
36# define LLONG_MIN (-LLONG_MAX - Q_INT64_C(1
))
39# define ULLONG_MAX Q_UINT64_C(0xffffffffffffffff
)
44using namespace QtMiscUtils;
49 char *buf, qsizetype bufSize,
50 bool &sign,
int &length,
int &decpt)
75 }
else if (qt_is_nan(d)) {
90#if !defined(QT_NO_DOUBLECONVERSION) && !defined(QT_BOOTSTRAPPED)
95 double_conversion::DoubleToStringConverter::DtoaMode mode;
96 if (precision == QLocale::FloatingPointShortest) {
97 mode = double_conversion::DoubleToStringConverter::SHORTEST;
99 mode = double_conversion::DoubleToStringConverter::PRECISION;
101 mode = double_conversion::DoubleToStringConverter::FIXED;
106 const auto boundedBufferSize =
static_cast<
int>((std::min)(bufSize, qsizetype(INT_MAX)));
107 double_conversion::DoubleToStringConverter::DoubleToAscii(d, mode, precision, buf,
109 &sign, &length, &decpt);
118 else if (precision == QLocale::FloatingPointShortest)
119 precision = std::numeric_limits<
double>::max_digits10;
135 const int formatLength = 7;
136 char format[formatLength];
137 format[formatLength - 1] =
'\0';
140 format[2] =
char((precision / 100) % 10) +
'0';
141 format[3] =
char((precision / 10) % 10) +
'0';
142 format[4] =
char(precision % 10) +
'0';
145 case QLocaleData::DFDecimal:
146 format[formatLength - 2] =
'f';
148 extraChars = wholePartSpace(d) + 2;
150 case QLocaleData::DFExponent:
151 format[formatLength - 2] =
'e';
155 case QLocaleData::DFSignificantDigits:
156 format[formatLength - 2] =
'g';
166 QVarLengthArray<
char> target(precision + extraChars);
168 length = qDoubleSnprintf(target.data(), target.size(), QT_CLOCALE, format, d);
169 int firstSignificant = 0;
170 int decptInTarget = length;
174 while (firstSignificant < length) {
175 if (target[firstSignificant] ==
'.')
176 decptInTarget = firstSignificant;
177 else if (target[firstSignificant] !=
'0')
183 if (decptInTarget == length)
184 decptInTarget = std::find(target.data() + firstSignificant, target.data() + length,
'.') -
188 if (form != QLocaleData::DFDecimal) {
190 eSign = std::find(target.data() + firstSignificant, target.data() + length,
'e') -
193 if (eSign < length) {
200 auto r = qstrntoll(target.data() + eSign + 1, length - eSign - 1, 10);
201 decpt = r.result + 1;
203 Q_ASSERT(r.used + eSign + 1 <= length);
209 decpt = decptInTarget - firstSignificant;
214 decpt = decptInTarget - firstSignificant;
218 if (decptInTarget > firstSignificant) {
220 int lengthBeforeDecpt = decptInTarget - firstSignificant;
221 memcpy(buf, target.data() + firstSignificant, qMin(lengthBeforeDecpt, bufSize));
222 if (eSign > decptInTarget && lengthBeforeDecpt < bufSize) {
224 memcpy(buf + lengthBeforeDecpt, target.data() + decptInTarget + 1,
225 qMin(eSign - decptInTarget - 1, bufSize - lengthBeforeDecpt));
228 length = qMin(eSign - firstSignificant - 1, bufSize);
231 length = qMin(eSign - firstSignificant, bufSize);
234 if (eSign > firstSignificant) {
237 memcpy(buf, target.data() + firstSignificant, qMin(eSign - firstSignificant, bufSize));
243 length = qMin(eSign - firstSignificant, bufSize);
252 while (length > 1 && buf[length - 1] ==
'0')
265 if (
char c = *num; numLen >= 3
266 && (c ==
'-' || c ==
'+' || c ==
'I' || c ==
'i' || c ==
'N' || c ==
'n')) {
267 bool negative = (c ==
'-');
268 bool hasSign = negative || (c ==
'+');
276 auto lowered = [](
char c) {
284 if (numLen != offset + 3)
288 char c2 = lowered(num[offset + 1]);
289 char c3 = lowered(num[offset + 2]);
290 if (c ==
'i' && c2 ==
'n' && c3 ==
'f')
291 return { negative ? -qt_inf() : qt_inf(), offset + 3 };
292 else if (c ==
'n' && c2 ==
'a' && c3 ==
'n' && !hasSign)
293 return { qt_qnan(), 3 };
300#if !defined(QT_NO_DOUBLECONVERSION) && !defined(QT_BOOTSTRAPPED)
301 int conv_flags = double_conversion::StringToDoubleConverter::NO_FLAGS;
303 conv_flags = double_conversion::StringToDoubleConverter::ALLOW_TRAILING_JUNK;
305 conv_flags = double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES
306 | double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES;
308 double_conversion::StringToDoubleConverter conv(conv_flags, 0.0, qt_qnan(),
nullptr,
nullptr);
309 if (
int(numLen) != numLen) {
313 d = conv.StringToDouble(num,
int(numLen), &processed);
316 if (!qt_is_finite(d)) {
322 return { d, -processed };
327 constexpr auto maxDigitsForULongLong = 1 + std::numeric_limits<
unsigned long long>::digits10;
329 char fmt[1 + maxDigitsForULongLong + 4 + 1];
330 std::snprintf(fmt,
sizeof fmt,
"%s%llu%s",
331 "%",
static_cast<
unsigned long long>(numLen),
"lf%n");
333 if (qDoubleSscanf(num, QT_CLOCALE, fmt, &d, &processed) < 1)
336 if ((strayCharMode == TrailingJunkProhibited && processed != numLen) || qt_is_nan(d)) {
341 if (!qt_is_finite(d)) {
345 for (
int i = 0; i < processed; ++i) {
347 if ((c <
'0' || c >
'9') && c !=
'.' && c !=
'-' && c !=
'+' && c !=
'e' && c !=
'E') {
352 return { d, -processed };
361 for (
int i = 0; i < processed; ++i) {
362 if (num[i] >=
'1' && num[i] <=
'9') {
364 return {d, -processed};
365 }
else if (num[i] ==
'e' || num[i] ==
'E') {
370 return { d, processed };
374static auto scanPrefix(
const char *p,
const char *stop,
int base)
381 if (p < stop && isAsciiDigit(*p)) {
383 const char *x_or_b = p + 1;
404 }
else if (base == 0) {
416 if (d -
'0' < qMin(base, 10))
420 return d >=
'a' && d <
'a' + base - 10;
427 const char *p = begin, *
const stop = begin + size;
428 while (p < stop && ascii_isspace(*p))
430 unsigned long long result = 0;
431 if (p >= stop || *p ==
'-')
433 const auto prefix =
scanPrefix(*p ==
'+' ? p + 1 : p
, stop
, base
);
434 if (!prefix.base || prefix.next >= stop)
437 const auto res =
std::from_chars(prefix.next, stop, result, prefix.base);
438 if (res.ec !=
std::errc{})
440 return { result, res.ptr == prefix.next ? 0 : res.ptr - begin };
445 const char *p = begin, *
const stop = begin + size;
446 while (p < stop && ascii_isspace(*p))
451 const bool negate = p < stop && *p ==
'-';
452 if (negate || (p < stop && *p ==
'+'))
458 if (!prefix.base || prefix.next >= stop || !isDigitForBase(*prefix.next, prefix.base))
461 long long result = 0;
462 auto res =
std::from_chars(prefix.next, stop, result, prefix.base);
463 if (negate && res.ec ==
std::errc::result_out_of_range) {
465 unsigned long long check = 0;
466 res =
std::from_chars(prefix.next, stop, check, prefix.base);
467 if (res.ec ==
std::errc{} && check +
std::numeric_limits<
long long>::min() == 0)
468 return {
std::numeric_limits<
long long>::min(), res.ptr - begin };
471 if (res.ec !=
std::errc{})
473 return { negate ? -result : result, res.ptr - begin };
476template <
typename Char>
481#define BIG_BASE_LOOP(b)
483 const int r = number % b;
484 *--p = Char((r < 10
? '0' : 'a' - 10
) + r);
487#ifndef __OPTIMIZE_SIZE__
488# define SMALL_BASE_LOOP(b)
490 *--p = Char('0' + number % b);
498#undef SMALL_BASE_LOOP
509 return QStringLiteral(
"0");
512 const unsigned maxlen = 65;
513 static_assert(CHAR_BIT *
sizeof(number) + 1 <= maxlen);
514 Q_DECL_UNINITIALIZED
char16_t buff[maxlen];
515 char16_t *
const end = buff + maxlen, *p = end;
517 qulltoString_helper<
char16_t>(number, base, p);
521 return QString(
reinterpret_cast<QChar *>(p), end - p);
524QString
qulltoa(qulonglong number,
int base,
const QStringView zero)
528 const unsigned maxlen = 128;
529 static_assert(CHAR_BIT *
sizeof(number) <= maxlen);
530 Q_DECL_UNINITIALIZED
char16_t buff[maxlen];
531 char16_t *
const end = buff + maxlen, *p = end;
533 if (base != 10 || zero == u"0") {
534 qulltoString_helper<
char16_t>(number, base, p);
535 }
else if (zero.size() && !zero.at(0).isSurrogate()) {
536 const char16_t zeroUcs2 = zero.at(0).unicode();
537 while (number != 0) {
538 *(--p) = unicodeForDigit(number % base, zeroUcs2);
542 }
else if (zero.size() == 2 && zero.at(0).isHighSurrogate()) {
543 const char32_t zeroUcs4 = QChar::surrogateToUcs4(zero.at(0), zero.at(1));
544 while (number != 0) {
545 const char32_t digit = unicodeForDigit(number % base, zeroUcs4);
547 *(--p) = QChar::lowSurrogate(digit);
548 *(--p) = QChar::highSurrogate(digit);
553 Q_UNREACHABLE_RETURN(QString());
556 return QString(
reinterpret_cast<QChar *>(p), end - p);
561#if defined(QT_CHECK_RANGE)
562 if (base < 2 || base > 36) {
563 qWarning(
"QByteArray::setNum: Invalid base %d", base);
567 qulltoString_helper(n, base, p);
572
573
574
575
576
577
578double qstrntod(
const char *s00, qsizetype len,
const char **se,
bool *ok)
580 auto r = qt_asciiToDouble(s00, len, TrailingJunkAllowed);
582 *se = s00 + (r.used < 0 ? -r.used : r.used);
588QString
qdtoa(qreal d,
int *decpt,
int *sign)
590 bool nonNullSign =
false;
591 int nonNullDecpt = 0;
595 constexpr qsizetype digits =
std::numeric_limits<
double>::max_digits10 + 1;
597 qt_doubleToAscii(d, QLocaleData::DFSignificantDigits, QLocale::FloatingPointShortest,
598 result, digits, nonNullSign, length, nonNullDecpt);
601 *sign = nonNullSign ? 1 : 0;
603 *decpt = nonNullDecpt;
605 return QLatin1StringView(result, length);
611 if (precision == QLocale::FloatingPointShortest) {
618 if (length <= decpt && length > 1)
620 else if (length == 1 && decpt <= 0)
629 useDecimal = 1 - decpt <= bias;
630 else if (decpt <= length)
633 useDecimal = decpt <= length + bias;
636 Q_ASSERT(precision >= 0);
637 useDecimal = decpt > -4 && decpt <= (precision ? precision : 1);
644 Q_ASSERT(number >= 0);
645 if (Q_LIKELY(number < 1000))
646 return number < 10 ? 1 : number < 100 ? 2 : 3;
648 for (number /= 1000; number; number /= 10)
659 if (precision != QLocale::FloatingPointShortest && precision < 0)
662 using D =
std::numeric_limits<
double>;
664 constexpr int MaxDigits = 1 + qMax(D::max_exponent10, D::digits10 - D::min_exponent10);
668 if (precision == QLocale::FloatingPointShortest)
669 bufSize += D::max_digits10;
671 bufSize += wholePartSpace(qAbs(d)) + precision;
673 bufSize += qMax(2, precision) + 1;
677 QVarLengthArray<
char, MaxDigits> buffer(bufSize);
678 bool negative =
false;
681 qt_doubleToAscii(d, form, precision, buffer.data(), buffer.size(), negative, length, decpt);
682 QLatin1StringView view(buffer.data(), length);
684 qsizetype total = (negative ? 1 : 0) + length;
685 if (qt_is_finite(d)) {
687 form = resolveFormat(precision, decpt, view.size());
695 if (
int extraPrecision = precision - (length - 1); extraPrecision > 0 && !succinct)
696 total += extraPrecision;
701 else if (decpt < length)
704 total += decpt - length;
706 if (precision > 0 && !succinct) {
709 total +=
std::max(0, precision - length + decpt);
711 total += 1 + precision;
719 constexpr bool IsQString =
std::is_same_v<T, QString>;
720 using Char = std::conditional_t<IsQString,
char16_t,
char>;
723 result.reserve(total);
725 if (negative && !qIsNull(d))
726 result.append(Char(
'-'));
727 if (!qt_is_finite(d)) {
730 result =
std::move(result).toUpper();
734 result.append(view.first(1));
735 view = view.sliced(1);
736 if (!view.isEmpty() || (!succinct && precision > 0)) {
737 result.append(Char(
'.'));
739 if (qsizetype pad = precision - view.size(); !succinct && pad > 0) {
740 for (
int i = 0; i < pad; ++i)
741 result.append(Char(
'0'));
744 int exponent = decpt - 1;
745 result.append(Char(uppercase ?
'E' :
'e'));
746 result.append(Char(exponent < 0 ?
'-' :
'+'));
747 exponent =
std::abs(exponent);
748 Q_ASSERT(exponent <= D::max_exponent10 + D::max_digits10);
749 int exponentDigits =
digits(exponent
);
751 if (exponentDigits == 1)
752 result.append(Char(
'0'));
753 result.resize(result.size() + exponentDigits);
754 auto location =
reinterpret_cast<Char *>(result.end());
755 qulltoString_helper<Char>(exponent, 10, location);
760 if constexpr (IsQString)
761 result.append(u"0.0");
763 result.append(
"0.0");
765 result.append(Char(
'0'));
768 auto numDecimals = result.size() - 2 - (negative ? 1 : 0);
769 for (qsizetype i = numDecimals; i < precision; ++i)
770 result.append(Char(
'0'));
773 if (decpt > view.size()) {
775 const int sign = negative ? 1 : 0;
776 while (result.size() - sign < decpt)
777 result.append(Char(
'0'));
780 result.append(view.first(decpt));
781 view = view.sliced(decpt);
783 result.append(Char(
'0'));
785 if (!view.isEmpty() || (!succinct && view.size() < precision)) {
786 result.append(Char(
'.'));
789 for (qsizetype i = view.size(); i < precision; ++i)
790 result.append(Char(
'0'));
800 Q_ASSERT(total >= result.size());
806 return dtoString<QString>(d, form, precision, uppercase);
811 return dtoString<QByteArray>(d, form, precision, uppercase);
814#if defined(QT_SUPPORTS_INT128) || defined(QT_USE_MSVC_INT128)
815static inline quint64 toUInt64(qinternaluint128 v)
817#if defined(QT_USE_MSVC_INT128)
818 return quint64(v._Word[0]);
824QString quint128toBasicLatin(qinternaluint128 number,
int base)
829 static constexpr auto dividers = []()
constexpr {
830 std::array<quint64, 35> bases {};
831 for (
int base = 2; base <= 36; ++base) {
839 static constexpr auto digitCounts = []()
constexpr {
840 std::array<quint8, 35> digits{};
841 for (
int base = 2; base <= 36; ++base) {
844 for (i = 0; v * base > v; ++i)
846 digits[base - 2] = i;
853 constexpr unsigned flags = QLocaleData::NoFlags;
854 const QLocaleData *dd = QLocaleData::c();
857 constexpr int Width = -1;
858 if (base == 2 || base == 4 || base == 16) {
860 result = dd->unsLongLongToString(quint64(number), 64, base, Width, flags);
861 result.prepend(dd->unsLongLongToString(quint64(number >> 64), -1, base, Width, flags));
863 int digitCount = digitCounts[base - 2];
864 quint64 divider = dividers[base - 2];
865 quint64 lower = toUInt64(number % divider);
868 result.prepend(dd->unsLongLongToString(lower, digitCount, base, Width, flags));
869 lower = toUInt64(number % divider);
872 result.prepend(dd->unsLongLongToString(lower, -1, base, Width, flags));
877QString qint128toBasicLatin(qinternalint128 number,
int base)
879 const bool negative = number < 0;
882 QString result = quint128toBasicLatin(qinternaluint128(number), base);
884 result.prepend(u'-');
#define QT_CLOCALE_HOLDER