diff options
author | Mårten Nordheim <marten.nordheim@qt.io> | 2020-07-30 13:55:15 +0200 |
---|---|---|
committer | Mårten Nordheim <marten.nordheim@qt.io> | 2020-08-24 17:44:08 +0200 |
commit | 43f01ec2e5bc52b290098d0fca1dd4ae40f2c6d3 (patch) | |
tree | 954932bb1a14894169502be53b1f670c76c60d19 /src/corelib | |
parent | 0011a4510265a13b16f04bce8f16cc9381c22e88 (diff) |
QByteDataBuffer: add readPointer functionality using QByteArrayView
While it could be done before it's nice to not have a custom "local"
struct or the size in an out-parameter.
Change-Id: Ie910f7060b1dadf037312d45e922f8e2deafe3ec
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/text/qbytedata_p.h | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/corelib/text/qbytedata_p.h b/src/corelib/text/qbytedata_p.h index 551edc8b1b..053e4202d3 100644 --- a/src/corelib/text/qbytedata_p.h +++ b/src/corelib/text/qbytedata_p.h @@ -193,6 +193,55 @@ public: return originalAmount; } + /*! + \internal + Returns a view into the first QByteArray contained inside, + ignoring any already read data. Call advanceReadPointer() + to advance the view forward. When a QByteArray is exhausted + the view returned by this function will view into another + QByteArray if any. Returns a default constructed view if + no data is available. + + \sa advanceReadPointer + */ + QByteArrayView readPointer() const + { + if (isEmpty()) + return {}; + return { buffers.first().constData() + qsizetype(firstPos), + buffers.first().size() - qsizetype(firstPos) }; + } + + /*! + \internal + Advances the read pointer by \a distance. + + \sa readPointer + */ + void advanceReadPointer(qint64 distance) + { + qint64 newPos = firstPos + distance; + if (isEmpty()) { + newPos = 0; + } else if (auto size = buffers.first().size(); newPos >= size) { + while (newPos >= size) { + bufferCompleteSize -= (size - firstPos); + newPos -= size; + buffers.pop_front(); + if (isEmpty()) { + size = 0; + newPos = 0; + break; + } + size = buffers.front().size(); + } + bufferCompleteSize -= newPos; + } else { + bufferCompleteSize -= newPos - firstPos; + } + firstPos = newPos; + } + inline char getChar() { char c; |