Avoid spurious detaching in QDir::to/fromNativeSeparators

The new code avoids non-const detaching operations until needed and uses
a pointer into the "raw" QChar data from then on, thus skipping unneeded
checks on the reference count for further detaching.

These functions are used all the time by the file system classes so this
small optimization won't hurt. In particular, it will help users who
already use '/' when passing paths into Qt.

Reviewed-by: Peter Hartmann
(cherry picked from commit 773a6df46243831dee7559f90e33d7eff3c5c71e)

Change-Id: I27787e787b544a63c9ea1e4138bd548500104dff
Reviewed-by: João Abecasis <joao.abecasis@nokia.com>
bb10
João Abecasis 2011-08-05 10:40:46 +02:00 committed by Qt by Nokia
parent 0e7cecb861
commit b0a6caf84b
1 changed files with 28 additions and 10 deletions

View File

@ -794,14 +794,23 @@ QString QDir::convertSeparators(const QString &pathName)
*/
QString QDir::toNativeSeparators(const QString &pathName)
{
QString n(pathName);
#if defined(Q_FS_FAT) || defined(Q_OS_OS2EMX) || defined(Q_OS_SYMBIAN)
for (int i = 0; i < (int)n.length(); ++i) {
if (n[i] == QLatin1Char('/'))
n[i] = QLatin1Char('\\');
int i = pathName.indexOf(QLatin1Char('/'));
if (i != -1) {
QString n(pathName);
QChar * const data = n.data();
data[i++] = QLatin1Char('\\');
for (; i < n.length(); ++i) {
if (data[i] == QLatin1Char('/'))
data[i] = QLatin1Char('\\');
}
return n;
}
#endif
return n;
return pathName;
}
/*!
@ -818,14 +827,23 @@ QString QDir::toNativeSeparators(const QString &pathName)
*/
QString QDir::fromNativeSeparators(const QString &pathName)
{
QString n(pathName);
#if defined(Q_FS_FAT) || defined(Q_OS_OS2EMX) || defined(Q_OS_SYMBIAN)
for (int i = 0; i < (int)n.length(); ++i) {
if (n[i] == QLatin1Char('\\'))
n[i] = QLatin1Char('/');
int i = pathName.indexOf(QLatin1Char('\\'));
if (i != -1) {
QString n(pathName);
QChar * const data = n.data();
data[i++] = QLatin1Char('/');
for (; i < n.length(); ++i) {
if (data[i] == QLatin1Char('\\'))
data[i] = QLatin1Char('/');
}
return n;
}
#endif
return n;
return pathName;
}
/*!