From 6f397e23f1ec63aa96521d81fc7c7aea75b0e274 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 27 Jul 2019 15:26:51 +0200 Subject: [PATCH] ByteBuffer: Add slice_view(). Works like slice() but makes a wrapper only. So we already have ByteBuffer::wrap() which is like a StringView for random data. This might not be the best abstraction actually, but this will be immediately useful so let's add it. --- AK/ByteBuffer.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/AK/ByteBuffer.h b/AK/ByteBuffer.h index f2cc8d623f..f86a709831 100644 --- a/AK/ByteBuffer.h +++ b/AK/ByteBuffer.h @@ -155,6 +155,17 @@ public: m_impl->trim(size); } + ByteBuffer slice_view(int offset, int size) const + { + if (is_null()) + return {}; + if (offset >= this->size()) + return {}; + if (offset + size >= this->size()) + size = this->size() - offset; + return wrap(offset_pointer(offset), size); + } + ByteBuffer slice(int offset, int size) const { if (is_null())