diff options
author | Marc Mutz <marc.mutz@qt.io> | 2022-02-16 07:00:05 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2022-02-18 18:50:49 +0100 |
commit | 5f338040f4ecbf6404ea5610ec19adfd9ca71c35 (patch) | |
tree | 9fa530dc415514e725d02592c39c3ceb30c5aae7 /src/gui/painting | |
parent | 795c94658d88d1139d922173aa26beace4ae9fae (diff) |
QColor: avoid Unicode table lookups in fromString()
All color names supported by QColor are US-ASCII.
Enforce this with a static_assert, then use this fact to perform the
case-folding of the input in US-ASCII instead of Unicode.
Avoids lookups in the Unicode tables.
Add QtMiscUtils::toAsciiLower() to foster sharing.
Change-Id: Ie0e123405d772943313dc4be1808667b152770b1
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/gui/painting')
-rw-r--r-- | src/gui/painting/qcolor.cpp | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/src/gui/painting/qcolor.cpp b/src/gui/painting/qcolor.cpp index ccce457671..9c32aff330 100644 --- a/src/gui/painting/qcolor.cpp +++ b/src/gui/painting/qcolor.cpp @@ -164,7 +164,7 @@ static bool get_hex_rgb(QAnyStringView name, QRgba64 *rgb) #define rgb(r,g,b) (0xff000000 | (r << 16) | (g << 8) | b) // keep this is in sync with QColorConstants -static const struct RGBData { +static constexpr struct RGBData { const char name[21]; uint value; } rgbTbl[] = { @@ -320,6 +320,16 @@ static const struct RGBData { static const int rgbTblSize = sizeof(rgbTbl) / sizeof(RGBData); +static_assert([] { + for (auto e : rgbTbl) { + for (auto it = e.name; *it ; ++it) { + if (uchar(*it) > 127) + return false; + } + } + return true; + }(), "the lookup code expects color names to be US-ASCII-only"); + #undef rgb inline bool operator<(const char *name, const RGBData &data) @@ -338,8 +348,9 @@ static bool get_named_rgb_no_space(const char *name_no_space, QRgb *rgb) } namespace { -static inline char toLower(QChar ch) noexcept { return ch.toLower().toLatin1(); } -static inline char toLower(char ch) noexcept { return toLower(QLatin1Char{ch}); } +// named colors are US-ASCII (enforced by static_assert above): +static char to_char(char ch) noexcept { return ch; } +static char to_char(QChar ch) noexcept { return ch.toLatin1(); } } static bool get_named_rgb(QAnyStringView name, QRgb* rgb) @@ -351,7 +362,7 @@ static bool get_named_rgb(QAnyStringView name, QRgb* rgb) name.visit([&pos, &name_no_space] (auto name) { for (auto c : name) { if (c != u'\t' && c != u' ') - name_no_space[pos++] = toLower(c); + name_no_space[pos++] = QtMiscUtils::toAsciiLower(to_char(c)); } }); name_no_space[pos] = 0; |