diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2017-01-30 15:25:23 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2017-03-26 06:44:29 +0000 |
commit | 59928d23fc22d916dea32f02d810b8b24b74922d (patch) | |
tree | 5b1bc9c5e3cdb713e8cd716df8189bb3afd361c6 /src/corelib/tools/qlocale.cpp | |
parent | 610a9e8f319eafcbcf193e4b90119a6f89d0caf9 (diff) |
Port QLocalePrivate::*ToCode() to QLatin1String
The returned data is in US-ASCII (or else Latin-1), and resides in
consecutive memory. We can therefore return it in a QLatin1String,
which, however, will in general not be NUL-terminated.
Many users use the return value as part of a QStringBuilder
expression, and those which are not are not pessimized further by
this change.
The caller in qtimezoneprivate_icu looks as if it could simply zero
-terminate the return value and use it as-is, as opposed to
converting to UTF-8, but I left the code equivalent to the original
just the same.
Change-Id: I0e628af8c1320fcff8d0aacf160e859681d2b85a
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qlocale.cpp')
-rw-r--r-- | src/corelib/tools/qlocale.cpp | 31 |
1 files changed, 9 insertions, 22 deletions
diff --git a/src/corelib/tools/qlocale.cpp b/src/corelib/tools/qlocale.cpp index 59952bd107..35aa0df0ef 100644 --- a/src/corelib/tools/qlocale.cpp +++ b/src/corelib/tools/qlocale.cpp @@ -167,48 +167,35 @@ QLocale::Country QLocalePrivate::codeToCountry(const QChar *code, int len) Q_DEC return QLocale::AnyCountry; } -QString QLocalePrivate::languageToCode(QLocale::Language language) +QLatin1String QLocalePrivate::languageToCode(QLocale::Language language) { if (language == QLocale::AnyLanguage) - return QString(); + return QLatin1String(); if (language == QLocale::C) return QLatin1String("C"); const unsigned char *c = language_code_list + 3*(uint(language)); - QString code(c[2] == 0 ? 2 : 3, Qt::Uninitialized); - - code[0] = ushort(c[0]); - code[1] = ushort(c[1]); - if (c[2] != 0) - code[2] = ushort(c[2]); + return QLatin1String(reinterpret_cast<const char*>(c), c[2] == 0 ? 2 : 3); - return code; } -QString QLocalePrivate::scriptToCode(QLocale::Script script) +QLatin1String QLocalePrivate::scriptToCode(QLocale::Script script) { if (script == QLocale::AnyScript || script > QLocale::LastScript) - return QString(); + return QLatin1String(); const unsigned char *c = script_code_list + 4*(uint(script)); - return QString::fromLatin1((const char *)c, 4); + return QLatin1String(reinterpret_cast<const char *>(c), 4); } -QString QLocalePrivate::countryToCode(QLocale::Country country) +QLatin1String QLocalePrivate::countryToCode(QLocale::Country country) { if (country == QLocale::AnyCountry) - return QString(); + return QLatin1String(); const unsigned char *c = country_code_list + 3*(uint(country)); - QString code(c[2] == 0 ? 2 : 3, Qt::Uninitialized); - - code[0] = ushort(c[0]); - code[1] = ushort(c[1]); - if (c[2] != 0) - code[2] = ushort(c[2]); - - return code; + return QLatin1String(reinterpret_cast<const char*>(c), c[2] == 0 ? 2 : 3); } // http://www.unicode.org/reports/tr35/#Likely_Subtags |