10#include <private/qtools_p.h>
11#include <private/qnumeric_p.h>
26#if defined(Q_OS_LINUX) && !defined(__UCLIBC__)
32# define LLONG_MAX Q_INT64_C(0x7fffffffffffffff
)
35# define LLONG_MIN (-LLONG_MAX - Q_INT64_C(1
))
38# define ULLONG_MAX Q_UINT64_C(0xffffffffffffffff
)
43using namespace QtMiscUtils;
48 char *buf, qsizetype bufSize,
49 bool &sign,
int &length,
int &decpt)
74 }
else if (qt_is_nan(d)) {
89#if !defined(QT_NO_DOUBLECONVERSION) && !defined(QT_BOOTSTRAPPED)
94 double_conversion::DoubleToStringConverter::DtoaMode mode;
95 if (precision == QLocale::FloatingPointShortest) {
96 mode = double_conversion::DoubleToStringConverter::SHORTEST;
98 mode = double_conversion::DoubleToStringConverter::PRECISION;
100 mode = double_conversion::DoubleToStringConverter::FIXED;
105 const auto boundedBufferSize =
static_cast<
int>((std::min)(bufSize, qsizetype(INT_MAX)));
106 double_conversion::DoubleToStringConverter::DoubleToAscii(d, mode, precision, buf,
108 &sign, &length, &decpt);
117 else if (precision == QLocale::FloatingPointShortest)
118 precision = std::numeric_limits<
double>::max_digits10;
134 const int formatLength = 7;
135 char format[formatLength];
136 format[formatLength - 1] =
'\0';
139 format[2] =
char((precision / 100) % 10) +
'0';
140 format[3] =
char((precision / 10) % 10) +
'0';
141 format[4] =
char(precision % 10) +
'0';
144 case QLocaleData::DFDecimal:
145 format[formatLength - 2] =
'f';
147 extraChars = wholePartSpace(d) + 2;
149 case QLocaleData::DFExponent:
150 format[formatLength - 2] =
'e';
154 case QLocaleData::DFSignificantDigits:
155 format[formatLength - 2] =
'g';
165 QVarLengthArray<
char> target(precision + extraChars);
167 length = qDoubleSnprintf(target.data(), target.size(), QT_CLOCALE, format, d);
168 int firstSignificant = 0;
169 int decptInTarget = length;
173 while (firstSignificant < length) {
174 if (target[firstSignificant] ==
'.')
175 decptInTarget = firstSignificant;
176 else if (target[firstSignificant] !=
'0')
182 if (decptInTarget == length)
183 decptInTarget = std::find(target.data() + firstSignificant, target.data() + length,
'.') -
187 if (form != QLocaleData::DFDecimal) {
189 eSign = std::find(target.data() + firstSignificant, target.data() + length,
'e') -
192 if (eSign < length) {
199 auto r = qstrntoll(target.data() + eSign + 1, length - eSign - 1, 10);
200 decpt = r.result + 1;
202 Q_ASSERT(r.used + eSign + 1 <= length);
208 decpt = decptInTarget - firstSignificant;
213 decpt = decptInTarget - firstSignificant;
217 if (decptInTarget > firstSignificant) {
219 int lengthBeforeDecpt = decptInTarget - firstSignificant;
220 memcpy(buf, target.data() + firstSignificant, qMin(lengthBeforeDecpt, bufSize));
221 if (eSign > decptInTarget && lengthBeforeDecpt < bufSize) {
223 memcpy(buf + lengthBeforeDecpt, target.data() + decptInTarget + 1,
224 qMin(eSign - decptInTarget - 1, bufSize - lengthBeforeDecpt));
227 length = qMin(eSign - firstSignificant - 1, bufSize);
230 length = qMin(eSign - firstSignificant, bufSize);
233 if (eSign > firstSignificant) {
236 memcpy(buf, target.data() + firstSignificant, qMin(eSign - firstSignificant, bufSize));
242 length = qMin(eSign - firstSignificant, bufSize);
251 while (length > 1 && buf[length - 1] ==
'0')
264 if (
char c = *num; numLen >= 3
265 && (c ==
'-' || c ==
'+' || c ==
'I' || c ==
'i' || c ==
'N' || c ==
'n')) {
266 bool negative = (c ==
'-');
267 bool hasSign = negative || (c ==
'+');
275 auto lowered = [](
char c) {
283 if (numLen != offset + 3)
287 char c2 = lowered(num[offset + 1]);
288 char c3 = lowered(num[offset + 2]);
289 if (c ==
'i' && c2 ==
'n' && c3 ==
'f')
290 return { negative ? -qt_inf() : qt_inf(), offset + 3 };
291 else if (c ==
'n' && c2 ==
'a' && c3 ==
'n' && !hasSign)
292 return { qt_qnan(), 3 };
299#if !defined(QT_NO_DOUBLECONVERSION) && !defined(QT_BOOTSTRAPPED)
300 int conv_flags = double_conversion::StringToDoubleConverter::NO_FLAGS;
302 conv_flags = double_conversion::StringToDoubleConverter::ALLOW_TRAILING_JUNK;
304 conv_flags = double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES
305 | double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES;
307 double_conversion::StringToDoubleConverter conv(conv_flags, 0.0, qt_qnan(),
nullptr,
nullptr);
308 if (
int(numLen) != numLen) {
312 d = conv.StringToDouble(num,
int(numLen), &processed);
315 if (!qt_is_finite(d)) {
321 return { d, -processed };
326 constexpr auto maxDigitsForULongLong = 1 + std::numeric_limits<
unsigned long long>::digits10;
328 char fmt[1 + maxDigitsForULongLong + 4 + 1];
329 std::snprintf(fmt,
sizeof fmt,
"%s%llu%s",
330 "%",
static_cast<
unsigned long long>(numLen),
"lf%n");
332 if (qDoubleSscanf(num, QT_CLOCALE, fmt, &d, &processed) < 1)
335 if ((strayCharMode == TrailingJunkProhibited && processed != numLen) || qt_is_nan(d)) {
340 if (!qt_is_finite(d)) {
344 for (
int i = 0; i < processed; ++i) {
346 if ((c <
'0' || c >
'9') && c !=
'.' && c !=
'-' && c !=
'+' && c !=
'e' && c !=
'E') {
351 return { d, -processed };
360 for (
int i = 0; i < processed; ++i) {
361 if (num[i] >=
'1' && num[i] <=
'9') {
363 return {d, -processed};
364 }
else if (num[i] ==
'e' || num[i] ==
'E') {
369 return { d, processed };
373static auto scanPrefix(
const char *p,
const char *stop,
int base)
380 if (p < stop && isAsciiDigit(*p)) {
382 const char *x_or_b = p + 1;
403 }
else if (base == 0) {
415 if (d -
'0' < qMin(base, 10))
419 return d >=
'a' && d <
'a' + base - 10;
426 const char *p = begin, *
const stop = begin + size;
427 while (p < stop && ascii_isspace(*p))
429 unsigned long long result = 0;
430 if (p >= stop || *p ==
'-')
432 const auto prefix =
scanPrefix(*p ==
'+' ? p + 1 : p
, stop
, base
);
433 if (!prefix.base || prefix.next >= stop)
436 const auto res = std::from_chars(prefix.next, stop, result, prefix.base);
437 if (res.ec !=
std::errc{})
439 return { result, res.ptr == prefix.next ? 0 : res.ptr - begin };
444 const char *p = begin, *
const stop = begin + size;
445 while (p < stop && ascii_isspace(*p))
450 const bool negate = p < stop && *p ==
'-';
451 if (negate || (p < stop && *p ==
'+'))
457 if (!prefix.base || prefix.next >= stop || !isDigitForBase(*prefix.next, prefix.base))
460 long long result = 0;
461 auto res = std::from_chars(prefix.next, stop, result, prefix.base);
462 if (negate && res.ec ==
std::errc::result_out_of_range) {
464 unsigned long long check = 0;
465 res = std::from_chars(prefix.next, stop, check, prefix.base);
466 if (res.ec ==
std::errc{} && check +
std::numeric_limits<
long long>::min() == 0)
467 return {
std::numeric_limits<
long long>::min(), res.ptr - begin };
470 if (res.ec !=
std::errc{})
472 return { negate ? -result : result, res.ptr - begin };
475template <
typename Char>
480#define BIG_BASE_LOOP(b)
482 const int r = number % b;
483 *--p = Char((r < 10
? '0' : 'a' - 10
) + r);
486#ifndef __OPTIMIZE_SIZE__
487# define SMALL_BASE_LOOP(b)
489 *--p = Char('0' + number % b);
497#undef SMALL_BASE_LOOP
508 return QStringLiteral(
"0");
511 const unsigned maxlen = 65;
512 static_assert(CHAR_BIT *
sizeof(number) + 1 <= maxlen);
513 char16_t buff[maxlen];
514 char16_t *
const end = buff + maxlen, *p = end;
516 qulltoString_helper<
char16_t>(number, base, p);
520 return QString(
reinterpret_cast<QChar *>(p), end - p);
523QString
qulltoa(qulonglong number,
int base,
const QStringView zero)
527 const unsigned maxlen = 128;
528 static_assert(CHAR_BIT *
sizeof(number) <= maxlen);
529 char16_t buff[maxlen];
530 char16_t *
const end = buff + maxlen, *p = end;
532 if (base != 10 || zero == u"0") {
533 qulltoString_helper<
char16_t>(number, base, p);
534 }
else if (zero.size() && !zero.at(0).isSurrogate()) {
535 const char16_t zeroUcs2 = zero.at(0).unicode();
536 while (number != 0) {
537 *(--p) = unicodeForDigit(number % base, zeroUcs2);
541 }
else if (zero.size() == 2 && zero.at(0).isHighSurrogate()) {
542 const char32_t zeroUcs4 = QChar::surrogateToUcs4(zero.at(0), zero.at(1));
543 while (number != 0) {
544 const char32_t digit = unicodeForDigit(number % base, zeroUcs4);
546 *(--p) = QChar::lowSurrogate(digit);
547 *(--p) = QChar::highSurrogate(digit);
552 Q_UNREACHABLE_RETURN(QString());
555 return QString(
reinterpret_cast<QChar *>(p), end - p);
560#if defined(QT_CHECK_RANGE)
561 if (base < 2 || base > 36) {
562 qWarning(
"QByteArray::setNum: Invalid base %d", base);
566 qulltoString_helper(n, base, p);
571
572
573
574
575
576
577double qstrntod(
const char *s00, qsizetype len,
const char **se,
bool *ok)
579 auto r = qt_asciiToDouble(s00, len, TrailingJunkAllowed);
581 *se = s00 + (r.used < 0 ? -r.used : r.used);
587QString
qdtoa(qreal d,
int *decpt,
int *sign)
589 bool nonNullSign =
false;
590 int nonNullDecpt = 0;
594 constexpr qsizetype digits = std::numeric_limits<
double>::max_digits10 + 1;
596 qt_doubleToAscii(d, QLocaleData::DFSignificantDigits, QLocale::FloatingPointShortest,
597 result, digits, nonNullSign, length, nonNullDecpt);
600 *sign = nonNullSign ? 1 : 0;
602 *decpt = nonNullDecpt;
604 return QLatin1StringView(result, length);
610 if (precision == QLocale::FloatingPointShortest) {
617 if (length <= decpt && length > 1)
619 else if (length == 1 && decpt <= 0)
628 useDecimal = 1 - decpt <= bias;
629 else if (decpt <= length)
632 useDecimal = decpt <= length + bias;
635 Q_ASSERT(precision >= 0);
636 useDecimal = decpt > -4 && decpt <= (precision ? precision : 1);
643 Q_ASSERT(number >= 0);
644 if (Q_LIKELY(number < 1000))
645 return number < 10 ? 1 : number < 100 ? 2 : 3;
647 for (number /= 1000; number; number /= 10)
658 if (precision != QLocale::FloatingPointShortest && precision < 0)
661 using D =
std::numeric_limits<
double>;
663 constexpr int MaxDigits = 1 + qMax(D::max_exponent10, D::digits10 - D::min_exponent10);
667 if (precision == QLocale::FloatingPointShortest)
668 bufSize += D::max_digits10;
670 bufSize += wholePartSpace(qAbs(d)) + precision;
672 bufSize += qMax(2, precision) + 1;
676 QVarLengthArray<
char, MaxDigits> buffer(bufSize);
677 bool negative =
false;
680 qt_doubleToAscii(d, form, precision, buffer.data(), buffer.size(), negative, length, decpt);
681 QLatin1StringView view(buffer.data(), length);
683 qsizetype total = (negative ? 1 : 0) + length;
684 if (qt_is_finite(d)) {
686 form = resolveFormat(precision, decpt, view.size());
694 if (
int extraPrecision = precision - (length - 1); extraPrecision > 0 && !succinct)
695 total += extraPrecision;
700 else if (decpt < length)
703 total += decpt - length;
705 if (precision > 0 && !succinct) {
708 total +=
std::max(0, precision - length + decpt);
710 total += 1 + precision;
718 constexpr bool IsQString =
std::is_same_v<T, QString>;
719 using Char = std::conditional_t<IsQString,
char16_t,
char>;
722 result.reserve(total);
724 if (negative && !qIsNull(d))
725 result.append(Char(
'-'));
726 if (!qt_is_finite(d)) {
729 result =
std::move(result).toUpper();
733 result.append(view.first(1));
734 view = view.sliced(1);
735 if (!view.isEmpty() || (!succinct && precision > 0)) {
736 result.append(Char(
'.'));
738 if (qsizetype pad = precision - view.size(); !succinct && pad > 0) {
739 for (
int i = 0; i < pad; ++i)
740 result.append(Char(
'0'));
743 int exponent = decpt - 1;
744 result.append(Char(uppercase ?
'E' :
'e'));
745 result.append(Char(exponent < 0 ?
'-' :
'+'));
746 exponent =
std::abs(exponent);
747 Q_ASSERT(exponent <= D::max_exponent10 + D::max_digits10);
748 int exponentDigits =
digits(exponent
);
750 if (exponentDigits == 1)
751 result.append(Char(
'0'));
752 result.resize(result.size() + exponentDigits);
753 auto location =
reinterpret_cast<Char *>(result.end());
754 qulltoString_helper<Char>(exponent, 10, location);
759 if constexpr (IsQString)
760 result.append(u"0.0");
762 result.append(
"0.0");
764 result.append(Char(
'0'));
767 auto numDecimals = result.size() - 2 - (negative ? 1 : 0);
768 for (qsizetype i = numDecimals; i < precision; ++i)
769 result.append(Char(
'0'));
772 if (decpt > view.size()) {
774 const int sign = negative ? 1 : 0;
775 while (result.size() - sign < decpt)
776 result.append(Char(
'0'));
779 result.append(view.first(decpt));
780 view = view.sliced(decpt);
782 result.append(Char(
'0'));
784 if (!view.isEmpty() || (!succinct && view.size() < precision)) {
785 result.append(Char(
'.'));
788 for (qsizetype i = view.size(); i < precision; ++i)
789 result.append(Char(
'0'));
799 Q_ASSERT(total >= result.size());
805 return dtoString<QString>(d, form, precision, uppercase);
810 return dtoString<QByteArray>(d, form, precision, uppercase);
813#if defined(QT_SUPPORTS_INT128) || defined(QT_USE_MSVC_INT128)
814static inline quint64 toUInt64(qinternaluint128 v)
816#if defined(QT_USE_MSVC_INT128)
817 return quint64(v._Word[0]);
823QString quint128toBasicLatin(qinternaluint128 number,
int base)
828 static constexpr auto dividers = []()
constexpr {
829 std::array<quint64, 35> bases {};
830 for (
int base = 2; base <= 36; ++base) {
838 static constexpr auto digitCounts = []()
constexpr {
839 std::array<quint8, 35> digits{};
840 for (
int base = 2; base <= 36; ++base) {
843 for (i = 0; v * base > v; ++i)
845 digits[base - 2] = i;
852 constexpr unsigned flags = QLocaleData::NoFlags;
853 const QLocaleData *dd = QLocaleData::c();
856 constexpr int Width = -1;
857 if (base == 2 || base == 4 || base == 16) {
859 result = dd->unsLongLongToString(quint64(number), 64, base, Width, flags);
860 result.prepend(dd->unsLongLongToString(quint64(number >> 64), -1, base, Width, flags));
862 int digitCount = digitCounts[base - 2];
863 quint64 divider = dividers[base - 2];
864 quint64 lower = toUInt64(number % divider);
867 result.prepend(dd->unsLongLongToString(lower, digitCount, base, Width, flags));
868 lower = toUInt64(number % divider);
871 result.prepend(dd->unsLongLongToString(lower, -1, base, Width, flags));
876QString qint128toBasicLatin(qinternalint128 number,
int base)
878 const bool negative = number < 0;
881 QString result = quint128toBasicLatin(qinternaluint128(number), base);
883 result.prepend(u'-');
#define QT_CLOCALE_HOLDER