Use QStringRef to optimize memory allocation

Replace substring functions that return QString
with corresponding functions that return QStringRef.

Change-Id: I3c485f89352a1ee66076fba74fd486da9349c354
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Anton Kudryavtsev 2016-08-15 17:30:44 +03:00
parent 469b139169
commit 1075f6c764
5 changed files with 10 additions and 10 deletions

View File

@ -349,7 +349,7 @@ static QString readSymLink(const QFileSystemEntry &link)
if (matchVolName.indexIn(result) == 0) {
DWORD len;
wchar_t buffer[MAX_PATH];
QString volumeName = result.mid(0, matchVolName.matchedLength()).prepend(QLatin1String("\\\\?\\"));
const QString volumeName = QLatin1String("\\\\?\\") + result.leftRef(matchVolName.matchedLength());
if (GetVolumePathNamesForVolumeName(reinterpret_cast<LPCWSTR>(volumeName.utf16()), buffer, MAX_PATH, &len) != 0)
result.replace(0,matchVolName.matchedLength(), QString::fromWCharArray(buffer));
}

View File

@ -2107,7 +2107,7 @@ QString Scanner::preprocess(const QString &input, bool *hasEscapeSequences)
hexCount = qMin(hexCount, 6);
bool ok = false;
ushort code = output.mid(hexStart, hexCount).toUShort(&ok, 16);
ushort code = output.midRef(hexStart, hexCount).toUShort(&ok, 16);
if (ok) {
output.replace(hexStart - 1, hexCount + 1, QChar(code));
i = hexStart;

View File

@ -131,7 +131,7 @@ static bool isValidBlockSeparator(QChar ch)
|| ch == QTextEndOfFrame;
}
static bool noBlockInString(const QString &str)
static bool noBlockInString(const QStringRef &str)
{
return !str.contains(QChar::ParagraphSeparator)
&& !str.contains(QTextBeginningOfFrame)
@ -320,7 +320,7 @@ void QTextDocumentPrivate::setLayout(QAbstractTextDocumentLayout *layout)
void QTextDocumentPrivate::insert_string(int pos, uint strPos, uint length, int format, QTextUndoCommand::Operation op)
{
// ##### optimize when only appending to the fragment!
Q_ASSERT(noBlockInString(text.mid(strPos, length)));
Q_ASSERT(noBlockInString(text.midRef(strPos, length)));
split(pos);
uint x = fragments.insert_single(pos, length);
@ -476,7 +476,7 @@ void QTextDocumentPrivate::insert(int pos, const QString &str, int format)
if (str.size() == 0)
return;
Q_ASSERT(noBlockInString(str));
Q_ASSERT(noBlockInString(QStringRef(&str)));
int strPos = text.length();
text.append(str);
@ -494,7 +494,7 @@ int QTextDocumentPrivate::remove_string(int pos, uint length, QTextUndoCommand::
Q_ASSERT(blocks.size(b) > length);
Q_ASSERT(x && fragments.position(x) == (uint)pos && fragments.size(x) == length);
Q_ASSERT(noBlockInString(text.mid(fragments.fragment(x)->stringPosition, length)));
Q_ASSERT(noBlockInString(text.midRef(fragments.fragment(x)->stringPosition, length)));
blocks.setSize(b, blocks.size(b)-length);
@ -629,7 +629,7 @@ void QTextDocumentPrivate::move(int pos, int to, int length, QTextUndoCommand::O
if (key+1 != blocks.position(b)) {
// qDebug("remove_string from %d length %d", key, X->size_array[0]);
Q_ASSERT(noBlockInString(text.mid(X->stringPosition, X->size_array[0])));
Q_ASSERT(noBlockInString(text.midRef(X->stringPosition, X->size_array[0])));
w = remove_string(key, X->size_array[0], op);
if (needsInsert) {

View File

@ -2711,7 +2711,7 @@ static QString stringMidRetainingBidiCC(const QString &string,
suffix += c;
}
return prefix + ellidePrefix + string.mid(midStart, midLength) + ellideSuffix + suffix;
return prefix + ellidePrefix + string.midRef(midStart, midLength) + ellideSuffix + suffix;
}
QString QTextEngine::elidedText(Qt::TextElideMode mode, const QFixed &width, int flags, int from, int count) const
@ -2874,7 +2874,7 @@ QString QTextEngine::elidedText(Qt::TextElideMode mode, const QFixed &width, int
if (prevCharJoins(layoutData->string, rightPos))
ellipsisText.append(QChar(0x200d) /* ZWJ */);
return layoutData->string.mid(from, leftPos - from) + ellipsisText + layoutData->string.mid(rightPos, to - rightPos);
return layoutData->string.midRef(from, leftPos - from) + ellipsisText + layoutData->string.midRef(rightPos, to - rightPos);
}
return layoutData->string.mid(from, to - from);

View File

@ -2023,7 +2023,7 @@ void WriteInitialization::addInitializer(Item *item,
const QString &name, int column, const QString &value, const QString &directive, bool translatable) const
{
if (!value.isEmpty())
item->addSetter(QLatin1String("->set") + name.at(0).toUpper() + name.mid(1) +
item->addSetter(QLatin1String("->set") + name.at(0).toUpper() + name.midRef(1) +
QLatin1Char('(') + (column < 0 ? QString() : QString::number(column) +
QLatin1String(", ")) + value + QLatin1String(");"), directive, translatable);
}