From df4076a06eeb6fbc0fc20d9ace0091eb476420a2 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Wed, 17 Aug 2022 14:57:29 +0200 Subject: [PATCH] Added ResizeablePointer::shift operator to shift data forward in buffer --- lib/util.cpp | 11 +++++++++++ lib/util.h | 1 + 2 files changed, 12 insertions(+) diff --git a/lib/util.cpp b/lib/util.cpp index 8dd75727..d4ff380f 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -191,6 +191,17 @@ namespace Util{ maxSize = 0; } + void ResizeablePointer::shift(size_t byteCount){ + //Shifting the entire buffer is easy, we do nothing and set size to zero + if (byteCount >= currSize){ + currSize = 0; + return; + } + //Shifting partial needs a memmove and size change + memmove(ptr, ((char*)ptr)+byteCount, currSize-byteCount); + currSize -= byteCount; + } + bool ResizeablePointer::assign(const void *p, uint32_t l){ if (!allocate(l)){return false;} memcpy(ptr, p, l); diff --git a/lib/util.h b/lib/util.h index 84b0d56c..d28a7f76 100644 --- a/lib/util.h +++ b/lib/util.h @@ -45,6 +45,7 @@ namespace Util{ bool append(const void *p, uint32_t l); bool append(const std::string &str); bool allocate(uint32_t l); + void shift(size_t byteCount); uint32_t rsize(); void truncate(const size_t newLen); inline operator char *(){return (char *)ptr;}