Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qlocale_tools.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:critical reason:data-parser
5
8#include "qlocale_p.h"
9#include "qstring.h"
10
11#include <private/qtools_p.h>
12#include <private/qnumeric_p.h>
13
14#include <cstdio>
15
16#include <ctype.h>
17#include <errno.h>
18#include <float.h>
19#include <limits.h>
20#include <math.h>
21#include <stdlib.h>
22#include <time.h>
23
24#include <limits>
25#include <charconv>
26
27#if defined(Q_OS_LINUX) && !defined(__UCLIBC__)
28# include <fenv.h>
29#endif
30
31// Sizes as defined by the ISO C99 standard - fallback
32#ifndef LLONG_MAX
33# define LLONG_MAX Q_INT64_C(0x7fffffffffffffff)
34#endif
35#ifndef LLONG_MIN
36# define LLONG_MIN (-LLONG_MAX - Q_INT64_C(1))
37#endif
38#ifndef ULLONG_MAX
39# define ULLONG_MAX Q_UINT64_C(0xffffffffffffffff)
40#endif
41
42QT_BEGIN_NAMESPACE
43
44using namespace QtMiscUtils;
45
47
48void qt_doubleToAscii(double d, QLocaleData::DoubleForm form, int precision,
49 char *buf, qsizetype bufSize,
50 bool &sign, int &length, int &decpt)
51{
52 if (bufSize == 0) {
53 decpt = 0;
54 sign = d < 0;
55 length = 0;
56 return;
57 }
58
59 // Detect special numbers (nan, +/-inf)
60 // We cannot use the high-level API of libdouble-conversion as we need to
61 // apply locale-specific formatting, such as decimal points, grouping
62 // separators, etc. Because of this, we have to check for infinity and NaN
63 // before calling DoubleToAscii.
64 if (qt_is_inf(d)) {
65 sign = d < 0;
66 if (bufSize >= 3) {
67 buf[0] = 'i';
68 buf[1] = 'n';
69 buf[2] = 'f';
70 length = 3;
71 } else {
72 length = 0;
73 }
74 return;
75 } else if (qt_is_nan(d)) {
76 if (bufSize >= 3) {
77 buf[0] = 'n';
78 buf[1] = 'a';
79 buf[2] = 'n';
80 length = 3;
81 } else {
82 length = 0;
83 }
84 return;
85 }
86
87 if (form == QLocaleData::DFSignificantDigits && precision == 0)
88 precision = 1; // 0 significant digits is silently converted to 1
89
90#if !defined(QT_NO_DOUBLECONVERSION) && !defined(QT_BOOTSTRAPPED)
91 // one digit before the decimal dot, counts as significant digit for DoubleToStringConverter
92 if (form == QLocaleData::DFExponent && precision >= 0)
93 ++precision;
94
95 double_conversion::DoubleToStringConverter::DtoaMode mode;
96 if (precision == QLocale::FloatingPointShortest) {
97 mode = double_conversion::DoubleToStringConverter::SHORTEST;
98 } else if (form == QLocaleData::DFSignificantDigits || form == QLocaleData::DFExponent) {
99 mode = double_conversion::DoubleToStringConverter::PRECISION;
100 } else {
101 mode = double_conversion::DoubleToStringConverter::FIXED;
102 }
103 // libDoubleConversion is limited to 32-bit lengths. It's ok to cap the buffer size,
104 // though, because the library will never write 2GiB of chars as output
105 // (the length out-parameter is just an int, too).
106 const auto boundedBufferSize = static_cast<int>((std::min)(bufSize, qsizetype(INT_MAX)));
107 double_conversion::DoubleToStringConverter::DoubleToAscii(d, mode, precision, buf,
108 boundedBufferSize,
109 &sign, &length, &decpt);
110#else // QT_NO_DOUBLECONVERSION || QT_BOOTSTRAPPED
111
112 // Cut the precision at 999, to fit it into the format string. We can't get more than 17
113 // significant digits, so anything after that is mostly noise. You do get closer to the "middle"
114 // of the range covered by the given double with more digits, so to a degree it does make sense
115 // to honor higher precisions. We define that at more than 999 digits that is not the case.
116 if (precision > 999)
117 precision = 999;
118 else if (precision == QLocale::FloatingPointShortest)
119 precision = std::numeric_limits<double>::max_digits10; // snprintf lacks "shortest" mode
120
121 if (qIsNull(d)) {
122 // Negative zero is expected as simple "0", not "-0". We cannot do d < 0, though.
123 sign = false;
124 buf[0] = '0';
125 length = 1;
126 decpt = 1;
127 return;
128 } else if (d < 0) {
129 sign = true;
130 d = -d;
131 } else {
132 sign = false;
133 }
134
135 const int formatLength = 7; // '%', '.', 3 digits precision, 'f', '\0'
136 char format[formatLength];
137 format[formatLength - 1] = '\0';
138 format[0] = '%';
139 format[1] = '.';
140 format[2] = char((precision / 100) % 10) + '0';
141 format[3] = char((precision / 10) % 10) + '0';
142 format[4] = char(precision % 10) + '0';
143 int extraChars;
144 switch (form) {
145 case QLocaleData::DFDecimal:
146 format[formatLength - 2] = 'f';
147 // <anything> '.' <precision> '\0'
148 extraChars = wholePartSpace(d) + 2;
149 break;
150 case QLocaleData::DFExponent:
151 format[formatLength - 2] = 'e';
152 // '.', 'e', '-', <exponent> '\0'
153 extraChars = 7;
154 break;
155 case QLocaleData::DFSignificantDigits:
156 format[formatLength - 2] = 'g';
157
158 // either the same as in the 'e' case, or '.' and '\0'
159 // precision covers part before '.'
160 extraChars = 7;
161 break;
162 default:
163 Q_UNREACHABLE();
164 }
165
166 QVarLengthArray<char> target(precision + extraChars);
167
168 length = qDoubleSnprintf(target.data(), target.size(), QT_CLOCALE, format, d);
169 int firstSignificant = 0;
170 int decptInTarget = length;
171
172 // Find the first significant digit (not 0), and note any '.' we encounter.
173 // There is no '-' at the front of target because we made sure d > 0 above.
174 while (firstSignificant < length) {
175 if (target[firstSignificant] == '.')
176 decptInTarget = firstSignificant;
177 else if (target[firstSignificant] != '0')
178 break;
179 ++firstSignificant;
180 }
181
182 // If no '.' found so far, search the rest of the target buffer for it.
183 if (decptInTarget == length)
184 decptInTarget = std::find(target.data() + firstSignificant, target.data() + length, '.') -
185 target.data();
186
187 int eSign = length;
188 if (form != QLocaleData::DFDecimal) {
189 // In 'e' or 'g' form, look for the 'e'.
190 eSign = std::find(target.data() + firstSignificant, target.data() + length, 'e') -
191 target.data();
192
193 if (eSign < length) {
194 // If 'e' is found, the final decimal point is determined by the number after 'e'.
195 // Mind that the final decimal point, decpt, is the offset of the decimal point from the
196 // start of the resulting string in buf. It may be negative or larger than bufSize, in
197 // which case the missing digits are zeroes. In the 'e' case decptInTarget is always 1,
198 // as variants of snprintf always generate numbers with one digit before the '.' then.
199 // This is why the final decimal point is offset by 1, relative to the number after 'e'.
200 auto r = qstrntoll(target.data() + eSign + 1, length - eSign - 1, 10);
201 decpt = r.result + 1;
202 Q_ASSERT(r.ok());
203 Q_ASSERT(r.used + eSign + 1 <= length);
204 } else {
205 // No 'e' found, so it's the 'f' form. Variants of snprintf generate numbers with
206 // potentially multiple digits before the '.', but without decimal exponent then. So we
207 // get the final decimal point from the position of the '.'. The '.' itself takes up one
208 // character. We adjust by 1 below if that gets in the way.
209 decpt = decptInTarget - firstSignificant;
210 }
211 } else {
212 // In 'f' form, there can not be an 'e', so it's enough to look for the '.'
213 // (and possibly adjust by 1 below)
214 decpt = decptInTarget - firstSignificant;
215 }
216
217 // Move the actual digits from the snprintf target to the actual buffer.
218 if (decptInTarget > firstSignificant) {
219 // First move the digits before the '.', if any
220 int lengthBeforeDecpt = decptInTarget - firstSignificant;
221 memcpy(buf, target.data() + firstSignificant, qMin(lengthBeforeDecpt, bufSize));
222 if (eSign > decptInTarget && lengthBeforeDecpt < bufSize) {
223 // Then move any remaining digits, until 'e'
224 memcpy(buf + lengthBeforeDecpt, target.data() + decptInTarget + 1,
225 qMin(eSign - decptInTarget - 1, bufSize - lengthBeforeDecpt));
226 // The final length of the output is the distance between the first significant digit
227 // and 'e' minus 1, for the '.', except if the buffer is smaller.
228 length = qMin(eSign - firstSignificant - 1, bufSize);
229 } else {
230 // 'e' was before the decpt or things didn't fit. Don't subtract the '.' from the length.
231 length = qMin(eSign - firstSignificant, bufSize);
232 }
233 } else {
234 if (eSign > firstSignificant) {
235 // If there are any significant digits at all, they are all after the '.' now.
236 // Just copy them straight away.
237 memcpy(buf, target.data() + firstSignificant, qMin(eSign - firstSignificant, bufSize));
238
239 // The decimal point was before the first significant digit, so we were one off above.
240 // Consider 0.1 - buf will be just '1', and decpt should be 0. But
241 // "decptInTarget - firstSignificant" will yield -1.
242 ++decpt;
243 length = qMin(eSign - firstSignificant, bufSize);
244 } else {
245 // No significant digits means the number is just 0.
246 buf[0] = '0';
247 length = 1;
248 decpt = 1;
249 }
250 }
251#endif // QT_NO_DOUBLECONVERSION || QT_BOOTSTRAPPED
252 while (length > 1 && buf[length - 1] == '0') // drop trailing zeroes
253 --length;
254}
255
256QSimpleParsedNumber<double> qt_asciiToDouble(const char *num, qsizetype numLen,
257 StrayCharacterMode strayCharMode)
258{
259 if (numLen <= 0)
260 return {};
261
262 // We have to catch NaN before because we need NaN as marker for "garbage" in the
263 // libdouble-conversion case and, in contrast to libdouble-conversion or sscanf, we don't allow
264 // "-nan" or "+nan"
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 == '+');
269 qptrdiff offset = 0;
270 if (hasSign) {
271 offset = 1;
272 c = num[offset];
273 }
274
275 if (c > '9') {
276 auto lowered = [](char c) {
277 // this will mangle non-letters, but none can become a letter
278 return c | 0x20;
279 };
280
281 // Found a non-digit, so this MUST be either "inf", "+inf", "-inf"
282 // or "nan". Anything else is an invalid parse and we don't need to
283 // feed it to the converter below.
284 if (numLen != offset + 3)
285 return {};
286
287 c = lowered(c);
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 };
294 return {};
295 }
296 }
297
298 double d = 0.0;
299 int processed;
300#if !defined(QT_NO_DOUBLECONVERSION) && !defined(QT_BOOTSTRAPPED)
301 int conv_flags = double_conversion::StringToDoubleConverter::NO_FLAGS;
302 if (strayCharMode == TrailingJunkAllowed) {
303 conv_flags = double_conversion::StringToDoubleConverter::ALLOW_TRAILING_JUNK;
304 } else if (strayCharMode == WhitespacesAllowed) {
305 conv_flags = double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES
306 | double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES;
307 }
308 double_conversion::StringToDoubleConverter conv(conv_flags, 0.0, qt_qnan(), nullptr, nullptr);
309 if (int(numLen) != numLen) {
310 // a number over 2 GB in length is silly, just assume it isn't valid
311 return {};
312 } else {
313 d = conv.StringToDouble(num, int(numLen), &processed);
314 }
315
316 if (!qt_is_finite(d)) {
317 if (qt_is_nan(d)) {
318 // Garbage found. We don't accept it and return 0.
319 return {};
320 } else {
321 // Overflow. That's not OK, but we still return infinity.
322 return { d, -processed };
323 }
324 }
325#else
326 // ::digits10 is 19, but ::max() is 18'446'744'073'709'551'615ULL - go, figure...
327 constexpr auto maxDigitsForULongLong = 1 + std::numeric_limits<unsigned long long>::digits10;
328 // need to ensure that we don't read more than numLen of input:
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");
332
333 if (qDoubleSscanf(num, QT_CLOCALE, fmt, &d, &processed) < 1)
334 processed = 0;
335
336 if ((strayCharMode == TrailingJunkProhibited && processed != numLen) || qt_is_nan(d)) {
337 // Implementation defined nan symbol or garbage found. We don't accept it.
338 return {};
339 }
340
341 if (!qt_is_finite(d)) {
342 // Overflow. Check for implementation-defined infinity symbols and reject them.
343 // We assume that any infinity symbol has to contain a character that cannot be part of a
344 // "normal" number (that is 0-9, ., -, +, e).
345 for (int i = 0; i < processed; ++i) {
346 char c = num[i];
347 if ((c < '0' || c > '9') && c != '.' && c != '-' && c != '+' && c != 'e' && c != 'E') {
348 // Garbage found
349 return {};
350 }
351 }
352 return { d, -processed };
353 }
354#endif // !defined(QT_NO_DOUBLECONVERSION) && !defined(QT_BOOTSTRAPPED)
355
356 // Otherwise we would have gotten NaN or sorted it out above.
357 Q_ASSERT(strayCharMode == TrailingJunkAllowed || processed == numLen);
358
359 // Check if underflow has occurred.
360 if (qIsNull(d)) {
361 for (int i = 0; i < processed; ++i) {
362 if (num[i] >= '1' && num[i] <= '9') {
363 // if a digit before any 'e' is not 0, then a non-zero number was intended.
364 return {d, -processed};
365 } else if (num[i] == 'e' || num[i] == 'E') {
366 break;
367 }
368 }
369 }
370 return { d, processed };
371}
372
373/* Detect base if 0 and, if base is hex or bin, skip over 0x/0b prefixes */
374static auto scanPrefix(const char *p, const char *stop, int base)
375{
376 struct R
377 {
378 const char *next;
379 int base;
380 };
381 if (p < stop && isAsciiDigit(*p)) {
382 if (*p == '0') {
383 const char *x_or_b = p + 1;
384 if (x_or_b < stop) {
385 switch (*x_or_b) {
386 case 'b':
387 case 'B':
388 if (base == 0)
389 base = 2;
390 if (base == 2)
391 p += 2;
392 return R{p, base};
393 case 'x':
394 case 'X':
395 if (base == 0)
396 base = 16;
397 if (base == 16)
398 p += 2;
399 return R{p, base};
400 }
401 }
402 if (base == 0)
403 base = 8;
404 } else if (base == 0) {
405 base = 10;
406 }
407 Q_ASSERT(base);
408 }
409 return R{p, base};
410}
411
412static bool isDigitForBase(char d, int base)
413{
414 if (d < '0')
415 return false;
416 if (d - '0' < qMin(base, 10))
417 return true;
418 if (base > 10) {
419 d |= 0x20; // tolower
420 return d >= 'a' && d < 'a' + base - 10;
421 }
422 return false;
423}
424
425QSimpleParsedNumber<qulonglong> qstrntoull(const char *begin, qsizetype size, int base)
426{
427 const char *p = begin, *const stop = begin + size;
428 while (p < stop && ascii_isspace(*p))
429 ++p;
430 unsigned long long result = 0;
431 if (p >= stop || *p == '-')
432 return { };
433 const auto prefix = scanPrefix(*p == '+' ? p + 1 : p, stop, base);
434 if (!prefix.base || prefix.next >= stop)
435 return { };
436
437 const auto res = std::from_chars(prefix.next, stop, result, prefix.base);
438 if (res.ec != std::errc{})
439 return { };
440 return { result, res.ptr == prefix.next ? 0 : res.ptr - begin };
441}
442
443QSimpleParsedNumber<qlonglong> qstrntoll(const char *begin, qsizetype size, int base)
444{
445 const char *p = begin, *const stop = begin + size;
446 while (p < stop && ascii_isspace(*p))
447 ++p;
448 // Frustratingly, std::from_chars() doesn't cope with a 0x prefix that might
449 // be between the sign and digits, so we have to handle that for it, which
450 // means we can't use its ability to read LLONG_MIN directly; see below.
451 const bool negate = p < stop && *p == '-';
452 if (negate || (p < stop && *p == '+'))
453 ++p;
454
455 const auto prefix = scanPrefix(p, stop, base);
456 // Must check for digit, as from_chars() will accept a sign, which would be
457 // a second sign, that we should reject.
458 if (!prefix.base || prefix.next >= stop || !isDigitForBase(*prefix.next, prefix.base))
459 return { };
460
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) {
464 // Maybe LLONG_MIN:
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 };
469 return { };
470 }
471 if (res.ec != std::errc{})
472 return { };
473 return { negate ? -result : result, res.ptr - begin };
474}
475
476template <typename Char>
477static Q_ALWAYS_INLINE void qulltoString_helper(qulonglong number, int base, Char *&p)
478{
479 // Performance-optimized code. Compiler can generate faster code when base is known.
480 switch (base) {
481#define BIG_BASE_LOOP(b)
482 do {
483 const int r = number % b;
484 *--p = Char((r < 10 ? '0' : 'a' - 10) + r);
485 number /= b;
486 } while (number)
487#ifndef __OPTIMIZE_SIZE__
488# define SMALL_BASE_LOOP(b)
489 do {
490 *--p = Char('0' + number % b);
491 number /= b;
492 } while (number)
493
494 case 2: SMALL_BASE_LOOP(2); break;
495 case 8: SMALL_BASE_LOOP(8); break;
496 case 10: SMALL_BASE_LOOP(10); break;
497 case 16: BIG_BASE_LOOP(16); break;
498#undef SMALL_BASE_LOOP
499#endif
500 default: BIG_BASE_LOOP(base); break;
501#undef BIG_BASE_LOOP
502 }
503}
504
505// This is technically "qulonglong to ascii", but that name's taken
506QString qulltoBasicLatin(qulonglong number, int base, bool negative)
507{
508 if (number == 0)
509 return QStringLiteral("0");
510 // Length of MIN_LLONG with the sign in front is 65; we never need surrogate pairs.
511 // We do not need a terminator.
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;
516
517 qulltoString_helper<char16_t>(number, base, p);
518 if (negative)
519 *--p = u'-';
520
521 return QString(reinterpret_cast<QChar *>(p), end - p);
522}
523
524QString qulltoa(qulonglong number, int base, const QStringView zero)
525{
526 // Length of MAX_ULLONG in base 2 is 64; and we may need a surrogate pair
527 // per digit. We do not need a terminator.
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;
532
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);
539
540 number /= base;
541 }
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);
546
547 *(--p) = QChar::lowSurrogate(digit);
548 *(--p) = QChar::highSurrogate(digit);
549
550 number /= base;
551 }
552 } else { // zero should always be either a non-surrogate or a surrogate pair:
553 Q_UNREACHABLE_RETURN(QString());
554 }
555
556 return QString(reinterpret_cast<QChar *>(p), end - p);
557}
558
559char *qulltoa2(char *p, qulonglong n, int base)
560{
561#if defined(QT_CHECK_RANGE)
562 if (base < 2 || base > 36) {
563 qWarning("QByteArray::setNum: Invalid base %d", base);
564 base = 10;
565 }
566#endif
567 qulltoString_helper(n, base, p);
568 return p;
569}
570
571/*!
572 \internal
573
574 Converts the initial portion of the string pointed to by \a s00 to a double,
575 using the 'C' locale. The function sets the pointer pointed to by \a se to
576 point to the character past the last character converted.
577 */
578double qstrntod(const char *s00, qsizetype len, const char **se, bool *ok)
579{
580 auto r = qt_asciiToDouble(s00, len, TrailingJunkAllowed);
581 if (se)
582 *se = s00 + (r.used < 0 ? -r.used : r.used);
583 if (ok)
584 *ok = r.ok();
585 return r.result;
586}
587
588QString qdtoa(qreal d, int *decpt, int *sign)
589{
590 bool nonNullSign = false;
591 int nonNullDecpt = 0;
592 int length = 0;
593
594 // Some versions of libdouble-conversion like an extra digit, probably for '\0'
595 constexpr qsizetype digits = std::numeric_limits<double>::max_digits10 + 1;
596 char result[digits];
597 qt_doubleToAscii(d, QLocaleData::DFSignificantDigits, QLocale::FloatingPointShortest,
598 result, digits, nonNullSign, length, nonNullDecpt);
599
600 if (sign)
601 *sign = nonNullSign ? 1 : 0;
602 if (decpt)
603 *decpt = nonNullDecpt;
604
605 return QLatin1StringView(result, length);
606}
607
608static QLocaleData::DoubleForm resolveFormat(int precision, int decpt, qsizetype length)
609{
610 bool useDecimal;
611 if (precision == QLocale::FloatingPointShortest) {
612 // Find out which representation is shorter.
613 // Set bias to everything added to exponent form but not
614 // decimal, minus the converse.
615
616 // Exponent adds separator, sign and two exponents:
617 int bias = 2 + 2;
618 if (length <= decpt && length > 1)
619 ++bias;
620 else if (length == 1 && decpt <= 0)
621 --bias;
622
623 // When 0 < decpt <= length, the forms have equal digit
624 // counts, plus things bias has taken into account;
625 // otherwise decimal form's digit count is right-padded with
626 // zeros to decpt, when decpt is positive, otherwise it's
627 // left-padded with 1 - decpt zeros.
628 if (decpt <= 0)
629 useDecimal = 1 - decpt <= bias;
630 else if (decpt <= length)
631 useDecimal = true;
632 else
633 useDecimal = decpt <= length + bias;
634 } else {
635 // X == decpt - 1, POSIX's P; -4 <= X < P iff -4 < decpt <= P
636 Q_ASSERT(precision >= 0);
637 useDecimal = decpt > -4 && decpt <= (precision ? precision : 1);
638 }
640}
641
642static constexpr int digits(int number)
643{
644 Q_ASSERT(number >= 0);
645 if (Q_LIKELY(number < 1000))
646 return number < 10 ? 1 : number < 100 ? 2 : 3;
647 int i = 3;
648 for (number /= 1000; number; number /= 10)
649 ++i;
650 return i;
651}
652
653// Used generically for both QString and QByteArray
654template <typename T>
655static T dtoString(double d, QLocaleData::DoubleForm form, int precision, bool uppercase)
656{
657 // Undocumented: aside from F.P.Shortest, precision < 0 is treated as
658 // default, 6 - same as printf().
659 if (precision != QLocale::FloatingPointShortest && precision < 0)
660 precision = 6;
661
662 using D = std::numeric_limits<double>;
663 // 1 is for the null-terminator
664 constexpr int MaxDigits = 1 + qMax(D::max_exponent10, D::digits10 - D::min_exponent10);
665
666 // "maxDigits" above is a reasonable estimate, though we may need more due to extra precision
667 int bufSize = 1;
668 if (precision == QLocale::FloatingPointShortest)
669 bufSize += D::max_digits10;
670 else if (form == QLocaleData::DFDecimal && qt_is_finite(d))
671 bufSize += wholePartSpace(qAbs(d)) + precision;
672 else // Add extra digit due to different interpretations of precision.
673 bufSize += qMax(2, precision) + 1; // Must also be big enough for "nan" or "inf"
674
675 // Reserve `MaxDigits` on the stack, which is a reasonable estimate;
676 // but we may need more due to extra precision, which we cannot know at compile-time.
677 QVarLengthArray<char, MaxDigits> buffer(bufSize);
678 bool negative = false;
679 int length = 0;
680 int decpt = 0;
681 qt_doubleToAscii(d, form, precision, buffer.data(), buffer.size(), negative, length, decpt);
682 QLatin1StringView view(buffer.data(), length);
683 const bool succinct = form == QLocaleData::DFSignificantDigits;
684 qsizetype total = (negative ? 1 : 0) + length;
685 if (qt_is_finite(d)) {
686 if (succinct)
687 form = resolveFormat(precision, decpt, view.size());
688
689 switch (form) {
691 total += 3; // (.e+) The '.' may not be needed, but we would only overestimate by 1 char
692 // Exponents: we guarantee at least 2
693 total += std::max(2, digits(std::abs(decpt - 1)));
694 // "length - 1" because one of the digits will always be before the decimal point
695 if (int extraPrecision = precision - (length - 1); extraPrecision > 0 && !succinct)
696 total += extraPrecision; // some requested zero-padding
697 break;
699 if (decpt <= 0) // leading "0." and zeros
700 total += 2 - decpt;
701 else if (decpt < length) // just the dot
702 total += 1;
703 else // trailing zeros (and no dot, unless we require extra precision):
704 total += decpt - length;
705
706 if (precision > 0 && !succinct) {
707 // May need trailing zeros to satisfy precision:
708 if (decpt < length)
709 total += std::max(0, precision - length + decpt);
710 else // and a dot to separate them:
711 total += 1 + precision;
712 }
713 break;
715 Q_UNREACHABLE(); // Handled earlier
716 }
717 }
718
719 constexpr bool IsQString = std::is_same_v<T, QString>;
720 using Char = std::conditional_t<IsQString, char16_t, char>;
721
722 T result;
723 result.reserve(total);
724
725 if (negative && !qIsNull(d)) // We don't return "-0"
726 result.append(Char('-'));
727 if (!qt_is_finite(d)) {
728 result.append(view);
729 if (uppercase)
730 result = std::move(result).toUpper();
731 } else {
732 switch (form) {
734 result.append(view.first(1));
735 view = view.sliced(1);
736 if (!view.isEmpty() || (!succinct && precision > 0)) {
737 result.append(Char('.'));
738 result.append(view);
739 if (qsizetype pad = precision - view.size(); !succinct && pad > 0) {
740 for (int i = 0; i < pad; ++i)
741 result.append(Char('0'));
742 }
743 }
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);
750 // C's printf guarantees a two-digit exponent, and so do we:
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);
756 break;
757 }
759 if (decpt < 0) {
760 if constexpr (IsQString)
761 result.append(u"0.0");
762 else
763 result.append("0.0");
764 while (++decpt < 0)
765 result.append(Char('0'));
766 result.append(view);
767 if (!succinct) {
768 auto numDecimals = result.size() - 2 - (negative ? 1 : 0);
769 for (qsizetype i = numDecimals; i < precision; ++i)
770 result.append(Char('0'));
771 }
772 } else {
773 if (decpt > view.size()) {
774 result.append(view);
775 const int sign = negative ? 1 : 0;
776 while (result.size() - sign < decpt)
777 result.append(Char('0'));
778 view = {};
779 } else if (decpt) {
780 result.append(view.first(decpt));
781 view = view.sliced(decpt);
782 } else {
783 result.append(Char('0'));
784 }
785 if (!view.isEmpty() || (!succinct && view.size() < precision)) {
786 result.append(Char('.'));
787 result.append(view);
788 if (!succinct) {
789 for (qsizetype i = view.size(); i < precision; ++i)
790 result.append(Char('0'));
791 }
792 }
793 }
794 break;
796 Q_UNREACHABLE(); // taken care of earlier
797 break;
798 }
799 }
800 Q_ASSERT(total >= result.size()); // No reallocations are needed
801 return result;
802}
803
804QString qdtoBasicLatin(double d, QLocaleData::DoubleForm form, int precision, bool uppercase)
805{
806 return dtoString<QString>(d, form, precision, uppercase);
807}
808
809QByteArray qdtoAscii(double d, QLocaleData::DoubleForm form, int precision, bool uppercase)
810{
811 return dtoString<QByteArray>(d, form, precision, uppercase);
812}
813
814#if defined(QT_SUPPORTS_INT128) || defined(QT_USE_MSVC_INT128)
815static inline quint64 toUInt64(qinternaluint128 v)
816{
817#if defined(QT_USE_MSVC_INT128)
818 return quint64(v._Word[0]);
819#else
820 return quint64(v);
821#endif
822}
823
824QString quint128toBasicLatin(qinternaluint128 number, int base)
825{
826 // We divide our 128-bit number into parts that we can do text
827 // concatenation with. This list is the maximum power of the
828 // base that is less than 2^64.
829 static constexpr auto dividers = []() constexpr {
830 std::array<quint64, 35> bases {};
831 for (int base = 2; base <= 36; ++base) {
832 quint64 v = base;
833 while (v * base > v)
834 v *= base;
835 bases[base - 2] = v;
836 }
837 return bases;
838 }();
839 static constexpr auto digitCounts = []() constexpr {
840 std::array<quint8, 35> digits{};
841 for (int base = 2; base <= 36; ++base) {
842 quint64 v = base;
843 int i = 0;
844 for (i = 0; v * base > v; ++i)
845 v *= base;
846 digits[base - 2] = i;
847 }
848 return digits;
849 }();
850
851 QString result;
852
853 constexpr unsigned flags = QLocaleData::NoFlags;
854 const QLocaleData *dd = QLocaleData::c();
855
856 // special base cases:
857 constexpr int Width = -1;
858 if (base == 2 || base == 4 || base == 16) {
859 // 2^64 is a power of 2, 4 and 16
860 result = dd->unsLongLongToString(quint64(number), 64, base, Width, flags);
861 result.prepend(dd->unsLongLongToString(quint64(number >> 64), -1, base, Width, flags));
862 } else {
863 int digitCount = digitCounts[base - 2];
864 quint64 divider = dividers[base - 2];
865 quint64 lower = toUInt64(number % divider);
866 number /= divider;
867 while (number) {
868 result.prepend(dd->unsLongLongToString(lower, digitCount, base, Width, flags));
869 lower = toUInt64(number % divider);
870 number /= divider;
871 }
872 result.prepend(dd->unsLongLongToString(lower, -1, base, Width, flags));
873 }
874 return result;
875}
876
877QString qint128toBasicLatin(qinternalint128 number, int base)
878{
879 const bool negative = number < 0;
880 if (negative)
881 number *= -1;
882 QString result = quint128toBasicLatin(qinternaluint128(number), base);
883 if (negative)
884 result.prepend(u'-');
885 return result;
886}
887#endif // defined(QT_SUPPORTS_INT128) || defined(QT_USE_MSVC_INT128)
888
889QT_END_NAMESPACE
#define QT_CLOCALE_HOLDER
static T dtoString(double d, QLocaleData::DoubleForm form, int precision, bool uppercase)
#define LLONG_MAX
static Q_ALWAYS_INLINE void qulltoString_helper(qulonglong number, int base, Char *&p)
static auto scanPrefix(const char *p, const char *stop, int base)
static constexpr int digits(int number)
#define SMALL_BASE_LOOP(b)
#define ULLONG_MAX
static bool isDigitForBase(char d, int base)
QSimpleParsedNumber< qlonglong > qstrntoll(const char *begin, qsizetype size, int base)
#define BIG_BASE_LOOP(b)
static QLocaleData::DoubleForm resolveFormat(int precision, int decpt, qsizetype length)
#define LLONG_MIN
QString qdtoa(qreal d, int *decpt, int *sign)
StrayCharacterMode
@ TrailingJunkAllowed
@ WhitespacesAllowed
Q_CORE_EXPORT double qstrntod(const char *s00, qsizetype len, char const **se, bool *ok)
QSimpleParsedNumber< double > qt_asciiToDouble(const char *num, qsizetype numLen, StrayCharacterMode strayCharMode=TrailingJunkProhibited)
QByteArray qdtoAscii(double d, QLocaleData::DoubleForm form, int precision, bool uppercase)
char * qulltoa2(char *p, qulonglong n, int base)
QString qdtoBasicLatin(double d, QLocaleData::DoubleForm form, int precision, bool uppercase)
QString qulltoa(qulonglong l, int base, const QStringView zero)
QSimpleParsedNumber< qulonglong > qstrntoull(const char *nptr, qsizetype size, int base)
void qt_doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, char *buf, qsizetype bufSize, bool &sign, int &length, int &decpt)
QString qulltoBasicLatin(qulonglong l, int base, bool negative)
@ DFSignificantDigits
Definition qlocale_p.h:255