QCompleter::splitPath(): don't get tangled up in strings

- Don't store "\\" as a QString just for passing it to QString:.startsWith()
  and prepend(). Keep it a QLatin1String.
  -> one allocation less

- Don't use said QString as a flag, use a bool

- Don't store QDir::separator() in a QString
  -> one allocation less
  -> two indexing operations less

- Don't retrieve QDir::separator() from said QString as QString(sep[0])
  -> one more allocation less

- Don't look for a QChar in a string by using QRegExp; use the QChar
  overload instead
  -> one more allocation (at _least_) less

- Don't convert QDir::separator() with QDir::fromNativeSeparators(); it will
  _always_ be '/'...
  -> one expensive function call less
  -> two QString allocations less
  -> one QString(QChar) introduced
     -> one allocation more
        -> could be removed with QStringLiteral/QString::fromLatin1Char()

Change-Id: I802e66685a95b08cfc557defc63e5f16a7e6306b
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Marc Mutz 2015-10-12 13:38:32 +02:00
parent 2d2cb6434f
commit 6951f0e4af

View File

@ -1820,26 +1820,23 @@ QStringList QCompleter::splitPath(const QString& path) const
return QStringList(completionPrefix());
QString pathCopy = QDir::toNativeSeparators(path);
QString sep = QDir::separator();
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
if (pathCopy == QLatin1String("\\") || pathCopy == QLatin1String("\\\\"))
return QStringList(pathCopy);
QString doubleSlash(QLatin1String("\\\\"));
if (pathCopy.startsWith(doubleSlash))
const bool startsWithDoubleSlash = pathCopy.startsWith(QLatin1String("\\\\"));
if (startsWithDoubleSlash)
pathCopy = pathCopy.mid(2);
else
doubleSlash.clear();
#endif
QRegExp re(QLatin1Char('[') + QRegExp::escape(sep) + QLatin1Char(']'));
QStringList parts = pathCopy.split(re);
const QChar sep = QDir::separator();
QStringList parts = pathCopy.split(sep);
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
if (!doubleSlash.isEmpty())
parts[0].prepend(doubleSlash);
if (startsWithDoubleSlash)
parts[0].prepend(QLatin1String("\\\\"));
#else
if (pathCopy[0] == sep[0]) // readd the "/" at the beginning as the split removed it
parts[0] = QDir::fromNativeSeparators(QString(sep[0]));
if (pathCopy[0] == sep) // readd the "/" at the beginning as the split removed it
parts[0] = QLatin1Char('/');
#endif
return parts;