diff options
author | Devtools Arcadia <[email protected]> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <[email protected]> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/stream/mem.cpp |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/stream/mem.cpp')
-rw-r--r-- | util/stream/mem.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/util/stream/mem.cpp b/util/stream/mem.cpp new file mode 100644 index 00000000000..22a3339e274 --- /dev/null +++ b/util/stream/mem.cpp @@ -0,0 +1,65 @@ +#include "mem.h" + +#include <util/generic/yexception.h> + +TMemoryInput::TMemoryInput() noexcept + : Buf_(nullptr) + , Len_(0) +{ +} + +TMemoryInput::TMemoryInput(const void* buf, size_t len) noexcept + : Buf_((const char*)buf) + , Len_(len) +{ +} + +TMemoryInput::TMemoryInput(const TStringBuf buf) noexcept + : Buf_(buf.data()) + , Len_(buf.size()) +{ +} + +TMemoryInput::~TMemoryInput() = default; + +size_t TMemoryInput::DoNext(const void** ptr, size_t len) { + len = Min(Len_, len); + + *ptr = Buf_; + Len_ -= len; + Buf_ += len; + return len; +} + +void TMemoryInput::DoUndo(size_t len) { + Len_ += len; + Buf_ -= len; +} + +TMemoryOutput::~TMemoryOutput() = default; + +size_t TMemoryOutput::DoNext(void** ptr) { + Y_ENSURE(Buf_ < End_, TStringBuf("memory output stream exhausted")); + *ptr = Buf_; + size_t bufferSize = End_ - Buf_; + Buf_ = End_; + + return bufferSize; +} + +void TMemoryOutput::DoUndo(size_t len) { + Buf_ -= len; +} + +void TMemoryOutput::DoWrite(const void* buf, size_t len) { + char* end = Buf_ + len; + Y_ENSURE(end <= End_, TStringBuf("memory output stream exhausted")); + + memcpy(Buf_, buf, len); + Buf_ = end; +} + +void TMemoryOutput::DoWriteC(char c) { + Y_ENSURE(Buf_ < End_, TStringBuf("memory output stream exhausted")); + *Buf_++ = c; +} |