From 3f85834a4751ef1063437aeebda87f0016f0d0cb Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 9 Jul 2024 11:57:34 +0200 Subject: [PATCH] Tidy up fix for calendar/locale data lookup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Save duplication of fetching and sanity-checking the locale-dependent calendar index data by packaging it as a lookup function. This can then take care of making the assertion of consistency with the matching QLocaleData entry and also lets me add, without duplicating, the slightly messy assert that m_data is what we expect (and are relying on) it to be. Task-number: QTBUG-126390 Change-Id: Ib514bee669956f432c007c858d01a9f7b6bbf86e Reviewed-by: Tor Arne Vestbø (cherry picked from commit c9c498b5d1f5797c714fb07f06a84df881edb928) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/text/qlocale.cpp | 39 +++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp index 25461601fa1..75bbe4c7722 100644 --- a/src/corelib/text/qlocale.cpp +++ b/src/corelib/text/qlocale.cpp @@ -3018,15 +3018,28 @@ QString QLocale::standaloneDayName(int day, FormatType type) const // Calendar look-up of month and day names: -// Only used in assertions -[[maybe_unused]] static bool sameLocale(const QLocaleData *locale, const QCalendarLocale &calendar) +// Get locale-specific month name data: +static const QCalendarLocale &getMonthDataFor(const QLocalePrivate *loc, + const QCalendarLocale *table) { - // NB: pass locale_data[] entry at the same index as the calendar one; this - // shall usually be the locale's m_data, but for the system locale it's - // different. - return locale->m_language_id == calendar.m_language_id - && locale->m_script_id == calendar.m_script_id - && locale->m_territory_id == calendar.m_territory_id; + // Only used in assertions + [[maybe_unused]] const auto sameLocale = [](const QLocaleData &locale, + const QCalendarLocale &cal) { + return locale.m_language_id == cal.m_language_id + && locale.m_script_id == cal.m_script_id + && locale.m_territory_id == cal.m_territory_id; + }; + const QCalendarLocale &monthly = table[loc->m_index]; +#ifdef QT_NO_SYSTEMLOCALE + [[maybe_unused]] constexpr bool isSys = false; +#else // Can't have preprocessor directives in a macro's parameter list, so use local. + [[maybe_unused]] const bool isSys = loc->m_data == &systemLocaleData; +#endif + Q_ASSERT(loc->m_data == &locale_data[loc->m_index] || isSys); + // Compare monthly to locale_data[] entry, as the m_index used with + // systemLocaleData is a best fit, not necessarily an exact match. + Q_ASSERT(sameLocale(locale_data[loc->m_index], monthly)); + return monthly; } /*! @@ -3137,9 +3150,8 @@ QString QCalendarBackend::monthName(const QLocale &locale, int month, int, QLocale::FormatType format) const { Q_ASSERT(month >= 1 && month <= maximumMonthsInYear()); - const QCalendarLocale &monthly = localeMonthIndexData()[locale.d->m_index]; - Q_ASSERT(sameLocale(&locale_data[locale.d->m_index], monthly)); - return rawMonthName(monthly, localeMonthData(), month, format); + return rawMonthName(getMonthDataFor(locale.d, localeMonthIndexData()), + localeMonthData(), month, format); } QString QRomanCalendar::monthName(const QLocale &locale, int month, int year, @@ -3173,9 +3185,8 @@ QString QCalendarBackend::standaloneMonthName(const QLocale &locale, int month, QLocale::FormatType format) const { Q_ASSERT(month >= 1 && month <= maximumMonthsInYear()); - const QCalendarLocale &monthly = localeMonthIndexData()[locale.d->m_index]; - Q_ASSERT(sameLocale(&locale_data[locale.d->m_index], monthly)); - return rawStandaloneMonthName(monthly, localeMonthData(), month, format); + return rawStandaloneMonthName(getMonthDataFor(locale.d, localeMonthIndexData()), + localeMonthData(), month, format); } QString QRomanCalendar::standaloneMonthName(const QLocale &locale, int month, int year,