QLocaleId: make most of the methods noexcept

All inline methods simply operate on ushort members, so they can be
noexcept.
The withLikelySubtagsAdded() method operates on static constexpr
array of locales. It does not allocate and cannot throw. The asserts
are there to check the internal consistency. So, it can also be made
noexcept.
Consequently, withLikelySubtagsRemoved() can be made noexcept, because
it only calls withLikelySubtagsAdded(), and does some trivial checks.

The only two methods that cannot be made noexept are:
* QLocaleId::name(), which obviously allocates a QByteArray
* QLocaleId::fromName(), because it end up calling a static
  isScript() helper, which allocates a QString.

The isScript() helper possibly can be rewritten to avoid allocations,
but that's an exercise for another patch.

Resulted from 6.8 API review, so picking to 6.8.

Change-Id: I9e98df31f9c13171bc005250024aa4ffb0faedb8
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
(cherry picked from commit 2485aae3ff3fa56833009f4766e53068667a7435)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ivan Solovev 2024-08-29 11:35:54 +02:00 committed by Qt Cherry-pick Bot
parent 046cc59aee
commit 759065adff
2 changed files with 10 additions and 10 deletions

View File

@ -295,7 +295,7 @@ bool operator<(LikelyPair lhs, LikelyPair rhs)
in the spec, but the examples clearly presume them and CLDR does provide
such likely matches.
*/
QLocaleId QLocaleId::withLikelySubtagsAdded() const
QLocaleId QLocaleId::withLikelySubtagsAdded() const noexcept
{
/* Each pattern that appears in a comments below, language_script_region and
similar, indicates which of this's fields (even if blank) are being
@ -379,7 +379,7 @@ QLocaleId QLocaleId::withLikelySubtagsAdded() const
return *this;
}
QLocaleId QLocaleId::withLikelySubtagsRemoved() const
QLocaleId QLocaleId::withLikelySubtagsRemoved() const noexcept
{
QLocaleId max = withLikelySubtagsAdded();
// language

View File

@ -189,34 +189,34 @@ namespace QIcu {
struct QLocaleId
{
[[nodiscard]] Q_AUTOTEST_EXPORT static QLocaleId fromName(QStringView name);
[[nodiscard]] inline bool operator==(QLocaleId other) const
[[nodiscard]] inline bool operator==(QLocaleId other) const noexcept
{ return language_id == other.language_id && script_id == other.script_id && territory_id == other.territory_id; }
[[nodiscard]] inline bool operator!=(QLocaleId other) const
[[nodiscard]] inline bool operator!=(QLocaleId other) const noexcept
{ return !operator==(other); }
[[nodiscard]] inline bool isValid() const
[[nodiscard]] inline bool isValid() const noexcept
{
return language_id <= QLocale::LastLanguage && script_id <= QLocale::LastScript
&& territory_id <= QLocale::LastTerritory;
}
[[nodiscard]] inline bool matchesAll() const
[[nodiscard]] inline bool matchesAll() const noexcept
{
return !language_id && !script_id && !territory_id;
}
// Use as: filter.accept...(candidate)
[[nodiscard]] inline bool acceptLanguage(quint16 lang) const
[[nodiscard]] inline bool acceptLanguage(quint16 lang) const noexcept
{
// Always reject AnyLanguage (only used for last entry in locale_data array).
// So, when searching for AnyLanguage, accept everything *but* AnyLanguage.
return language_id ? lang == language_id : lang;
}
[[nodiscard]] inline bool acceptScriptTerritory(QLocaleId other) const
[[nodiscard]] inline bool acceptScriptTerritory(QLocaleId other) const noexcept
{
return (!territory_id || other.territory_id == territory_id)
&& (!script_id || other.script_id == script_id);
}
[[nodiscard]] QLocaleId withLikelySubtagsAdded() const;
[[nodiscard]] QLocaleId withLikelySubtagsRemoved() const;
[[nodiscard]] QLocaleId withLikelySubtagsAdded() const noexcept;
[[nodiscard]] QLocaleId withLikelySubtagsRemoved() const noexcept;
[[nodiscard]] QByteArray name(char separator = '-') const;