diff options
author | Andreas Kling <andreas.kling@nokia.com> | 2010-06-16 22:17:32 (GMT) |
---|---|---|
committer | Andreas Kling <andreas.kling@nokia.com> | 2010-06-16 23:50:29 (GMT) |
commit | b768d155c5b40c66f7b83ca5e52ad6dda78c2fe8 (patch) | |
tree | b9d0e0f43b9091cb7031fd10ce28f825b23b8b6e /src/corelib | |
parent | 6dffaee6ab0bbeddd9e0cb16ff4545c3cb09bfe9 (diff) | |
download | Qt-b768d155c5b40c66f7b83ca5e52ad6dda78c2fe8.zip Qt-b768d155c5b40c66f7b83ca5e52ad6dda78c2fe8.tar.gz Qt-b768d155c5b40c66f7b83ca5e52ad6dda78c2fe8.tar.bz2 |
Implement QIODevice::peek() using read() + ungetBlock().
The previous implementation was doing ungetChar() in a loop.
Task-number: QTBUG-9654
Reviewed-by: João Abecasis <joao.abecasis@nokia.com>
Reviewed-by: mread
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/io/qiodevice.cpp | 21 | ||||
-rw-r--r-- | src/corelib/io/qiodevice_p.h | 9 |
2 files changed, 23 insertions, 7 deletions
diff --git a/src/corelib/io/qiodevice.cpp b/src/corelib/io/qiodevice.cpp index 223df9b..d2e4f75 100644 --- a/src/corelib/io/qiodevice.cpp +++ b/src/corelib/io/qiodevice.cpp @@ -1476,10 +1476,13 @@ bool QIODevice::getChar(char *c) */ qint64 QIODevice::peek(char *data, qint64 maxSize) { + Q_D(QIODevice); qint64 readBytes = read(data, maxSize); - int i = readBytes; - while (i > 0) - ungetChar(data[i-- - 1]); + if (readBytes <= 0) + return readBytes; + + d->buffer.ungetBlock(data, readBytes); + d->pos -= readBytes; return readBytes; } @@ -1502,11 +1505,15 @@ qint64 QIODevice::peek(char *data, qint64 maxSize) */ QByteArray QIODevice::peek(qint64 maxSize) { + Q_D(QIODevice); QByteArray result = read(maxSize); - int i = result.size(); - const char *data = result.constData(); - while (i > 0) - ungetChar(data[i-- - 1]); + + if (result.isEmpty()) + return result; + + d->buffer.ungetBlock(result.constData(), result.size()); + d->pos -= result.size(); + return result; } diff --git a/src/corelib/io/qiodevice_p.h b/src/corelib/io/qiodevice_p.h index 94dadca..225a0b9 100644 --- a/src/corelib/io/qiodevice_p.h +++ b/src/corelib/io/qiodevice_p.h @@ -151,6 +151,15 @@ public: len++; *first = c; } + void ungetBlock(const char* block, int size) { + if ((first - buf) < size) { + // underflow, the existing valid data needs to move to the end of the (potentially bigger) buffer + makeSpace(len + size, freeSpaceAtStart); + memcpy(first - size, block, size); + } + first -= size; + len += size; + } private: enum FreeSpacePos {freeSpaceAtStart, freeSpaceAtEnd}; |