QCollator: don't pipe bcp47Name() through QString

Give QCollator access to QLocalePrivate::bcp47Name(), to avoid
both the latin-1 -> UTF-16 conversion in QLocale::bcp47Name(),
as well all as

- the replace('-', '_').toLatin1() call in ICU
- the toLocal8Bit() call in macOS
- the toUtf8() call in Windows

implementations of QCollatorPrivate::init().

This is safe, since, according to https://tools.ietf.org/html/bcp47,
a BCP47 name only contains US-ASCII (ALPHA used, which is defined by
https://tools.ietf.org/html/rfc5234 to be [a-zA-Z] only).

Change-Id: Id56befb1b5a7983494d848cdabf7ebeda377cf9f
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Marc Mutz 2017-02-07 10:19:37 +01:00
parent 9aaaa8427d
commit 0c50edbe84
4 changed files with 11 additions and 3 deletions

View File

@ -39,6 +39,7 @@
****************************************************************************/
#include "qcollator_p.h"
#include "qlocale_p.h"
#include "qstringlist.h"
#include "qstring.h"
@ -56,7 +57,7 @@ void QCollatorPrivate::init()
cleanup();
UErrorCode status = U_ZERO_ERROR;
QByteArray name = locale.bcp47Name().replace(QLatin1Char('-'), QLatin1Char('_')).toLatin1();
QByteArray name = QLocalePrivate::get(locale)->bcp47Name('_');
collator = ucol_open(name.constData(), &status);
if (U_FAILURE(status)) {
qWarning("Could not create collator: %d", status);

View File

@ -38,9 +38,12 @@
****************************************************************************/
#include "qcollator_p.h"
#include "qlocale_p.h"
#include "qstringlist.h"
#include "qstring.h"
#include <QtCore/private/qcore_mac_p.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFLocale.h>
@ -53,7 +56,7 @@ void QCollatorPrivate::init()
{
cleanup();
LocaleRef localeRef;
int rc = LocaleRefFromLocaleString(locale.bcp47Name().toLocal8Bit(), &localeRef);
int rc = LocaleRefFromLocaleString(QLocalePrivate::get(locale)->bcp47Name().constData(), &localeRef);
if (rc != 0)
qWarning("couldn't initialize the locale");

View File

@ -38,6 +38,7 @@
****************************************************************************/
#include "qcollator_p.h"
#include "qlocale_p.h"
#include "qstringlist.h"
#include "qstring.h"
@ -61,7 +62,7 @@ void QCollatorPrivate::init()
collator = 0;
#ifndef USE_COMPARESTRINGEX
localeID = qt_inIsoNametoLCID(locale.bcp47Name().toUtf8().constData());
localeID = qt_inIsoNametoLCID(QLocalePrivate::get(locale)->bcp47Name().constData());
#else
localeName = locale.bcp47Name();
#endif

View File

@ -332,6 +332,9 @@ public:
return retval;
}
static QLocalePrivate *get(QLocale &l) { return l.d; }
static const QLocalePrivate *get(const QLocale &l) { return l.d; }
QChar decimal() const { return QChar(m_data->m_decimal); }
QChar group() const { return QChar(m_data->m_group); }
QChar list() const { return QChar(m_data->m_list); }