From 01c02a1ef8ab3e1fd920d889b0555059f72edfe4 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 29 Apr 2023 20:22:31 +0200 Subject: [PATCH] util/StaticFifoBuffer: add method MoveFrom() --- src/util/StaticFifoBuffer.hxx | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/util/StaticFifoBuffer.hxx b/src/util/StaticFifoBuffer.hxx index b274861bd..5e38598b3 100644 --- a/src/util/StaticFifoBuffer.hxx +++ b/src/util/StaticFifoBuffer.hxx @@ -80,6 +80,31 @@ public: tail += n; } + /** + * Move as much data as possible from the specified buffer. + * + * @return the number of items moved + */ + template + constexpr size_type MoveFrom(std::span src) noexcept { + auto w = Write(); + + if (src.size() > w.size() && head > 0) { + /* if the source contains more data than we + can append at the tail, try to make more + room by shifting the head to 0 */ + Shift(); + w = Write(); + } + + if (src.size() > w.size()) + src = src.first(w.size()); + + std::move(src.begin(), src.end(), w.begin()); + Append(src.size()); + return src.size(); + } + constexpr size_type GetAvailable() const noexcept { return tail - head; }