summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorOswald Buddenhagen <oswald.buddenhagen@nokia.com>2009-08-18 14:43:34 (GMT)
committerOswald Buddenhagen <oswald.buddenhagen@nokia.com>2009-09-30 15:33:49 (GMT)
commit27b3784bf84ecd7c760b4d3ec47a657a1f3a0d40 (patch)
tree1883da85bb49c7ab1264553b7256db3df5497d21 /src
parent607df95ef6ca5c5a2632d3befbf604dfcfbed530 (diff)
downloadQt-27b3784bf84ecd7c760b4d3ec47a657a1f3a0d40.zip
Qt-27b3784bf84ecd7c760b4d3ec47a657a1f3a0d40.tar.gz
Qt-27b3784bf84ecd7c760b4d3ec47a657a1f3a0d40.tar.bz2
optimize read() and readAll()
factor out a dedicated private::read() function based on private::scan(). this avoids making the latter even more complex in the process of optimizing it. Reviewed-By: mariusSO
Diffstat (limited to 'src')
-rw-r--r--src/corelib/io/qtextstream.cpp44
1 files changed, 24 insertions, 20 deletions
diff --git a/src/corelib/io/qtextstream.cpp b/src/corelib/io/qtextstream.cpp
index 415ba60..594718e 100644
--- a/src/corelib/io/qtextstream.cpp
+++ b/src/corelib/io/qtextstream.cpp
@@ -241,6 +241,7 @@ static const int QTEXTSTREAM_BUFFERSIZE = 16384;
#include "private/qlocale_p.h"
#include <stdlib.h>
+#include <limits.h>
#include <new>
#if defined QTEXTSTREAM_DEBUG
@@ -375,10 +376,10 @@ public:
enum TokenDelimiter {
Space,
NotSpace,
- EndOfLine,
- EndOfFile
+ EndOfLine
};
+ QString read(int maxlen);
bool scan(const QChar **ptr, int *tokenLength,
int maxlen, TokenDelimiter delimiter);
inline const QChar *readPtr() const;
@@ -704,6 +705,25 @@ bool QTextStreamPrivate::flushWriteBuffer()
return flushed && bytesWritten == qint64(data.size());
}
+QString QTextStreamPrivate::read(int maxlen)
+{
+ QString ret;
+ if (string) {
+ lastTokenSize = qMin(maxlen, string->size() - stringOffset);
+ ret = string->mid(stringOffset, lastTokenSize);
+ } else {
+ while (readBuffer.size() - readBufferOffset < maxlen && fillReadBuffer()) ;
+ lastTokenSize = qMin(maxlen, readBuffer.size() - readBufferOffset);
+ ret = readBuffer.mid(readBufferOffset, lastTokenSize);
+ }
+ consumeLastToken();
+
+#if defined (QTEXTSTREAM_DEBUG)
+ qDebug("QTextStreamPrivate::read() maxlen = %d, token length = %d", maxlen, ret.length());
+#endif
+ return ret;
+}
+
/*! \internal
Scans no more than \a maxlen QChars in the current buffer for the
@@ -757,8 +777,6 @@ bool QTextStreamPrivate::scan(const QChar **ptr, int *length, int maxlen, TokenD
}
lastChar = ch;
break;
- default:
- break;
}
}
} while (!foundToken
@@ -1614,14 +1632,7 @@ QString QTextStream::readAll()
Q_D(QTextStream);
CHECK_VALID_STREAM(QString());
- const QChar *readPtr;
- int length;
- if (!d->scan(&readPtr, &length, /* maxlen = */ 0, QTextStreamPrivate::EndOfFile))
- return QString();
-
- QString tmp = QString(readPtr, length);
- d->consumeLastToken();
- return tmp;
+ return d->read(INT_MAX);
}
/*!
@@ -1673,14 +1684,7 @@ QString QTextStream::read(qint64 maxlen)
if (maxlen <= 0)
return QString::fromLatin1(""); // empty, not null
- const QChar *readPtr;
- int length;
- if (!d->scan(&readPtr, &length, int(maxlen), QTextStreamPrivate::EndOfFile))
- return QString();
-
- QString tmp = QString(readPtr, length);
- d->consumeLastToken();
- return tmp;
+ return d->read(int(maxlen));
}
/*! \internal