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_p.h
Go to the documentation of this file.
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// Qt-Security score:critical reason:data-parser
4
5#ifndef QLOCALE_TOOLS_P_H
6#define QLOCALE_TOOLS_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists for the convenience
13// of internal files. This header file may change from version to version
14// without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include "qlocale_p.h"
20#include "qstring.h"
21
22#if !defined(QT_SUPPORTS_INT128) && (defined(Q_CC_MSVC) && (_MSC_VER >= 1930) && __has_include(<__msvc_int128.hpp>))
23#include <__msvc_int128.hpp>
24#define QT_USE_MSVC_INT128
25#endif
26
28
29#if defined(QT_SUPPORTS_INT128)
30using qinternalint128 = qint128;
31using qinternaluint128 = quint128;
32#elif defined(QT_USE_MSVC_INT128)
33using qinternalint128 = std::_Signed128;
34using qinternaluint128 = std::_Unsigned128;
35#endif
36
41
42// API note: this function can't process a number with more than 2.1 billion digits
43[[nodiscard]] QSimpleParsedNumber<double>
44qt_asciiToDouble(const char *num, qsizetype numLen,
46void qt_doubleToAscii(double d, QLocaleData::DoubleForm form, int precision,
47 char *buf, qsizetype bufSize,
48 bool &sign, int &length, int &decpt);
49
50[[nodiscard]] QString qulltoBasicLatin(qulonglong l, int base, bool negative);
51[[nodiscard]] QString qulltoa(qulonglong l, int base, const QStringView zero);
52[[nodiscard]] char *qulltoa2(char *p, qulonglong n, int base);
53[[nodiscard]] Q_CORE_EXPORT QString qdtoa(qreal d, int *decpt, int *sign);
54[[nodiscard]] QString qdtoBasicLatin(double d, QLocaleData::DoubleForm form,
55 int precision, bool uppercase);
56[[nodiscard]] QByteArray qdtoAscii(double d, QLocaleData::DoubleForm form,
57 int precision, bool uppercase);
58
59#if defined(QT_SUPPORTS_INT128) || defined(QT_USE_MSVC_INT128)
60[[nodiscard]] Q_CORE_EXPORT QString quint128toBasicLatin(qinternaluint128 number,
61 int base = 10);
62[[nodiscard]] Q_CORE_EXPORT QString qint128toBasicLatin(qinternalint128 number,
63 int base = 10);
64#endif
65
66#if QT_VERSION < QT_VERSION_CHECK(6, 12, 0)
67[[deprecated("Use qIsNull(double) instead.")]]
68[[nodiscard]] constexpr inline bool isZero(double d)
69{
70 return qIsNull(d);
71}
72#endif
73
74// Enough space for the digits before the decimal separator:
75[[nodiscard]] inline int wholePartSpace(double d)
76{
77 Q_ASSERT(d >= 0); // caller should call qAbs() if needed
78 // Optimize for numbers between -512k and 512k - otherwise, use the
79 // maximum number of digits in the whole number part of a double:
80 return d > (1 << 19) ? std::numeric_limits<double>::max_exponent10 + 1 : 6;
81}
82
83// Returns code-point of same kind (UCS2 or UCS4) as zero; digit is 0 through 9
84template <typename UcsInt>
85[[nodiscard]] inline UcsInt unicodeForDigit(uint digit, UcsInt zero)
86{
87 // Must match QLocaleData::NumericData::digitValue()'s digit-digestion.
88 Q_ASSERT(digit < 10);
89 if (!digit)
90 return zero;
91
92 // See QTBUG-85409: Suzhou's digits are U+3007, U+3021, ..., U+3029
93 if (zero == u'\u3007')
94 return u'\u3020' + digit;
95 // In util/locale_database/ldml.py, LocaleScanner.numericData() asserts no
96 // other number system in CLDR has discontinuous digits.
97
98 return zero + digit;
99}
100
101[[nodiscard]] Q_CORE_EXPORT double qstrntod(const char *s00, qsizetype len,
102 char const **se, bool *ok);
103[[nodiscard]] inline double qstrtod(const char *s00, char const **se, bool *ok)
104{
105 qsizetype len = qsizetype(strlen(s00));
106 return qstrntod(s00, len, se, ok);
107}
108
109[[nodiscard]] Q_AUTOTEST_EXPORT
110QSimpleParsedNumber<qlonglong> qstrntoll(const char *nptr, qsizetype size, int base);
111[[nodiscard]] QSimpleParsedNumber<qulonglong> qstrntoull(const char *nptr, qsizetype size, int base);
112
113QT_END_NAMESPACE
114
115#endif
\inmodule QtCore
QDataStream & operator>>(QDataStream &in, QByteArray &ba)
Reads a byte array into ba from the stream in and returns a reference to the stream.
quint16 qChecksum(QByteArrayView data, Qt::ChecksumType standard)
Definition qlist.h:81
static QLatin1StringView scriptToCode(QLocale::Script script)
Definition qlocale.cpp:262
QString toUpper(const QString &str, bool *ok) const
static const QLocalePrivate * get(const QLocale &l)
Definition qlocale_p.h:697
QString toLower(const QString &str, bool *ok) const
quint16 languageId() const
Definition qlocale_p.h:682
static QLocale::Language codeToLanguage(QStringView code, QLocale::LanguageCodeTypes codeTypes=QLocale::AnyLanguageCode) noexcept
Definition qlocale.cpp:112
const QLocaleData *const m_data
Definition qlocale_p.h:715
QLatin1StringView scriptCode() const
Definition qlocale_p.h:692
QLocale::MeasurementSystem measurementSystem() const
Definition qlocale.cpp:3556
quint16 territoryId() const
Definition qlocale_p.h:683
static QLatin1StringView territoryToCode(QLocale::Territory territory)
Definition qlocale.cpp:270
qsizetype m_index
Definition qlocale_p.h:717
static QLocale::Territory codeToTerritory(QStringView code) noexcept
Definition qlocale.cpp:218
static std::array< char, 4 > languageToCode(QLocale::Language language, QLocale::LanguageCodeTypes codeTypes=QLocale::AnyLanguageCode)
Definition qlocale.cpp:237
static QLocale::Script codeToScript(QStringView code) noexcept
Definition qlocale.cpp:212
QLocale::NumberOptions m_numberOptions
Definition qlocale_p.h:718
QLatin1StringView territoryCode() const
Definition qlocale_p.h:694
constexpr QLocalePrivate(const QLocaleData *data, qsizetype index, QLocale::NumberOptions numberOptions=QLocale::DefaultNumberOptions, int refs=0)
Definition qlocale_p.h:676
std::array< char, 4 > languageCode(QLocale::LanguageCodeTypes codeTypes=QLocale::AnyLanguageCode) const
Definition qlocale_p.h:688
static QBasicAtomicInt s_generation
Definition qlocale_p.h:720
QByteArray bcp47Name(char separator='-') const
Definition qlocale.cpp:498
QBasicAtomicInt ref
Definition qlocale_p.h:716
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:177
qsizetype fallbackLocaleIndex() const
Definition qlocale_p.h:724
@ StringToAlternateQuotation
Definition qlocale_p.h:181
@ DateTimeToStringShort
Definition qlocale_p.h:169
@ StandaloneMonthNameLong
Definition qlocale_p.h:187
@ ListToSeparatedString
Definition qlocale_p.h:183
@ StandaloneDayNameNarrow
Definition qlocale_p.h:192
@ StandaloneMonthNameNarrow
Definition qlocale_p.h:189
@ StringToStandardQuotation
Definition qlocale_p.h:180
@ StandaloneDayNameShort
Definition qlocale_p.h:191
@ StandaloneDayNameLong
Definition qlocale_p.h:190
@ StandaloneMonthNameShort
Definition qlocale_p.h:188
virtual QLocale fallbackLocale() const
virtual QVariant query(QueryType type, QVariant &&in=QVariant()) const
Combined button and popup list for selecting options.
constexpr char ascii_space_chars[]
Definition qlocale_p.h:91
constexpr auto makeCharacterSetMatch() noexcept
Definition qlocale_p.h:100
static constexpr bool isLowerCaseAscii(char c)
static const quint16 crc_tbl[16]
QByteArray qCompress(const uchar *data, qsizetype nbytes, int compressionLevel)
ZLibOp
@ Decompression
static Q_DECL_COLD_FUNCTION const char * zlibOpAsString(ZLibOp op)
static QByteArray toCase(const QByteArray &input, QByteArray *rvalue, uchar(*lookup)(uchar))
static qsizetype q_fromPercentEncoding(QByteArrayView src, char percent, QSpan< char > buffer)
static qsizetype lastIndexOfHelper(const char *haystack, qsizetype l, const char *needle, qsizetype ol, qsizetype from)
static constexpr bool isUpperCaseAscii(char c)
static QByteArray xxflate(ZLibOp op, QArrayDataPointer< char > out, QByteArrayView input, qxp::function_ref< int(z_stream *) const > init, qxp::function_ref< int(z_stream *, size_t) const > processChunk, qxp::function_ref< void(z_stream *) const > deinit)
static constexpr uchar asciiLower(uchar c)
static qsizetype countCharHelper(QByteArrayView haystack, char needle) noexcept
static constexpr uchar asciiUpper(uchar c)
Q_CORE_EXPORT char * qstrncpy(char *dst, const char *src, size_t len)
Q_CORE_EXPORT int qstricmp(const char *, const char *)
Q_CORE_EXPORT char * qstrdup(const char *)
Q_CORE_EXPORT char * qstrcpy(char *dst, const char *src)
Q_DECL_PURE_FUNCTION Q_CORE_EXPORT const void * qmemrchr(const void *s, int needle, size_t n) noexcept
Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2)
#define __has_include(x)
#define __has_feature(x)
bool qt_splitLocaleName(QStringView name, QStringView *lang=nullptr, QStringView *script=nullptr, QStringView *cntry=nullptr) noexcept
Definition qlocale.cpp:666
Q_DECLARE_TYPEINFO(QLocaleId, Q_PRIMITIVE_TYPE)
qsizetype qt_repeatCount(QStringView s) noexcept
Definition qlocale.cpp:779
constexpr bool ascii_isspace(uchar c) noexcept
Definition qlocale_p.h:743
#define ForEachQLocaleRange(X)
Definition qlocale_p.h:626
QString qt_readEscapedFormatString(QStringView format, qsizetype *idx)
Definition qlocale.cpp:729
StrayCharacterMode
@ TrailingJunkAllowed
@ TrailingJunkProhibited
Q_CORE_EXPORT double qstrntod(const char *s00, qsizetype len, char const **se, bool *ok)
double qstrtod(const char *s00, 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)
int wholePartSpace(double d)
QString qdtoBasicLatin(double d, QLocaleData::DoubleForm form, int precision, bool uppercase)
UcsInt unicodeForDigit(uint digit, UcsInt zero)
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)
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept
Definition qsize.h:192
qregisteruint mask[Extent]
Definition qlocale_p.h:52
static constexpr int WordBits
Definition qlocale_p.h:50
static constexpr int MaxRange
Definition qlocale_p.h:51
constexpr auto maskLocation(uchar c) const noexcept
Definition qlocale_p.h:78
constexpr QCharacterSetMatch(std::string_view set) noexcept
Definition qlocale_p.h:54
constexpr bool matches(uchar c) const noexcept
Definition qlocale_p.h:63
QStringView viewListEntry(const char16_t *table, qsizetype index) const
Definition qlocale_p.h:584
char32_t ucsFirst(const char16_t *table) const
Definition qlocale_p.h:588
QString getData(const char16_t *table) const
Definition qlocale_p.h:570
QString getListEntry(const char16_t *table, qsizetype index) const
Definition qlocale_p.h:580
QStringView viewData(const char16_t *table) const
Definition qlocale_p.h:576
Descriptor for a digit sequence within a text.
Definition qlocale_p.h:453
qsizetype size() const
Returns the number of ASCII characters describing the digit sequence.
Definition qlocale_p.h:485
DigitSequence sliced(qsizetype from, qsizetype count) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qlocale_p.h:522
DigitSequence & operator=(const DigitSequence &)=default
QStringView used(const QStringView text) const
Returns the slice of text described by this digit sequence.
Definition qlocale_p.h:528
Q_DECLARE_FLAGS(Options, Option)
bool hasSign() const
Returns true precisely if the digit sequence parsed includes a leading sign.
Definition qlocale_p.h:491
bool isEmpty() const
Returns true precisely if this digit sequence represents nothing.
Definition qlocale_p.h:490
DigitSequence first(qsizetype count) const
Returns a DigitSequence describing a prefix of this.
Definition qlocale_p.h:503
void transcribeTo(CharBuff *buff) const
Transcribes the ASCII form of this digit sequence to buff.
Definition qlocale_p.h:493
DigitSequence(DigitSequence &&)=default
DigitSequence & operator=(DigitSequence &&)=default
DigitSequence sliced(qsizetype from) const
Returns a DigitSequence describing a tail of this.
Definition qlocale_p.h:513
QStringView used(const QStringView text, qsizetype from) const
Definition qlocale_p.h:526
DigitSequence last(qsizetype count) const
Returns a DigitSequence describing a tail of this.
Definition qlocale_p.h:511
qsizetype endIndex() const
Definition qlocale_p.h:479
DigitSequence(const DigitSequence &)=default
void setZero(QStringView zero)
Definition qlocale_p.h:377
bool fractionalIsGroup() const
Definition qlocale_p.h:441
qsizetype zeroWidth() const
Definition qlocale_p.h:404
const GroupSizes & groupSizes() const
Definition qlocale_p.h:405
bool isValid(NumberMode mode) const
Definition qlocale_p.h:407
const GroupSizes grouping
Definition qlocale_p.h:371
qint8 digitValue(char32_t digit) const
Definition qlocale_p.h:420
QString positiveSign() const
Definition qlocale.cpp:1124
static float convertDoubleToFloat(double d, bool *ok)
Definition qlocale_p.h:339
QString groupSeparator() const
Definition qlocale.cpp:1083
QSimpleParsedNumber< qint64 > stringToLongLong(QStringView str, int base, QLocale::NumberOptions options) const
Definition qlocale.cpp:5132
Q_AUTOTEST_EXPORT char32_t zeroUcs() const
Definition qlocale.cpp:1103
quint8 m_grouping_least
Definition qlocale_p.h:667
QString zeroDigit() const
Definition qlocale.cpp:1098
bool numberToCLocale(QStringView s, QLocale::NumberOptions number_options, NumberMode mode, CharBuff *result) const
Returns a DigitSequence describing some portion of text starting at from.
Definition qlocale.cpp:4858
quint8 m_grouping_higher
Definition qlocale_p.h:666
QString decimalPoint() const
Definition qlocale.cpp:1078
QString doubleToString(double d, int precision=-1, DoubleForm form=DFSignificantDigits, int width=-1, unsigned flags=NoFlags) const
Definition qlocale.cpp:4074
static Q_AUTOTEST_EXPORT bool allLocaleDataRows(bool(*check)(qsizetype, const QLocaleData &))
Definition qlocale.cpp:528
quint8 m_weekend_start
Definition qlocale_p.h:663
quint8 m_currency_digits
Definition qlocale_p.h:660
QLocaleId id() const
Definition qlocale_p.h:551
QString listSeparator() const
Definition qlocale.cpp:1093
DigitSequence digitSequence(QStringView text, DigitSequence::Options flags=DigitSequence::Option::Default, qsizetype from=0) const
Definition qlocale_p.h:540
static QSimpleParsedNumber< quint64 > bytearrayToUnsLongLong(QByteArrayView num, int base)
Definition qlocale.cpp:5179
QString percentSign() const
Definition qlocale.cpp:1088
@ BlankBeforePositive
Definition qlocale_p.h:270
@ AddTrailingZeroes
Definition qlocale_p.h:267
double stringToDouble(QStringView str, bool *ok, QLocale::NumberOptions options) const
Definition qlocale.cpp:5116
QString longLongToString(qint64 l, int precision=-1, int base=10, int width=-1, unsigned flags=NoFlags) const
Definition qlocale.cpp:4317
@ DoubleScientificMode
Definition qlocale_p.h:281
@ DoubleStandardMode
Definition qlocale_p.h:281
@ DFSignificantDigits
Definition qlocale_p.h:261
quint8 m_first_day_of_week
Definition qlocale_p.h:662
quint8 m_weekend_end
Definition qlocale_p.h:664
quint8 m_currency_rounding
Definition qlocale_p.h:661
QString exponentSeparator() const
Definition qlocale.cpp:1129
QString negativeSign() const
Definition qlocale.cpp:1119
QSimpleParsedNumber< quint64 > stringToUnsLongLong(QStringView str, int base, QLocale::NumberOptions options) const
Definition qlocale.cpp:5143
QString unsLongLongToString(quint64 l, int precision=-1, int base=10, int width=-1, unsigned flags=NoFlags) const
Definition qlocale.cpp:4332
quint8 m_grouping_first
Definition qlocale_p.h:665
QLocaleId withLikelySubtagsAdded() const noexcept
Definition qlocale.cpp:330
QLocaleId withLikelySubtagsRemoved() const noexcept
Definition qlocale.cpp:419
ushort script_id
Definition qlocale_p.h:242
bool operator==(QLocaleId other) const noexcept
Definition qlocale_p.h:211
bool matchesAll() const noexcept
Definition qlocale_p.h:220
bool isValid() const noexcept
Definition qlocale_p.h:215
bool operator!=(QLocaleId other) const noexcept
Definition qlocale_p.h:213
bool acceptScriptTerritory(QLocaleId other) const noexcept
Definition qlocale_p.h:231
ushort territory_id
Definition qlocale_p.h:242
bool acceptLanguage(quint16 lang) const noexcept
Definition qlocale_p.h:225
ushort language_id
Definition qlocale_p.h:242
CurrencyToStringArgument(const QVariant &v, const QString &s)
Definition qlocale_p.h:138