diff options
author | Alex Trotsenko <alex1973tr@gmail.com> | 2015-06-03 12:56:27 +0300 |
---|---|---|
committer | Alex Trotsenko <alex1973tr@gmail.com> | 2015-08-27 07:16:49 +0000 |
commit | 526d9b52ce966c0feeed95b20c024fd2b3026d3c (patch) | |
tree | 81c5d65ff444f965996a28fa446c04a0696f35cd /src/corelib/tools/qringbuffer.cpp | |
parent | 64261c0871043ef44a0a9c855d78428e773dfa94 (diff) |
QRingBuffer: allow to search from any position
Change-Id: I348cd9da88fc81c3dd0789ce8cce9d80c4524e24
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@theqtcompany.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qringbuffer.cpp')
-rw-r--r-- | src/corelib/tools/qringbuffer.cpp | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/src/corelib/tools/qringbuffer.cpp b/src/corelib/tools/qringbuffer.cpp index 85cfdaf129..5345b44204 100644 --- a/src/corelib/tools/qringbuffer.cpp +++ b/src/corelib/tools/qringbuffer.cpp @@ -193,20 +193,33 @@ void QRingBuffer::clear() bufferSize = 0; } -qint64 QRingBuffer::indexOf(char c, qint64 maxLength) const +qint64 QRingBuffer::indexOf(char c, qint64 maxLength, qint64 pos) const { - qint64 index = 0; - qint64 j = head; - for (int i = 0; index < maxLength && i < buffers.size(); ++i) { - const char *ptr = buffers[i].constData() + j; - j = qMin(index + (i == tailBuffer ? tail : buffers[i].size()) - j, maxLength); - - while (index < j) { - if (*ptr++ == c) - return index; - ++index; + if (maxLength <= 0 || pos < 0) + return -1; + + qint64 index = -(pos + head); + for (int i = 0; i < buffers.size(); ++i) { + const qint64 nextBlockIndex = qMin(index + (i == tailBuffer ? tail : buffers[i].size()), + maxLength); + + if (nextBlockIndex > 0) { + const char *ptr = buffers[i].constData(); + if (index < 0) { + ptr -= index; + index = 0; + } + + do { + if (*ptr++ == c) + return index + pos; + } while (++index < nextBlockIndex); + + if (index == maxLength) + return -1; + } else { + index = nextBlockIndex; } - j = 0; } return -1; } |