From 7a86a134fcfce35414f7488dbfcfe4c747fb12fc Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 20 Mar 2018 02:18:26 -0700 Subject: [PATCH] ircd::buffer: Add move() analogue. --- include/ircd/buffer/buffer.h | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/include/ircd/buffer/buffer.h b/include/ircd/buffer/buffer.h index 2aff49e3b..ca405a0ea 100644 --- a/include/ircd/buffer/buffer.h +++ b/include/ircd/buffer/buffer.h @@ -68,8 +68,11 @@ namespace ircd::buffer template size_t consume(buffer &buffer, const size_t &bytes); template buffer operator+(const buffer &buffer, const size_t &bytes); template it copy(it &dest, const it &stop, const const_buffer &); + template it move(it &dest, const it &stop, const const_buffer &); template size_t copy(const mutable_buffer &dst, const char (&buf)[SIZE]); + template size_t move(const mutable_buffer &dst, const char (&buf)[SIZE]); size_t copy(const mutable_buffer &dst, const const_buffer &src); + size_t move(const mutable_buffer &dst, const const_buffer &src); size_t reverse(const mutable_buffer &dst, const const_buffer &src); void reverse(const mutable_buffer &buf); void zero(const mutable_buffer &buf); @@ -210,6 +213,19 @@ ircd::buffer::reverse(const mutable_buffer &dst, return ret; } +template +__attribute__((error +( + "Move source is an array. Is this a string literal? Do you want to move the \\0?" + " Disambiguate this by typing the source string_view or const_buffer." +))) +inline size_t +ircd::buffer::move(const mutable_buffer &dst, + const char (&buf)[SIZE]) +{ + return move(dst, const_buffer{buf}); +} + template __attribute__((error ( @@ -223,6 +239,16 @@ ircd::buffer::copy(const mutable_buffer &dst, return copy(dst, const_buffer{buf}); } +inline size_t +ircd::buffer::move(const mutable_buffer &dst, + const const_buffer &src) +{ + auto e{begin(dst)}; + move(e, end(dst), src); + assert(std::distance(begin(dst), e) >= 0); + return std::distance(begin(dst), e); +} + inline size_t ircd::buffer::copy(const mutable_buffer &dst, const const_buffer &src) @@ -233,6 +259,26 @@ ircd::buffer::copy(const mutable_buffer &dst, return std::distance(begin(dst), e); } +template +it +ircd::buffer::move(it &dest, + const it &stop, + const const_buffer &src) +{ + const it ret{dest}; + const ssize_t srcsz(size(src)); + assert(ret <= stop); + const ssize_t remain{std::distance(ret, stop)}; + const ssize_t mvsz{std::min(srcsz, remain)}; + assert(mvsz <= srcsz); + assert(mvsz <= remain); + assert(remain >= 0); + memmove(ret, data(src), mvsz); + dest += mvsz; + assert(dest <= stop); + return ret; +} + template it ircd::buffer::copy(it &dest,