From 6030e63037cdc09d43ad372ecbcfeece72b6e353 Mon Sep 17 00:00:00 2001 From: Anton Kudryavtsev Date: Thu, 31 Mar 2016 17:34:32 +0300 Subject: QtGui: use QStringRef to optimize memory allocation Replace substring functions that return QString with corresponding functions that return QStringRef where it's possible. Create QString from QStringRef only where necessary. Change-Id: Id83c60cc7568b20ef008b51c582cfdf466a47e35 Reviewed-by: Edward Welbourne --- src/gui/image/qpnghandler.cpp | 2 +- src/gui/kernel/qkeysequence.cpp | 13 +++++++------ src/gui/text/qcssparser.cpp | 7 +++---- src/gui/text/qfont.cpp | 4 ++-- src/gui/text/qfontdatabase.cpp | 6 +++--- 5 files changed, 16 insertions(+), 16 deletions(-) (limited to 'src/gui') diff --git a/src/gui/image/qpnghandler.cpp b/src/gui/image/qpnghandler.cpp index c84f429e46..d021e6f09f 100644 --- a/src/gui/image/qpnghandler.cpp +++ b/src/gui/image/qpnghandler.cpp @@ -761,7 +761,7 @@ static void set_text(const QImage &image, png_structp png_ptr, png_infop info_pt QMap::ConstIterator it = text.constBegin(); int i = 0; while (it != text.constEnd()) { - text_ptr[i].key = qstrdup(it.key().left(79).toLatin1().constData()); + text_ptr[i].key = qstrdup(it.key().leftRef(79).toLatin1().constData()); bool noCompress = (it.value().length() < 40); #ifdef PNG_iTXt_SUPPORTED diff --git a/src/gui/kernel/qkeysequence.cpp b/src/gui/kernel/qkeysequence.cpp index f7771c252f..e7b1f9e073 100644 --- a/src/gui/kernel/qkeysequence.cpp +++ b/src/gui/kernel/qkeysequence.cpp @@ -1154,21 +1154,22 @@ int QKeySequencePrivate::decodeString(const QString &str, QKeySequence::Sequence } int p = accel.lastIndexOf(QLatin1Char('+'), str.length() - 2); // -2 so that Ctrl++ works + QStringRef accelRef(&accel); if(p > 0) - accel = accel.mid(p + 1); + accelRef = accelRef.mid(p + 1); int fnum = 0; - if (accel.length() == 1) { + if (accelRef.length() == 1) { #if defined(Q_OS_MACX) - int qtKey = qtkeyForMacSymbol(accel.at(0)); + int qtKey = qtkeyForMacSymbol(accelRef.at(0)); if (qtKey != -1) { ret |= qtKey; } else #endif { - ret |= accel.at(0).toUpper().unicode(); + ret |= accelRef.at(0).toUpper().unicode(); } - } else if (accel.at(0) == QLatin1Char('f') && (fnum = accel.mid(1).toInt()) >= 1 && fnum <= 35) { + } else if (accelRef.at(0) == QLatin1Char('f') && (fnum = accelRef.mid(1).toInt()) >= 1 && fnum <= 35) { ret |= Qt::Key_F1 + fnum - 1; } else { // For NativeText, check the traslation table first, @@ -1182,7 +1183,7 @@ int QKeySequencePrivate::decodeString(const QString &str, QKeySequence::Sequence QString keyName(tran == 0 ? QCoreApplication::translate("QShortcut", keyname[i].name) : QString::fromLatin1(keyname[i].name)); - if (accel == keyName.toLower()) { + if (accelRef == keyName.toLower()) { ret |= keyname[i].key; found = true; break; diff --git a/src/gui/text/qcssparser.cpp b/src/gui/text/qcssparser.cpp index d455a74d96..c237d80d34 100644 --- a/src/gui/text/qcssparser.cpp +++ b/src/gui/text/qcssparser.cpp @@ -1510,7 +1510,7 @@ QRect Declaration::rectValue() const const QStringList func = v.variant.toStringList(); if (func.count() != 2 || func.at(0).compare(QLatin1String("rect")) != 0) return QRect(); - QStringList args = func[1].split(QLatin1Char(' '), QString::SkipEmptyParts); + const auto args = func[1].splitRef(QLatin1Char(' '), QString::SkipEmptyParts); if (args.count() != 4) return QRect(); QRect rect(args[0].toInt(), args[1].toInt(), args[2].toInt(), args[3].toInt()); @@ -1915,9 +1915,8 @@ bool StyleSelector::basicSelectorMatches(const BasicSelector &sel, NodePtr node) return false; if (a.valueMatchCriterium == QCss::AttributeSelector::MatchContains) { - - QStringList lst = attrValue.split(QLatin1Char(' ')); - if (!lst.contains(a.value)) + const auto lst = attrValue.splitRef(QLatin1Char(' ')); + if (!lst.contains(QStringRef(&a.value))) return false; } else if ( (a.valueMatchCriterium == QCss::AttributeSelector::MatchEqual diff --git a/src/gui/text/qfont.cpp b/src/gui/text/qfont.cpp index f92e4ab457..828dbc318c 100644 --- a/src/gui/text/qfont.cpp +++ b/src/gui/text/qfont.cpp @@ -2033,7 +2033,7 @@ uint qHash(const QFont &font, uint seed) Q_DECL_NOTHROW */ bool QFont::fromString(const QString &descrip) { - QStringList l(descrip.split(QLatin1Char(','))); + const auto l = descrip.splitRef(QLatin1Char(',')); int count = l.count(); if (!count || (count > 2 && count < 9) || count > 11) { @@ -2042,7 +2042,7 @@ bool QFont::fromString(const QString &descrip) return false; } - setFamily(l[0]); + setFamily(l[0].toString()); if (count > 1 && l[1].toDouble() > 0.0) setPointSizeF(l[1].toDouble()); if (count == 9) { diff --git a/src/gui/text/qfontdatabase.cpp b/src/gui/text/qfontdatabase.cpp index 359c461ad3..9b50581bea 100644 --- a/src/gui/text/qfontdatabase.cpp +++ b/src/gui/text/qfontdatabase.cpp @@ -697,15 +697,15 @@ static QStringList familyList(const QFontDef &req) if (req.family.isEmpty()) return family_list; - QStringList list = req.family.split(QLatin1Char(',')); + const auto list = req.family.splitRef(QLatin1Char(',')); const int numFamilies = list.size(); family_list.reserve(numFamilies); for (int i = 0; i < numFamilies; ++i) { - QString str = list.at(i).trimmed(); + QStringRef str = list.at(i).trimmed(); if ((str.startsWith(QLatin1Char('"')) && str.endsWith(QLatin1Char('"'))) || (str.startsWith(QLatin1Char('\'')) && str.endsWith(QLatin1Char('\'')))) str = str.mid(1, str.length() - 2); - family_list << str; + family_list << str.toString(); } // append the substitute list for each family in family_list -- cgit v1.2.3