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_p.h
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
6#ifndef QLOCALE_P_H
7#define QLOCALE_P_H
8
9//
10// W A R N I N G
11// -------------
12//
13// This file is not part of the Qt API. It exists for the convenience
14// of internal files. This header file may change from version to version
15// without notice, or even be removed.
16//
17// We mean it.
18//
19
20#include "qlocale.h"
21
22#include <QtCore/qcalendar.h>
23#include <QtCore/qlist.h>
24#include <QtCore/qnumeric.h>
25#include <QtCore/private/qnumeric_p.h>
26#include <QtCore/qstring.h>
27#include <QtCore/qvariant.h>
28#include <QtCore/qvarlengtharray.h>
29#ifdef Q_OS_WASM
30#include <private/qstdweb_p.h>
31#endif
32
33#include <limits>
34#include <cmath>
35#include <string_view>
36
37QT_BEGIN_NAMESPACE
38
39template <typename T> struct QSimpleParsedNumber
40{
41 T result;
42 // When used < 0, -used is how much was used, but it was an error.
43 qsizetype used;
44 bool ok() const { return used > 0; }
45};
46
47template <int Extent, uchar Lowest> struct QCharacterSetMatch
48{
50 static constexpr int WordBits = std::numeric_limits<Word>::digits;
51 static constexpr int MaxRange = WordBits * Extent;
53
54 constexpr QCharacterSetMatch(std::string_view set) noexcept
55 : mask{}
56 {
57 for (char c : set) {
58 auto [offset, shift] = maskLocation(c);
59 mask[offset] |= Word(1) << shift;
60 }
61 }
62
63 constexpr bool matches(uchar c) const noexcept
64 {
65 auto [offset, shift] = maskLocation(c);
66 if (offset < 0)
67 return false;
68 Word m = 0;
69 if constexpr (Extent == 2) {
70 // special case for faster code (with GCC, at least)
71 m = (c - Lowest < WordBits) ? mask[0] : mask[1];
72 } else {
73 m = mask[offset];
74 }
75 return (m >> shift) & 1;
76 }
77
78 constexpr auto maskLocation(uchar c) const noexcept
79 {
80 struct { int offset; int shift; } r = { -1, -1 };
81 unsigned idx = c - Lowest;
82 if (idx < MaxRange) {
83 r.offset = idx / WordBits;
84 r.shift = idx % WordBits;
85 }
86 return r;
87 }
88};
89
90namespace QtPrivate {
91inline constexpr char ascii_space_chars[] =
92 "\t" // 9: HT - horizontal tab
93 "\n" // 10: LF - line feed
94 "\v" // 11: VT - vertical tab
95 "\f" // 12: FF - form feed
96 "\r" // 13: CR - carriage return
97 " "; // 32: space
98
99template <const char *Set, int ForcedLowest = -1>
100inline constexpr auto makeCharacterSetMatch() noexcept
101{
102 constexpr int BitsPerWord = std::numeric_limits<qregisteruint>::digits;
103 constexpr auto view = std::string_view(Set);
104 constexpr uchar MinElement = *std::min_element(view.begin(), view.end());
105 constexpr uchar MaxElement = *std::max_element(view.begin(), view.end());
106 constexpr int Range = MaxElement - MinElement;
107 constexpr int Extent = (Range + BitsPerWord - 1) / BitsPerWord;
108 constexpr int TotalBits = BitsPerWord * Extent;
109
110 if constexpr (ForcedLowest >= 0) {
111 // use the force
112 static_assert(ForcedLowest <= int(MinElement), "The force is not with you");
113 static_assert(ForcedLowest + TotalBits >= MaxElement, "The force is not with you");
114 return QCharacterSetMatch<Extent, ForcedLowest>(view);
115 } else if constexpr (MaxElement < TotalBits) {
116 // if we can use a Lowest of zero, we can remove a subtraction
117 // from the matches() code at runtime
118 return QCharacterSetMatch<Extent, 0>(view);
119 } else {
120 return QCharacterSetMatch<Extent, MinElement>(view);
121 }
122}
123} // QtPrivate
124
125// Subclassed by Android platform plugin:
126class Q_CORE_EXPORT QSystemLocale
127{
128 Q_DISABLE_COPY_MOVE(QSystemLocale)
129 QSystemLocale *next = nullptr; // Maintains a stack.
130
131public:
132 QSystemLocale();
133 virtual ~QSystemLocale();
134
136 {
138 CurrencyToStringArgument(const QVariant &v, const QString &s)
139 : value(v), symbol(s) { }
140 QVariant value;
141 QString symbol;
142 };
143
145 LanguageId, // uint
146 TerritoryId, // uint
147 DecimalPoint, // QString
148 Grouping, // QLocaleData::GroupSizes
149 GroupSeparator, // QString (empty QString means: don't group digits)
150 ZeroDigit, // QString
151 NegativeSign, // QString
152 DateFormatLong, // QString
153 DateFormatShort, // QString
154 TimeFormatLong, // QString
155 TimeFormatShort, // QString
156 DayNameLong, // QString, in: int
157 DayNameShort, // QString, in: int
158 DayNameNarrow, // QString, in: int
159 MonthNameLong, // QString, in: int
160 MonthNameShort, // QString, in: int
161 MonthNameNarrow, // QString, in: int
162 DateToStringLong, // QString, in: QDate
163 DateToStringShort, // QString in: QDate
164 TimeToStringLong, // QString in: QTime
165 TimeToStringShort, // QString in: QTime
168 DateTimeToStringLong, // QString in: QDateTime
169 DateTimeToStringShort, // QString in: QDateTime
171 PositiveSign, // QString
172 AMText, // QString
173 PMText, // QString
174 FirstDayOfWeek, // Qt::DayOfWeek
175 Weekdays, // QList<Qt::DayOfWeek>
176 CurrencySymbol, // QString in: CurrencyToStringArgument
177 CurrencyToString, // QString in: qlonglong, qulonglong or double
178 Collation, // QString
179 UILanguages, // QStringList
180 StringToStandardQuotation, // QString in: QStringView to quote
181 StringToAlternateQuotation, // QString in: QStringView to quote
182 ScriptId, // uint
184 LocaleChanged, // system locale changed
187 StandaloneMonthNameLong, // QString, in: int
188 StandaloneMonthNameShort, // QString, in: int
189 StandaloneMonthNameNarrow, // QString, in: int
190 StandaloneDayNameLong, // QString, in: int
191 StandaloneDayNameShort, // QString, in: int
192 StandaloneDayNameNarrow // QString, in: int
193 };
194 virtual QVariant query(QueryType type, QVariant &&in = QVariant()) const;
195
196 virtual QLocale fallbackLocale() const;
197 inline qsizetype fallbackLocaleIndex() const;
198
199protected:
200 inline const QSharedDataPointer<QLocalePrivate> localeData(const QLocale &locale) const
201 {
202 return locale.d;
203 }
204};
205Q_DECLARE_TYPEINFO(QSystemLocale::QueryType, Q_PRIMITIVE_TYPE);
206Q_DECLARE_TYPEINFO(QSystemLocale::CurrencyToStringArgument, Q_RELOCATABLE_TYPE);
207
209{
210 [[nodiscard]] Q_AUTOTEST_EXPORT static QLocaleId fromName(QStringView name) noexcept;
211 [[nodiscard]] inline bool operator==(QLocaleId other) const noexcept
212 { return language_id == other.language_id && script_id == other.script_id && territory_id == other.territory_id; }
213 [[nodiscard]] inline bool operator!=(QLocaleId other) const noexcept
214 { return !operator==(other); }
215 [[nodiscard]] inline bool isValid() const noexcept
216 {
217 return language_id <= QLocale::LastLanguage && script_id <= QLocale::LastScript
218 && territory_id <= QLocale::LastTerritory;
219 }
220 [[nodiscard]] inline bool matchesAll() const noexcept
221 {
222 return !language_id && !script_id && !territory_id;
223 }
224 // Use as: filter.accept...(candidate)
225 [[nodiscard]] inline bool acceptLanguage(quint16 lang) const noexcept
226 {
227 // Always reject AnyLanguage (only used for last entry in locale_data array).
228 // So, when searching for AnyLanguage, accept everything *but* AnyLanguage.
229 return language_id ? lang == language_id : lang;
230 }
231 [[nodiscard]] inline bool acceptScriptTerritory(QLocaleId other) const noexcept
232 {
233 return (!territory_id || other.territory_id == territory_id)
234 && (!script_id || other.script_id == script_id);
235 }
236
237 [[nodiscard]] QLocaleId withLikelySubtagsAdded() const noexcept;
238 [[nodiscard]] QLocaleId withLikelySubtagsRemoved() const noexcept;
239
240 [[nodiscard]] Q_AUTOTEST_EXPORT QByteArray name(char separator = '-') const;
241
242 ushort language_id = 0, script_id = 0, territory_id = 0;
243};
245
247{
248public:
249 // Having an index for each locale enables us to have diverse sources of
250 // data, e.g. calendar locales, as well as the main CLDR-derived data.
251 [[nodiscard]] Q_AUTOTEST_EXPORT static qsizetype findLocaleIndex(QLocaleId localeId) noexcept;
252 [[nodiscard]] Q_AUTOTEST_EXPORT static const QLocaleData *c() noexcept;
253 [[nodiscard]] Q_AUTOTEST_EXPORT
254 static bool allLocaleDataRows(bool (*check)(qsizetype, const QLocaleData &));
255 [[nodiscard]] Q_AUTOTEST_EXPORT
256 static const QLocaleData *dataForLocaleIndex(qsizetype index);
257
264
280
282
283 struct GroupSizes // Numbers of digits in various groups:
284 {
285 int first = 0; // Min needed before the separator, when there's only one.
286 int higher = 0; // Each group between separators.
287 int least = 0; // Least significant, when any separators appear.
288 bool isValid() const { return least > 0 && higher > first && first > 0; }
289 };
290
291 using CharBuff = QVarLengthArray<char, 256>;
292
294 {
295 enum State { // A duplicate of QValidator::State
299 };
300
303 };
304
305private:
306 enum PrecisionMode {
307 PMDecimalDigits = 0x01,
308 PMSignificantDigits = 0x02,
309 PMChopTrailingZeros = 0x03
310 };
311
312 [[nodiscard]] QString decimalForm(QString &&digits, int decpt, int precision,
313 PrecisionMode pm, bool mustMarkDecimal,
314 bool groupDigits) const;
315 [[nodiscard]] QString exponentForm(QString &&digits, int decpt, int precision,
316 PrecisionMode pm, bool mustMarkDecimal,
317 int minExponentDigits) const;
318 [[nodiscard]] QString signPrefix(bool negative, unsigned flags) const;
319 [[nodiscard]] QString applyIntegerFormatting(QString &&numStr, bool negative, int precision,
320 int base, int width, unsigned flags) const;
321
322public:
323 [[nodiscard]] QString doubleToString(double d,
324 int precision = -1,
326 int width = -1,
327 unsigned flags = NoFlags) const;
328 [[nodiscard]] QString longLongToString(qint64 l, int precision = -1,
329 int base = 10,
330 int width = -1,
331 unsigned flags = NoFlags) const;
332 [[nodiscard]] QString unsLongLongToString(quint64 l, int precision = -1,
333 int base = 10,
334 int width = -1,
335 unsigned flags = NoFlags) const;
336
337 // this function is meant to be called with the result of stringToDouble or bytearrayToDouble
338 // so *ok must have been properly set (if not null)
339 [[nodiscard]] static float convertDoubleToFloat(double d, bool *ok)
340 {
341 float result;
342 bool b = convertDoubleTo<float>(d, &result);
343 if (ok && *ok)
344 *ok = b;
345 return result;
346 }
347
348 [[nodiscard]] double stringToDouble(QStringView str, bool *ok,
349 QLocale::NumberOptions options) const;
350 [[nodiscard]] QSimpleParsedNumber<qint64>
351 stringToLongLong(QStringView str, int base, QLocale::NumberOptions options) const;
352 [[nodiscard]] QSimpleParsedNumber<quint64>
353 stringToUnsLongLong(QStringView str, int base, QLocale::NumberOptions options) const;
354
355 // this function is used in QIntValidator (QtGui)
358 [[nodiscard]] static QSimpleParsedNumber<quint64>
359 bytearrayToUnsLongLong(QByteArrayView num, int base);
360
361 [[nodiscard]] bool numberToCLocale(QStringView s, QLocale::NumberOptions number_options,
362 NumberMode mode, CharBuff *result) const;
363
365 {
366#ifndef QT_NO_SYSTEMLOCALE
367 // Only used for the system locale, to store data for the view to look at:
369#endif
372 char32_t zeroUcs = 0;
374 bool exponentCyrillic = false; // True only for floating-point parsing of Cyrillic.
375 const bool isC; // C locale sets this and nothing else.
376
377 void setZero(QStringView zero)
378 {
379 Q_PRE(!isC);
380 // No known locale has digits that are more than one Unicode
381 // code-point, so we can safely deal with digits as plain char32_t.
382 switch (zero.size()) {
383 case 1:
384 Q_ASSERT(!zero.at(0).isSurrogate());
385 zeroUcs = zero.at(0).unicode();
386 Q_ASSERT(!QChar::requiresSurrogates(zeroUcs + 9));
387 zeroLen = 1;
388 break;
389 case 2:
390 Q_ASSERT(zero.at(0).isHighSurrogate());
391 Q_ASSERT(zero.at(1).isLowSurrogate());
392 zeroUcs = QChar::surrogateToUcs4(zero.at(0), zero.at(1));
393 Q_ASSERT(QChar::requiresSurrogates(zeroUcs));
394 zeroLen = 2;
395 break;
396 default:
397 Q_ASSERT(zero.size() == 0); // i.e. we got no value to use
398 break;
399 }
400 }
401 Q_NODISCARD_CTOR Q_AUTOTEST_EXPORT
403
404 [[nodiscard]] qsizetype zeroWidth() const { return isC ? 1 : zeroLen; }
405 [[nodiscard]] const GroupSizes &groupSizes() const { return grouping; }
406
407 [[nodiscard]] bool isValid(NumberMode mode) const // Asserted as a sanity check.
408 {
409 if (isC)
410 return true;
411 if (exponentCyrillic && exponent != u"E" && exponent != u"\u0415")
412 return false;
413 return (zeroLen == 1 || zeroLen == 2) && zeroUcs > 0
414 && (mode == IntegerMode || !decimal.isEmpty())
415 // group may be empty (user config in system locale)
416 && !minus.isEmpty() && !plus.isEmpty()
417 && (mode != DoubleScientificMode || !exponent.isEmpty());
418 }
419
420 [[nodiscard]] qint8 digitValue(char32_t digit) const
421 {
422 // Compute locale-appropriate digit value (or -1)
423 if (!isC && zeroUcs != U'0') {
424 // Must match qlocale_tools_p.h's unicodeForDigit().
425 if (digit == zeroUcs || zeroUcs != U'\u3007') {
426 if (qint32 ans = digit - zeroUcs; 0 <= ans && ans <= 9)
427 return qint8(ans);
428 } else if (digit > U'\u3020') {
429 if (qint32 ans = digit - U'\u3020'; 0 <= ans && ans <= 9)
430 return qint8(ans);
431 }
432 // Accepting ASCII with zeroLen != 1 would mess up code that
433 // assumes consistent digit width.
434 if (zeroLen != 1)
435 return -1;
436 }
437 qint32 ans = digit - U'0';
438 return qint8(0 <= ans && ans <= 9 ? ans : -1);
439 }
440
441 [[nodiscard]] bool fractionalIsGroup() const
442 {
443 // True precisely if fractional part separator and digit-grouping
444 // separator are the same. This can happen due to user
445 // mis-configuration. (Our CLDR-digestion scripts check against CLDR
446 // having such clashes.)
447 // For C locale we don't store separators, we just know them.
448 return Q_UNLIKELY(!isC && group == decimal);
449 }
450 };
451
453 {
454 QByteArray digits; // ASCII digits
455 // Index just before start of first digit.
457 // digits.sliced(n, m), comes from
458 // text.sliced(digitStart + n * digitWidth, m * digitWidth)
459 qint8 digitWidth; // Width within text of each digit.
460 char sign = '\0'; // '\0' for unspecified, or '-' or '+' if specified.
461
462 enum class Option {
465 };
467
468 Q_AUTOTEST_EXPORT // All the hard work is in this constructor:
471 qsizetype from = 0);
472 DigitSequence(const DigitSequence &) = default;
476 ~DigitSequence() = default;
477
478 // Where in text did the parse end:
480 {
481 return digitStart + digits.size() * digitWidth;
482 }
483
484 // How many ASCII characters the sequence transcribes to:
486 {
487 return digits.size() + (hasSign() ? 1 : 0);
488 }
489
490 bool isEmpty() const { return size() == 0; }
491 bool hasSign() const { return sign != '\0'; }
492
493 void transcribeTo(CharBuff *buff) const
494 {
495 if (sign)
496 buff->append(sign);
497 buff->append(digits.constData(), digits.size());
498 }
499
500 // Subsequences (for when partitioning among fields).
501 // Sign, when present, is treated as entry 0, bumping digit indices up by 1.
502 // Generally, first(c) and sliced(c) abut but don't overlap.
503 DigitSequence first(qsizetype count) const
504 {
505 Q_PRE(count >= 0);
506 Q_PRE(count <= size());
507 const qsizetype n = hasSign() ? count - 1 : count;
508 // Stop at end of last included (sign or) digit:
509 return { digits.first(n), digitStart, digitWidth, count ? sign : '\0' };
510 }
511 DigitSequence last(qsizetype count) const
512 { return sliced(size() - count); }
513 DigitSequence sliced(qsizetype from) const
514 {
515 Q_PRE(from >= 0);
516 Q_PRE(from <= size());
517 const qsizetype n = hasSign() ? from - 1 : from;
518 Q_ASSERT(n <= digits.size());
519 return { digits.sliced(n), digitStart + digitWidth * n,
520 digitWidth, from ? '\0' : sign };
521 }
522 DigitSequence sliced(qsizetype from, qsizetype count) const
523 { return sliced(from).first(count); }
524
525 // Portion of the view from which it was constructed:
526 [[nodiscard]] QStringView used(const QStringView text, qsizetype from) const
527 { return text.first(endIndex()).sliced(from); }
528 [[nodiscard]] QStringView used(const QStringView text) const // digits-only:
529 { return text.sliced(digitStart, digits.size() * digitWidth); }
530 // (can apply to the first()/last()/sliced() of the result of parsing)
531 private:
532 // Used by first(), sliced():
533 DigitSequence(QByteArray &&digs, qsizetype dStart, qint8 dWidth, char sgn)
534 : digits(std::move(digs)), digitStart(dStart), digitWidth(dWidth), sign(sgn)
535 {
536 }
537 };
538
539 [[nodiscard]]
540 DigitSequence digitSequence(QStringView text,
541 DigitSequence::Options flags = DigitSequence::Option::Default,
542 qsizetype from = 0) const
543 { return DigitSequence(text, NumericData(this, IntegerMode), flags, from); }
544
545 // this function is used in QIntValidator (QtGui)
549
550 // Access to assorted data members:
551 [[nodiscard]] QLocaleId id() const
552 { return QLocaleId { m_language_id, m_script_id, m_territory_id }; }
553
554 [[nodiscard]] QString decimalPoint() const;
555 [[nodiscard]] QString groupSeparator() const;
556 [[nodiscard]] QString listSeparator() const;
557 [[nodiscard]] QString percentSign() const;
558 [[nodiscard]] QString zeroDigit() const;
559 [[nodiscard]] Q_AUTOTEST_EXPORT char32_t zeroUcs() const;
560 [[nodiscard]] QString positiveSign() const;
561 [[nodiscard]] QString negativeSign() const;
562 [[nodiscard]] QString exponentSeparator() const;
564
566 {
567 using Index = quint32;
568 Index offset; // Some zone data tables are big.
569 Index size; // (for consistency and to avoid struct-padding)
570 [[nodiscard]] QString getData(const char16_t *table) const
571 {
572 return size > 0
573 ? QString::fromRawData(stringStart(table), stringSize())
574 : QString();
575 }
576 [[nodiscard]] QStringView viewData(const char16_t *table) const
577 {
578 return { stringStart(table), stringSize() };
579 }
580 [[nodiscard]] QString getListEntry(const char16_t *table, qsizetype index) const
581 {
582 return listEntry(table, index).getData(table);
583 }
584 [[nodiscard]] QStringView viewListEntry(const char16_t *table, qsizetype index) const
585 {
586 return listEntry(table, index).viewData(table);
587 }
588 [[nodiscard]] char32_t ucsFirst(const char16_t *table) const
589 {
590 if (size && !QChar::isSurrogate(table[offset]))
591 return table[offset];
592 if (size > 1 && QChar::isHighSurrogate(table[offset]))
593 return QChar::surrogateToUcs4(table[offset], table[offset + 1]);
594 return 0;
595 }
596 private:
597 [[nodiscard]] const QChar *stringStart(const char16_t *table) const
598 {
599 return reinterpret_cast<const QChar *>(table + offset);
600 }
601 [[nodiscard]] qsizetype stringSize() const
602 {
603 // On 32-bit platforms, this is a narrowing cast, but the size has
604 // always come from an 8-bit or 16-bit table value so can't actually
605 // have a problem with that.
606 qsizetype result = static_cast<qsizetype>(size);
607 Q_ASSERT(result >= 0);
608 return result;
609 }
610 [[nodiscard]] DataRange listEntry(const char16_t *table, qsizetype index) const
611 {
612 constexpr char16_t separator = u';';
613 Index i = 0;
614 while (index > 0 && i < size) {
615 if (table[offset + i] == separator)
616 index--;
617 i++;
618 }
619 Index end = i;
620 while (end < size && table[offset + end] != separator)
621 end++;
622 return { offset + i, end - i };
623 }
624 };
625
626#define ForEachQLocaleRange(X)
627 X(startListPattern) X(midListPattern) X(endListPattern) X(pairListPattern) X(listDelimit)
628 X(decimalSeparator) X(groupDelim) X(percent) X(zero) X(minus) X(plus) X(exponential)
629 X(quoteStart) X(quoteEnd) X(quoteStartAlternate) X(quoteEndAlternate)
630 X(longDateFormat) X(shortDateFormat) X(longTimeFormat) X(shortTimeFormat)
631 X(longDayNamesStandalone) X(longDayNames)
632 X(shortDayNamesStandalone) X(shortDayNames)
633 X(narrowDayNamesStandalone) X(narrowDayNames)
634 X(anteMeridiem) X(postMeridiem)
635 X(byteCount) X(byteAmountSI) X(byteAmountIEC)
636 X(currencySymbol) X(currencyDisplayName)
637 X(currencyFormat) X(currencyFormatNegative)
638 X(endonymLanguage) X(endonymTerritory)
639
640#define rangeGetter(name)
641 [[nodiscard]] DataRange name() const { return { m_ ## name ## _idx, m_ ## name ## _size }; }
643#undef rangeGetter
644
645public:
646 quint16 m_language_id, m_script_id, m_territory_id;
647
648 // Offsets, then sizes, for each range:
649#define rangeIndex(name) quint16 m_ ## name ## _idx;
651#undef rangeIndex
652#define Size(name) quint8 m_ ## name ## _size;
654#undef Size
655
656#undef ForEachQLocaleRange
657
658 // Strays:
659 char m_currency_iso_code[3];
661 quint8 m_currency_rounding : 3; // (not yet used !)
665 quint8 m_grouping_first : 2; // Don't group until more significant group has this many digits.
666 quint8 m_grouping_higher : 3; // Number of digits between grouping separators
667 quint8 m_grouping_least : 3; // Number of digits after last grouping separator (before decimal).
668};
669
670Q_DECLARE_OPERATORS_FOR_FLAGS(QLocaleData::DigitSequence::Options)
671Q_DECLARE_TYPEINFO(QLocaleData::GroupSizes, Q_PRIMITIVE_TYPE);
672
674{
675public:
676 constexpr QLocalePrivate(const QLocaleData *data, qsizetype index,
677 QLocale::NumberOptions numberOptions = QLocale::DefaultNumberOptions,
678 int refs = 0)
681
682 [[nodiscard]] quint16 languageId() const { return m_data->m_language_id; }
683 [[nodiscard]] quint16 territoryId() const { return m_data->m_territory_id; }
684
685 [[nodiscard]] QByteArray bcp47Name(char separator = '-') const;
686
687 [[nodiscard]] inline std::array<char, 4>
688 languageCode(QLocale::LanguageCodeTypes codeTypes = QLocale::AnyLanguageCode) const
689 {
690 return languageToCode(QLocale::Language(m_data->m_language_id), codeTypes);
691 }
692 [[nodiscard]] inline QLatin1StringView scriptCode() const
693 { return scriptToCode(QLocale::Script(m_data->m_script_id)); }
694 [[nodiscard]] inline QLatin1StringView territoryCode() const
695 { return territoryToCode(QLocale::Territory(m_data->m_territory_id)); }
696
697 [[nodiscard]] static const QLocalePrivate *get(const QLocale &l) { return l.d; }
698 [[nodiscard]] static std::array<char, 4>
699 languageToCode(QLocale::Language language,
700 QLocale::LanguageCodeTypes codeTypes = QLocale::AnyLanguageCode);
701 [[nodiscard]] static QLatin1StringView scriptToCode(QLocale::Script script);
702 [[nodiscard]] static QLatin1StringView territoryToCode(QLocale::Territory territory);
703 [[nodiscard]] static QLocale::Language
704 codeToLanguage(QStringView code,
705 QLocale::LanguageCodeTypes codeTypes = QLocale::AnyLanguageCode) noexcept;
706 [[nodiscard]] static QLocale::Script codeToScript(QStringView code) noexcept;
707 [[nodiscard]] static QLocale::Territory codeToTerritory(QStringView code) noexcept;
708
709 [[nodiscard]] QLocale::MeasurementSystem measurementSystem() const;
710
711 [[nodiscard]] QString toUpper(const QString &str, bool *ok) const;
712 [[nodiscard]] QString toLower(const QString &str, bool *ok) const;
713
714 // System locale has an m_data all its own; all others have m_data = locale_data + m_index
715 const QLocaleData *const m_data;
717 qsizetype m_index; // System locale needs this updated when m_data->id() changes.
719
721};
722
723#ifndef QT_NO_SYSTEMLOCALE
724qsizetype QSystemLocale::fallbackLocaleIndex() const { return fallbackLocale().d->m_index; }
725#endif
726
727template <>
728inline QLocalePrivate *QSharedDataPointer<QLocalePrivate>::clone()
729{
730 // cannot use QLocalePrivate's copy constructor
731 // since it is deleted in C++11
732 return new QLocalePrivate(d->m_data, d->m_index, d->m_numberOptions);
733}
734
735// Also used to merely skip over an escape in a format string, advancint idx to
736// point after it (so not [[nodiscard]]):
737QString qt_readEscapedFormatString(QStringView format, qsizetype *idx);
738[[nodiscard]] bool qt_splitLocaleName(QStringView name, QStringView *lang = nullptr,
739 QStringView *script = nullptr,
740 QStringView *cntry = nullptr) noexcept;
741[[nodiscard]] qsizetype qt_repeatCount(QStringView s) noexcept;
742
743[[nodiscard]] constexpr inline bool ascii_isspace(uchar c) noexcept
744{
745 constexpr auto matcher = QtPrivate::makeCharacterSetMatch<QtPrivate::ascii_space_chars>();
746 return matcher.matches(c);
747}
748
749QT_END_NAMESPACE
750
751// ### move to qnamespace.h
752QT_DECL_METATYPE_EXTERN_TAGGED(QList<Qt::DayOfWeek>, QList_Qt__DayOfWeek, Q_CORE_EXPORT)
753#ifndef QT_NO_SYSTEMLOCALE
754QT_DECL_METATYPE_EXTERN_TAGGED(QSystemLocale::CurrencyToStringArgument,
755 QSystemLocale__CurrencyToStringArgument, Q_CORE_EXPORT)
756#endif
757
758#endif // QLOCALE_P_H
\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
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
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_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
QByteArray qdtoAscii(double d, QLocaleData::DoubleForm form, int precision, bool uppercase)
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