[network/libktorrent] src/util: CircularBuffer: use QByteArrayView instead of custom Range type

Jack Hill <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 73f1f0fb8ad48fd2d27df720b0dc54292c74bd60 by Jack Hill.
Committed on 01/08/2026 at 10:46.
Pushed by jackh into branch 'master'.

CircularBuffer: use QByteArrayView instead of custom Range type

M  +12   -13   src/util/circularbuffer.cpp
M  +2    -4    src/util/circularbuffer.h

https://invent.kde.org/network/libktorrent/-/commit/73f1f0fb8ad48fd2d27df720b0dc54292c74bd60

diff --git a/src/util/circularbuffer.cpp b/src/util/circularbuffer.cpp
index e2c08656..aa1aab7a 100644
--- a/src/util/circularbuffer.cpp
+++ b/src/util/circularbuffer.cpp
@@ -33,14 +33,13 @@ bt::Uint32 CircularBuffer::read(bt::Uint8 *ptr, bt::Uint32 max_len)
 
     const bt::Uint32 to_read = buf_size < max_len ? buf_size : max_len;
 
-    Range r = firstRange();
-    const bt::Uint32 s = r.second;
-    if (s >= to_read) {
-        memcpy(ptr, r.first, to_read);
+    const auto r1 = firstRange();
+    if (r1.size() >= to_read) {
+        memcpy(ptr, r1.data(), to_read);
     } else { // s < to_read
-        memcpy(ptr, r.first, s);
-        r = secondRange();
-        memcpy(ptr + s, r.first, to_read - s);
+        memcpy(ptr, r1.data(), r1.size());
+        const auto r2 = secondRange();
+        memcpy(ptr + r1.size(), r2.data(), to_read - r1.size());
     }
 
     start = (start + to_read) % buf_capacity;
@@ -74,21 +73,21 @@ bt::Uint32 CircularBuffer::write(QByteArrayView buf)
     return to_write;
 }
 
-CircularBuffer::Range CircularBuffer::firstRange()
+QByteArrayView CircularBuffer::firstRange()
 {
     if (start + buf_size > buf_capacity) {
-        return Range(data + start, buf_capacity - start);
+        return QByteArrayView{data, capacity()}.sliced(start);
     } else {
-        return Range(data + start, buf_size);
+        return QByteArrayView{data, capacity()}.sliced(start, buf_size);
     }
 }
 
-CircularBuffer::Range CircularBuffer::secondRange()
+QByteArrayView CircularBuffer::secondRange()
 {
     if (start + buf_size > buf_capacity) {
-        return Range(data, buf_size - (buf_capacity - start));
+        return QByteArrayView{data, capacity()}.first(buf_size - (capacity() - start));
     } else {
-        return Range((bt::Uint8 *)nullptr, 0);
+        return QByteArrayView{};
     }
 }
 
diff --git a/src/util/circularbuffer.h b/src/util/circularbuffer.h
index b32a9d96..e260b116 100644
--- a/src/util/circularbuffer.h
+++ b/src/util/circularbuffer.h
@@ -70,11 +70,9 @@ public:
     }
 
 private:
-    using Range = std::pair<bt::Uint8 *, bt::Uint32>;
-
     //! Get the first range
-    Range firstRange();
-    Range secondRange();
+    QByteArrayView firstRange();
+    QByteArrayView secondRange();
 
 private:
     bt::Uint8 *data;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.