mem.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "mem.h"
  2. #include <util/generic/yexception.h>
  3. TMemoryInput::TMemoryInput() noexcept
  4. : Buf_(nullptr)
  5. , Len_(0)
  6. {
  7. }
  8. TMemoryInput::TMemoryInput(const void* buf, size_t len) noexcept
  9. : Buf_((const char*)buf)
  10. , Len_(len)
  11. {
  12. }
  13. TMemoryInput::TMemoryInput(const TStringBuf buf) noexcept
  14. : Buf_(buf.data())
  15. , Len_(buf.size())
  16. {
  17. }
  18. TMemoryInput::~TMemoryInput() = default;
  19. size_t TMemoryInput::DoNext(const void** ptr, size_t len) {
  20. len = Min(Len_, len);
  21. *ptr = Buf_;
  22. Len_ -= len;
  23. Buf_ += len;
  24. return len;
  25. }
  26. void TMemoryInput::DoUndo(size_t len) {
  27. Len_ += len;
  28. Buf_ -= len;
  29. }
  30. TMemoryOutput::~TMemoryOutput() = default;
  31. size_t TMemoryOutput::DoNext(void** ptr) {
  32. Y_ENSURE(Buf_ < End_, TStringBuf("memory output stream exhausted"));
  33. *ptr = Buf_;
  34. size_t bufferSize = End_ - Buf_;
  35. Buf_ = End_;
  36. return bufferSize;
  37. }
  38. void TMemoryOutput::DoUndo(size_t len) {
  39. Buf_ -= len;
  40. }
  41. void TMemoryOutput::DoWrite(const void* buf, size_t len) {
  42. char* end = Buf_ + len;
  43. Y_ENSURE(end <= End_, TStringBuf("memory output stream exhausted"));
  44. memcpy(Buf_, buf, len);
  45. Buf_ = end;
  46. }
  47. void TMemoryOutput::DoWriteC(char c) {
  48. Y_ENSURE(Buf_ < End_, TStringBuf("memory output stream exhausted"));
  49. *Buf_++ = c;
  50. }