Provide macOS native toUpper() and toLower()

These functions are useful when ICU is not available, in the CI
for example. It helps the CI pass the locale-related string
conversion tests in qtdeclarative.

Task-number: QTBUG-112898
Change-Id: I92cfda756b419cbcc438798b48824ef6c50c5d62
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Johnny Jazeix 2024-11-01 17:07:46 +01:00
parent fe76b9e479
commit cabdea9665
4 changed files with 40 additions and 6 deletions

View File

@ -3563,7 +3563,7 @@ Qt::LayoutDirection QLocale::textDirection() const
*/
QString QLocale::toUpper(const QString &str) const
{
#if !defined(QT_BOOTSTRAPPED) && (QT_CONFIG(icu) || defined(Q_OS_WIN))
#if !defined(QT_BOOTSTRAPPED) && (QT_CONFIG(icu) || defined(Q_OS_WIN) || defined(Q_OS_APPLE))
bool ok = true;
QString result = d->toUpper(str, &ok);
if (ok)
@ -3587,7 +3587,7 @@ QString QLocale::toUpper(const QString &str) const
*/
QString QLocale::toLower(const QString &str) const
{
#if !defined(QT_BOOTSTRAPPED) && (QT_CONFIG(icu) || defined(Q_OS_WIN))
#if !defined(QT_BOOTSTRAPPED) && (QT_CONFIG(icu) || defined(Q_OS_WIN) || defined(Q_OS_APPLE))
bool ok = true;
const QString result = d->toLower(str, &ok);
if (ok)

View File

@ -697,4 +697,40 @@ QVariant QSystemLocale::query(QueryType type, QVariant &&in) const
#endif // QT_NO_SYSTEMLOCALE
#if !QT_CONFIG(icu)
static QString localeConvertString(const QByteArray &localeID, const QString &str, bool *ok,
bool toLowerCase)
{
QMacAutoReleasePool pool;
Q_ASSERT(ok);
NSString *localestring = [[NSString alloc] initWithData:localeID.toNSData()
encoding:NSUTF8StringEncoding];
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:localestring];
if (!locale) {
*ok = false;
return QString();
}
*ok = true;
NSString *nsstring = str.toNSString();
if (toLowerCase)
nsstring = [nsstring lowercaseStringWithLocale:locale];
else
nsstring = [nsstring uppercaseStringWithLocale:locale];
return QString::fromNSString(nsstring);
}
QString QLocalePrivate::toLower(const QString &str, bool *ok) const
{
return localeConvertString(bcp47Name('-'), str, ok, true);
}
QString QLocalePrivate::toUpper(const QString &str, bool *ok) const
{
return localeConvertString(bcp47Name('-'), str, ok, false);
}
#endif
QT_END_NAMESPACE

View File

@ -552,10 +552,8 @@ public:
[[nodiscard]] QLocale::MeasurementSystem measurementSystem() const;
#if QT_CONFIG(icu) || (defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED))
[[nodiscard]] QString toUpper(const QString &str, bool *ok) const;
[[nodiscard]] QString toLower(const QString &str, bool *ok) const;
#endif
// System locale has an m_data all its own; all others have m_data = locale_data + m_index
const QLocaleData *const m_data;

View File

@ -161,7 +161,7 @@ private slots:
void lcsToCode();
void codeToLcs();
#if QT_CONFIG(icu) || defined(Q_OS_WIN)
#if QT_CONFIG(icu) || defined(Q_OS_WIN) || defined(Q_OS_APPLE)
void toLowerUpper_data();
void toLowerUpper();
@ -4777,7 +4777,7 @@ void tst_QLocale::codeToLcs()
QCOMPARE(QLocale::codeToScript(QString("Hans")), QLocale::SimplifiedHanScript);
}
#if QT_CONFIG(icu) || defined(Q_OS_WIN)
#if QT_CONFIG(icu) || defined(Q_OS_WIN) || defined(Q_OS_APPLE)
void tst_QLocale::toLowerUpper_data()
{
QTest::addColumn<QLocale>("locale");