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
5#ifndef QLOCALE_P_H
6#define QLOCALE_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.h"
20
21#include <QtCore/private/qglobal_p.h>
22#include <QtCore/qcalendar.h>
23#include <QtCore/qlist.h>
24#include <QtCore/qnumeric.h>
25#include <QtCore/qstring.h>
26#include <QtCore/qvariant.h>
27#include <QtCore/qvarlengtharray.h>
28
29#include <limits>
30#include <cmath>
31#include <string_view>
32
33QT_BEGIN_NAMESPACE
34
35template <typename MaskType, uchar Lowest> struct QCharacterSetMatch
36{
37 static constexpr int MaxRange = std::numeric_limits<MaskType>::digits;
38 MaskType mask;
39
40 constexpr QCharacterSetMatch(std::string_view set)
41 : mask(0)
42 {
43 for (char c : set) {
44 int idx = uchar(c) - Lowest;
45 mask |= MaskType(1) << idx;
46 }
47 }
48
49 constexpr bool matches(uchar c) const
50 {
51 unsigned idx = c - Lowest;
52 if (idx >= MaxRange)
53 return false;
54 return (mask >> idx) & 1;
55 }
56};
57
58namespace QtPrivate {
59inline constexpr char ascii_space_chars[] =
60 "\t" // 9: HT - horizontal tab
61 "\n" // 10: LF - line feed
62 "\v" // 11: VT - vertical tab
63 "\f" // 12: FF - form feed
64 "\r" // 13: CR - carriage return
65 " "; // 32: space
66
67template <const char *Set, int ForcedLowest = -1>
68inline constexpr auto makeCharacterSetMatch()
69{
70 constexpr auto view = std::string_view(Set);
71 constexpr uchar MinElement = *std::min_element(first: view.begin(), last: view.end());
72 constexpr uchar MaxElement = *std::max_element(first: view.begin(), last: view.end());
73 constexpr int Range = MaxElement - MinElement;
74 static_assert(Range < 64, "Characters in the set are 64 or more values apart");
75
76 if constexpr (ForcedLowest >= 0) {
77 // use the force
78 static_assert(ForcedLowest <= int(MinElement), "The force is not with you");
79 using MaskType = std::conditional_t<MaxElement - ForcedLowest < 32, quint32, quint64>;
80 return QCharacterSetMatch<MaskType, ForcedLowest>(view);
81 } else if constexpr (MaxElement < std::numeric_limits<qregisteruint>::digits) {
82 // if we can use a Lowest of zero, we can remove a subtraction
83 // from the matches() code at runtime
84 using MaskType = std::conditional_t<(MaxElement < 32), quint32, qregisteruint>;
85 return QCharacterSetMatch<MaskType, 0>(view);
86 } else {
87 using MaskType = std::conditional_t<(Range < 32), quint32, quint64>;
88 return QCharacterSetMatch<MaskType, MinElement>(view);
89 }
90}
91} // QtPrivate
92
93struct QLocaleData;
94// Subclassed by Android platform plugin:
95class Q_CORE_EXPORT QSystemLocale
96{
97 QSystemLocale *next = nullptr; // Maintains a stack.
98public:
99 QSystemLocale();
100 virtual ~QSystemLocale();
101
102 struct CurrencyToStringArgument
103 {
104 CurrencyToStringArgument() { }
105 CurrencyToStringArgument(const QVariant &v, const QString &s)
106 : value(v), symbol(s) { }
107 QVariant value;
108 QString symbol;
109 };
110
111 enum QueryType {
112 LanguageId, // uint
113 TerritoryId, // uint
114 DecimalPoint, // QString
115 GroupSeparator, // QString (empty QString means: don't group digits)
116 ZeroDigit, // QString
117 NegativeSign, // QString
118 DateFormatLong, // QString
119 DateFormatShort, // QString
120 TimeFormatLong, // QString
121 TimeFormatShort, // QString
122 DayNameLong, // QString, in: int
123 DayNameShort, // QString, in: int
124 DayNameNarrow, // QString, in: int
125 MonthNameLong, // QString, in: int
126 MonthNameShort, // QString, in: int
127 MonthNameNarrow, // QString, in: int
128 DateToStringLong, // QString, in: QDate
129 DateToStringShort, // QString in: QDate
130 TimeToStringLong, // QString in: QTime
131 TimeToStringShort, // QString in: QTime
132 DateTimeFormatLong, // QString
133 DateTimeFormatShort, // QString
134 DateTimeToStringLong, // QString in: QDateTime
135 DateTimeToStringShort, // QString in: QDateTime
136 MeasurementSystem, // uint
137 PositiveSign, // QString
138 AMText, // QString
139 PMText, // QString
140 FirstDayOfWeek, // Qt::DayOfWeek
141 Weekdays, // QList<Qt::DayOfWeek>
142 CurrencySymbol, // QString in: CurrencyToStringArgument
143 CurrencyToString, // QString in: qlonglong, qulonglong or double
144 Collation, // QString
145 UILanguages, // QStringList
146 StringToStandardQuotation, // QString in: QStringView to quote
147 StringToAlternateQuotation, // QString in: QStringView to quote
148 ScriptId, // uint
149 ListToSeparatedString, // QString
150 LocaleChanged, // system locale changed
151 NativeLanguageName, // QString
152 NativeTerritoryName, // QString
153 StandaloneMonthNameLong, // QString, in: int
154 StandaloneMonthNameShort, // QString, in: int
155 StandaloneMonthNameNarrow, // QString, in: int
156 StandaloneDayNameLong, // QString, in: int
157 StandaloneDayNameShort, // QString, in: int
158 StandaloneDayNameNarrow // QString, in: int
159 };
160 virtual QVariant query(QueryType type, QVariant in = QVariant()) const;
161
162 virtual QLocale fallbackLocale() const;
163 inline qsizetype fallbackLocaleIndex() const;
164};
165Q_DECLARE_TYPEINFO(QSystemLocale::QueryType, Q_PRIMITIVE_TYPE);
166Q_DECLARE_TYPEINFO(QSystemLocale::CurrencyToStringArgument, Q_RELOCATABLE_TYPE);
167
168#if QT_CONFIG(icu)
169namespace QIcu {
170 QString toUpper(const QByteArray &localeId, const QString &str, bool *ok);
171 QString toLower(const QByteArray &localeId, const QString &str, bool *ok);
172}
173#endif
174
175
176struct QLocaleId
177{
178 [[nodiscard]] Q_AUTOTEST_EXPORT static QLocaleId fromName(QStringView name);
179 [[nodiscard]] inline bool operator==(QLocaleId other) const
180 { return language_id == other.language_id && script_id == other.script_id && territory_id == other.territory_id; }
181 [[nodiscard]] inline bool operator!=(QLocaleId other) const
182 { return !operator==(other); }
183 [[nodiscard]] inline bool isValid() const
184 {
185 return language_id <= QLocale::LastLanguage && script_id <= QLocale::LastScript
186 && territory_id <= QLocale::LastTerritory;
187 }
188 [[nodiscard]] inline bool matchesAll() const
189 {
190 return !language_id && !script_id && !territory_id;
191 }
192 // Use as: filter.accept...(candidate)
193 [[nodiscard]] inline bool acceptLanguage(quint16 lang) const
194 {
195 // Always reject AnyLanguage (only used for last entry in locale_data array).
196 // So, when searching for AnyLanguage, accept everything *but* AnyLanguage.
197 return language_id ? lang == language_id : lang;
198 }
199 [[nodiscard]] inline bool acceptScriptTerritory(QLocaleId other) const
200 {
201 return (!territory_id || other.territory_id == territory_id)
202 && (!script_id || other.script_id == script_id);
203 }
204
205 [[nodiscard]] QLocaleId withLikelySubtagsAdded() const;
206 [[nodiscard]] QLocaleId withLikelySubtagsRemoved() const;
207
208 [[nodiscard]] QByteArray name(char separator = '-') const;
209
210 ushort language_id = 0, script_id = 0, territory_id = 0;
211};
212Q_DECLARE_TYPEINFO(QLocaleId, Q_PRIMITIVE_TYPE);
213
214
215using CharBuff = QVarLengthArray<char, 256>;
216
217struct QLocaleData
218{
219public:
220 // Having an index for each locale enables us to have diverse sources of
221 // data, e.g. calendar locales, as well as the main CLDR-derived data.
222 [[nodiscard]] static qsizetype findLocaleIndex(QLocaleId localeId);
223 [[nodiscard]] static const QLocaleData *c();
224
225 enum DoubleForm {
226 DFExponent = 0,
227 DFDecimal,
228 DFSignificantDigits,
229 _DFMax = DFSignificantDigits
230 };
231
232 enum Flags {
233 NoFlags = 0,
234 AddTrailingZeroes = 0x01,
235 ZeroPadded = 0x02,
236 LeftAdjusted = 0x04,
237 BlankBeforePositive = 0x08,
238 AlwaysShowSign = 0x10,
239 GroupDigits = 0x20,
240 CapitalEorX = 0x40,
241
242 ShowBase = 0x80,
243 UppercaseBase = 0x100,
244 ZeroPadExponent = 0x200,
245 ForcePoint = 0x400
246 };
247
248 enum NumberMode { IntegerMode, DoubleStandardMode, DoubleScientificMode };
249
250private:
251 enum PrecisionMode {
252 PMDecimalDigits = 0x01,
253 PMSignificantDigits = 0x02,
254 PMChopTrailingZeros = 0x03
255 };
256
257 [[nodiscard]] QString decimalForm(QString &&digits, int decpt, int precision,
258 PrecisionMode pm, bool mustMarkDecimal,
259 bool groupDigits) const;
260 [[nodiscard]] QString exponentForm(QString &&digits, int decpt, int precision,
261 PrecisionMode pm, bool mustMarkDecimal,
262 int minExponentDigits) const;
263 [[nodiscard]] QString signPrefix(bool negative, unsigned flags) const;
264 [[nodiscard]] QString applyIntegerFormatting(QString &&numStr, bool negative, int precision,
265 int base, int width, unsigned flags) const;
266
267public:
268 [[nodiscard]] QString doubleToString(double d,
269 int precision = -1,
270 DoubleForm form = DFSignificantDigits,
271 int width = -1,
272 unsigned flags = NoFlags) const;
273 [[nodiscard]] QString longLongToString(qint64 l, int precision = -1,
274 int base = 10,
275 int width = -1,
276 unsigned flags = NoFlags) const;
277 [[nodiscard]] QString unsLongLongToString(quint64 l, int precision = -1,
278 int base = 10,
279 int width = -1,
280 unsigned flags = NoFlags) const;
281
282 // this function is meant to be called with the result of stringToDouble or bytearrayToDouble
283 [[nodiscard]] static float convertDoubleToFloat(double d, bool *ok)
284 {
285 if (qIsInf(d))
286 return float(d);
287 if (std::fabs(x: d) > (std::numeric_limits<float>::max)()) {
288 if (ok)
289 *ok = false;
290 const float huge = std::numeric_limits<float>::infinity();
291 return d < 0 ? -huge : huge;
292 }
293 if (d != 0 && float(d) == 0) {
294 // Values that underflow double already failed. Match them:
295 if (ok)
296 *ok = false;
297 return 0;
298 }
299 return float(d);
300 }
301
302 [[nodiscard]] double stringToDouble(QStringView str, bool *ok,
303 QLocale::NumberOptions options) const;
304 [[nodiscard]] qint64 stringToLongLong(QStringView str, int base, bool *ok,
305 QLocale::NumberOptions options) const;
306 [[nodiscard]] quint64 stringToUnsLongLong(QStringView str, int base, bool *ok,
307 QLocale::NumberOptions options) const;
308
309 // this function is used in QIntValidator (QtGui)
310 [[nodiscard]] Q_CORE_EXPORT static qint64 bytearrayToLongLong(QByteArrayView num, int base,
311 bool *ok);
312 [[nodiscard]] static quint64 bytearrayToUnsLongLong(QByteArrayView num, int base, bool *ok);
313
314 [[nodiscard]] bool numberToCLocale(QStringView s, QLocale::NumberOptions number_options,
315 NumberMode mode, CharBuff *result) const;
316
317 struct NumericData
318 {
319#ifndef QT_NO_SYSTEMLOCALE
320 // Only used for the system locale, to store data for the view to look at:
321 QString sysDecimal, sysGroup, sysMinus, sysPlus;
322#endif
323 QStringView decimal, group, minus, plus, exponent;
324 char32_t zeroUcs = 0;
325 qint8 zeroLen = 0;
326 bool isC = false; // C locale sets this and nothing else.
327 bool exponentCyrillic = false; // True only for floating-point parsing of Cyrillic.
328 void setZero(QStringView zero)
329 {
330 // No known locale has digits that are more than one Unicode
331 // code-point, so we can safely deal with digits as plain char32_t.
332 switch (zero.size()) {
333 case 1:
334 Q_ASSERT(!zero.at(0).isSurrogate());
335 zeroUcs = zero.at(n: 0).unicode();
336 zeroLen = 1;
337 break;
338 case 2:
339 Q_ASSERT(zero.at(0).isHighSurrogate());
340 zeroUcs = QChar::surrogateToUcs4(high: zero.at(n: 0), low: zero.at(n: 1));
341 zeroLen = 2;
342 break;
343 default:
344 Q_ASSERT(zero.size() == 0); // i.e. we got no value to use
345 break;
346 }
347 }
348 [[nodiscard]] bool isValid(NumberMode mode) const // Asserted as a sanity check.
349 {
350 if (isC)
351 return true;
352 if (exponentCyrillic && exponent != u"E" && exponent != u"\u0415")
353 return false;
354 return (zeroLen == 1 || zeroLen == 2) && zeroUcs > 0
355 && (mode == IntegerMode || !decimal.isEmpty())
356 // group may be empty (user config in system locale)
357 && !minus.isEmpty() && !plus.isEmpty()
358 && (mode != DoubleScientificMode || !exponent.isEmpty());
359 }
360 };
361 [[nodiscard]] inline NumericData numericData(NumberMode mode) const;
362
363 // this function is used in QIntValidator (QtGui)
364 [[nodiscard]] Q_CORE_EXPORT bool validateChars(
365 QStringView str, NumberMode numMode, QByteArray *buff, int decDigits = -1,
366 QLocale::NumberOptions number_options = QLocale::DefaultNumberOptions) const;
367
368 // Access to assorted data members:
369 [[nodiscard]] QLocaleId id() const
370 { return QLocaleId { .language_id: m_language_id, .script_id: m_script_id, .territory_id: m_territory_id }; }
371
372 [[nodiscard]] QString decimalPoint() const;
373 [[nodiscard]] QString groupSeparator() const;
374 [[nodiscard]] QString listSeparator() const;
375 [[nodiscard]] QString percentSign() const;
376 [[nodiscard]] QString zeroDigit() const;
377 [[nodiscard]] char32_t zeroUcs() const;
378 [[nodiscard]] QString positiveSign() const;
379 [[nodiscard]] QString negativeSign() const;
380 [[nodiscard]] QString exponentSeparator() const;
381
382 struct DataRange
383 {
384 quint16 offset;
385 quint16 size;
386 [[nodiscard]] QString getData(const char16_t *table) const
387 {
388 return size > 0
389 ? QString::fromRawData(reinterpret_cast<const QChar *>(table + offset), size)
390 : QString();
391 }
392 [[nodiscard]] QStringView viewData(const char16_t *table) const
393 {
394 return { reinterpret_cast<const QChar *>(table + offset), size };
395 }
396 [[nodiscard]] QString getListEntry(const char16_t *table, qsizetype index) const
397 {
398 return listEntry(table, index).getData(table);
399 }
400 [[nodiscard]] QStringView viewListEntry(const char16_t *table, qsizetype index) const
401 {
402 return listEntry(table, index).viewData(table);
403 }
404 [[nodiscard]] char32_t ucsFirst(const char16_t *table) const
405 {
406 if (size && !QChar::isSurrogate(ucs4: table[offset]))
407 return table[offset];
408 if (size > 1 && QChar::isHighSurrogate(ucs4: table[offset]))
409 return QChar::surrogateToUcs4(high: table[offset], low: table[offset + 1]);
410 return 0;
411 }
412 private:
413 [[nodiscard]] DataRange listEntry(const char16_t *table, qsizetype index) const
414 {
415 const char16_t separator = ';';
416 quint16 i = 0;
417 while (index > 0 && i < size) {
418 if (table[offset + i] == separator)
419 index--;
420 i++;
421 }
422 quint16 end = i;
423 while (end < size && table[offset + end] != separator)
424 end++;
425 return { .offset: quint16(offset + i), .size: quint16(end - i) };
426 }
427 };
428
429#define ForEachQLocaleRange(X) \
430 X(startListPattern) X(midListPattern) X(endListPattern) X(pairListPattern) X(listDelimit) \
431 X(decimalSeparator) X(groupDelim) X(percent) X(zero) X(minus) X(plus) X(exponential) \
432 X(quoteStart) X(quoteEnd) X(quoteStartAlternate) X(quoteEndAlternate) \
433 X(longDateFormat) X(shortDateFormat) X(longTimeFormat) X(shortTimeFormat) \
434 X(longDayNamesStandalone) X(longDayNames) \
435 X(shortDayNamesStandalone) X(shortDayNames) \
436 X(narrowDayNamesStandalone) X(narrowDayNames) \
437 X(anteMeridiem) X(postMeridiem) \
438 X(byteCount) X(byteAmountSI) X(byteAmountIEC) \
439 X(currencySymbol) X(currencyDisplayName) \
440 X(currencyFormat) X(currencyFormatNegative) \
441 X(endonymLanguage) X(endonymTerritory)
442
443#define rangeGetter(name) \
444 [[nodiscard]] DataRange name() const { return { m_ ## name ## _idx, m_ ## name ## _size }; }
445 ForEachQLocaleRange(rangeGetter)
446#undef rangeGetter
447
448public:
449 quint16 m_language_id, m_script_id, m_territory_id;
450
451 // Offsets, then sizes, for each range:
452#define rangeIndex(name) quint16 m_ ## name ## _idx;
453 ForEachQLocaleRange(rangeIndex)
454#undef rangeIndex
455#define Size(name) quint8 m_ ## name ## _size;
456 ForEachQLocaleRange(Size)
457#undef Size
458
459#undef ForEachQLocaleRange
460
461 // Strays:
462 char m_currency_iso_code[3];
463 quint8 m_currency_digits : 2;
464 quint8 m_currency_rounding : 3; // (not yet used !)
465 quint8 m_first_day_of_week : 3;
466 quint8 m_weekend_start : 3;
467 quint8 m_weekend_end : 3;
468 quint8 m_grouping_top : 2; // Don't group until more significant group has this many digits.
469 quint8 m_grouping_higher : 3; // Number of digits between grouping separators
470 quint8 m_grouping_least : 3; // Number of digits after last grouping separator (before decimal).
471};
472
473class QLocalePrivate
474{
475public:
476 constexpr QLocalePrivate(const QLocaleData *data, qsizetype index,
477 QLocale::NumberOptions numberOptions = QLocale::DefaultNumberOptions,
478 int refs = 0)
479 : m_data(data), ref Q_BASIC_ATOMIC_INITIALIZER(refs),
480 m_index(index), m_numberOptions(numberOptions) {}
481
482 [[nodiscard]] quint16 languageId() const { return m_data->m_language_id; }
483 [[nodiscard]] quint16 territoryId() const { return m_data->m_territory_id; }
484
485 [[nodiscard]] QByteArray bcp47Name(char separator = '-') const;
486
487 [[nodiscard]] inline std::array<char, 4>
488 languageCode(QLocale::LanguageCodeTypes codeTypes = QLocale::AnyLanguageCode) const
489 {
490 return languageToCode(language: QLocale::Language(m_data->m_language_id), codeTypes);
491 }
492 [[nodiscard]] inline QLatin1StringView scriptCode() const
493 { return scriptToCode(script: QLocale::Script(m_data->m_script_id)); }
494 [[nodiscard]] inline QLatin1StringView territoryCode() const
495 { return territoryToCode(territory: QLocale::Territory(m_data->m_territory_id)); }
496
497 [[nodiscard]] static const QLocalePrivate *get(const QLocale &l) { return l.d; }
498 [[nodiscard]] static std::array<char, 4>
499 languageToCode(QLocale::Language language,
500 QLocale::LanguageCodeTypes codeTypes = QLocale::AnyLanguageCode);
501 [[nodiscard]] static QLatin1StringView scriptToCode(QLocale::Script script);
502 [[nodiscard]] static QLatin1StringView territoryToCode(QLocale::Territory territory);
503 [[nodiscard]] static QLocale::Language
504 codeToLanguage(QStringView code,
505 QLocale::LanguageCodeTypes codeTypes = QLocale::AnyLanguageCode) noexcept;
506 [[nodiscard]] static QLocale::Script codeToScript(QStringView code) noexcept;
507 [[nodiscard]] static QLocale::Territory codeToTerritory(QStringView code) noexcept;
508
509 [[nodiscard]] QLocale::MeasurementSystem measurementSystem() const;
510
511 // System locale has an m_data all its own; all others have m_data = locale_data + m_index
512 const QLocaleData *const m_data;
513 QBasicAtomicInt ref;
514 const qsizetype m_index;
515 QLocale::NumberOptions m_numberOptions;
516
517 static QBasicAtomicInt s_generation;
518};
519
520#ifndef QT_NO_SYSTEMLOCALE
521qsizetype QSystemLocale::fallbackLocaleIndex() const { return fallbackLocale().d->m_index; }
522#endif
523
524template <>
525inline QLocalePrivate *QSharedDataPointer<QLocalePrivate>::clone()
526{
527 // cannot use QLocalePrivate's copy constructor
528 // since it is deleted in C++11
529 return new QLocalePrivate(d->m_data, d->m_index, d->m_numberOptions);
530}
531
532// Also used to merely skip over an escape in a format string, advancint idx to
533// point after it (so not [[nodiscard]]):
534QString qt_readEscapedFormatString(QStringView format, qsizetype *idx);
535[[nodiscard]] bool qt_splitLocaleName(QStringView name, QStringView *lang = nullptr,
536 QStringView *script = nullptr, QStringView *cntry = nullptr);
537[[nodiscard]] qsizetype qt_repeatCount(QStringView s);
538
539[[nodiscard]] constexpr inline bool ascii_isspace(uchar c)
540{
541 constexpr auto matcher = QtPrivate::makeCharacterSetMatch<QtPrivate::ascii_space_chars>();
542 return matcher.matches(c);
543}
544
545QT_END_NAMESPACE
546
547// ### move to qnamespace.h
548QT_DECL_METATYPE_EXTERN_TAGGED(QList<Qt::DayOfWeek>, QList_Qt__DayOfWeek, Q_CORE_EXPORT)
549#ifndef QT_NO_SYSTEMLOCALE
550QT_DECL_METATYPE_EXTERN_TAGGED(QSystemLocale::CurrencyToStringArgument,
551 QSystemLocale__CurrencyToStringArgument, Q_CORE_EXPORT)
552#endif
553
554#endif // QLOCALE_P_H
555

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