Optimize QXcbKeyboard::lookupString.

For the common case of strings smaller than 32 chars this removes a
temporary QByteArray allocation and the second call to
xkb_state_key_get_utf8.

Change-Id: I81cbbf2df683476b38c2ffb96119293cd5b09a90
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Milian Wolff 2015-04-17 12:06:29 +02:00 committed by Marc Mutz
parent 0495970631
commit a91ac8d5a6

View File

@ -1515,10 +1515,12 @@ void QXcbKeyboard::handleKeyEvent(xcb_window_t sourceWindow, QEvent::Type type,
QString QXcbKeyboard::lookupString(struct xkb_state *state, xcb_keycode_t code) const
{
QByteArray chars;
chars.resize(1 + xkb_state_key_get_utf8(state, code, 0, 0));
// equivalent of XLookupString
QVarLengthArray<char, 32> chars(32);
const int size = xkb_state_key_get_utf8(state, code, chars.data(), chars.size());
if (Q_UNLIKELY(size + 1 > chars.size())) { // +1 for NUL
chars.resize(size + 1);
xkb_state_key_get_utf8(state, code, chars.data(), chars.size());
}
return QString::fromUtf8(chars.constData(), size);
}