From 759065adff7765f7e8990001d1e7bf7e3c0bd942 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Thu, 29 Aug 2024 11:35:54 +0200 Subject: [PATCH] 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 Reviewed-by: Edward Welbourne (cherry picked from commit 2485aae3ff3fa56833009f4766e53068667a7435) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/text/qlocale.cpp | 4 ++-- src/corelib/text/qlocale_p.h | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp index 33d4ccb1df6..f72991c764d 100644 --- a/src/corelib/text/qlocale.cpp +++ b/src/corelib/text/qlocale.cpp @@ -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 diff --git a/src/corelib/text/qlocale_p.h b/src/corelib/text/qlocale_p.h index 3044d137b9b..f911551eebd 100644 --- a/src/corelib/text/qlocale_p.h +++ b/src/corelib/text/qlocale_p.h @@ -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;