1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QLOCALE_TOOLS_P_H
5#define QLOCALE_TOOLS_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of internal files. This header file may change from version to version
13// without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qlocale_p.h"
19#include "qstring.h"
20
21QT_BEGIN_NAMESPACE
22
23enum StrayCharacterMode {
24 TrailingJunkProhibited,
25 TrailingJunkAllowed,
26 WhitespacesAllowed
27};
28
29template <typename T> struct QSimpleParsedNumber
30{
31 T result;
32 // When used < 0, -used is how much was used, but it was an error.
33 qsizetype used;
34 bool ok() const { return used > 0; }
35};
36
37// API note: this function can't process a number with more than 2.1 billion digits
38[[nodiscard]] QSimpleParsedNumber<double>
39qt_asciiToDouble(const char *num, qsizetype numLen,
40 StrayCharacterMode strayCharMode = TrailingJunkProhibited);
41void qt_doubleToAscii(double d, QLocaleData::DoubleForm form, int precision,
42 char *buf, qsizetype bufSize,
43 bool &sign, int &length, int &decpt);
44
45[[nodiscard]] QString qulltoBasicLatin(qulonglong l, int base, bool negative);
46[[nodiscard]] QString qulltoa(qulonglong l, int base, const QStringView zero);
47[[nodiscard]] Q_CORE_EXPORT QString qdtoa(qreal d, int *decpt, int *sign);
48[[nodiscard]] QString qdtoBasicLatin(double d, QLocaleData::DoubleForm form,
49 int precision, bool uppercase);
50[[nodiscard]] QByteArray qdtoAscii(double d, QLocaleData::DoubleForm form,
51 int precision, bool uppercase);
52
53[[nodiscard]] constexpr inline bool isZero(double d)
54{
55 return d == 0; // Amusingly, compilers do not grumble.
56}
57
58// Enough space for the digits before the decimal separator:
59[[nodiscard]] inline int wholePartSpace(double d)
60{
61 Q_ASSERT(d >= 0); // caller should call qAbs() if needed
62 // Optimize for numbers between -512k and 512k - otherwise, use the
63 // maximum number of digits in the whole number part of a double:
64 return d > (1 << 19) ? std::numeric_limits<double>::max_exponent10 + 1 : 6;
65}
66
67// Returns code-point of same kind (UCS2 or UCS4) as zero; digit is 0 through 9
68template <typename UcsInt>
69[[nodiscard]] inline UcsInt unicodeForDigit(uint digit, UcsInt zero)
70{
71 // Must match qlocale.cpp's NumberTokenizer's digit-digestion.
72 Q_ASSERT(digit < 10);
73 if (!digit)
74 return zero;
75
76 // See QTBUG-85409: Suzhou's digits are U+3007, U+3021, ..., U+3029
77 if (zero == u'\u3007')
78 return u'\u3020' + digit;
79 // In util/locale_database/ldml.py, LocaleScanner.numericData() asserts no
80 // other number system in CLDR has discontinuous digits.
81
82 return zero + digit;
83}
84
85[[nodiscard]] Q_CORE_EXPORT double qstrntod(const char *s00, qsizetype len,
86 char const **se, bool *ok);
87[[nodiscard]] inline double qstrtod(const char *s00, char const **se, bool *ok)
88{
89 qsizetype len = qsizetype(strlen(s: s00));
90 return qstrntod(s00, len, se, ok);
91}
92
93[[nodiscard]] QSimpleParsedNumber<qlonglong> qstrntoll(const char *nptr, qsizetype size, int base);
94[[nodiscard]] QSimpleParsedNumber<qulonglong> qstrntoull(const char *nptr, qsizetype size, int base);
95
96QT_END_NAMESPACE
97
98#endif
99

source code of qtbase/src/corelib/text/qlocale_tools_p.h