summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoão Abecasis <joao.abecasis@nokia.com>2011-08-05 08:40:46 (GMT)
committerJoão Abecasis <joao.abecasis@nokia.com>2011-08-11 16:58:10 (GMT)
commit773a6df46243831dee7559f90e33d7eff3c5c71e (patch)
tree327bea4f1229adad5b8516f648d38553a23f90cf
parent023976f9dd48a3deb947905d32d5fc0692da7318 (diff)
downloadQt-773a6df46243831dee7559f90e33d7eff3c5c71e.zip
Qt-773a6df46243831dee7559f90e33d7eff3c5c71e.tar.gz
Qt-773a6df46243831dee7559f90e33d7eff3c5c71e.tar.bz2
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
-rw-r--r--src/corelib/io/qdir.cpp38
1 files changed, 28 insertions, 10 deletions
diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp
index b31cf69..f9196e0 100644
--- a/src/corelib/io/qdir.cpp
+++ b/src/corelib/io/qdir.cpp
@@ -802,14 +802,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;
}
/*!
@@ -826,14 +835,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;
}
/*!