chunked_buffer.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "chunked_buffer.h"
  2. #include <yql/essentials/utils/yql_panic.h>
  3. namespace NYql {
  4. TChunkedBuffer::TChunkedBuffer(TChunkedBuffer&& other) {
  5. Items_ = std::move(other.Items_);
  6. Size_ = other.Size_;
  7. other.Size_ = 0;
  8. }
  9. TChunkedBuffer& TChunkedBuffer::operator=(TChunkedBuffer&& other) {
  10. Items_ = std::move(other.Items_);
  11. Size_ = other.Size_;
  12. other.Size_ = 0;
  13. return *this;
  14. }
  15. TChunkedBuffer::TChunkedBuffer(TStringBuf buf, const std::shared_ptr<const void>& owner) {
  16. Append(buf, owner);
  17. }
  18. TChunkedBuffer::TChunkedBuffer(TString&& str) {
  19. Append(std::move(str));
  20. }
  21. const TChunkedBuffer::TChunk& TChunkedBuffer::Front() const {
  22. YQL_ENSURE(!Items_.empty());
  23. return Items_.front();
  24. }
  25. size_t TChunkedBuffer::CopyTo(IOutputStream& dst, size_t toCopy) const {
  26. size_t copied = 0;
  27. for (auto& chunk : Items_) {
  28. if (!toCopy) {
  29. break;
  30. }
  31. size_t copyChunk = std::min(chunk.Buf.size(), toCopy);
  32. dst.Write(chunk.Buf.data(), copyChunk);
  33. toCopy -= copyChunk;
  34. copied += copyChunk;
  35. }
  36. return copied;
  37. }
  38. TChunkedBuffer& TChunkedBuffer::Append(TStringBuf buf, const std::shared_ptr<const void>& owner) {
  39. if (!buf.empty()) {
  40. Items_.emplace_back(TChunk{buf, owner});
  41. Size_ += buf.size();
  42. }
  43. return *this;
  44. }
  45. TChunkedBuffer& TChunkedBuffer::Append(TString&& str) {
  46. if (!str.empty()) {
  47. auto owner = std::make_shared<TString>(std::move(str));
  48. Items_.emplace_back(TChunk{*owner, owner});
  49. Size_ += owner->size();
  50. }
  51. return *this;
  52. }
  53. TChunkedBuffer& TChunkedBuffer::Append(TChunkedBuffer&& other) {
  54. while (!other.Items_.empty()) {
  55. Items_.emplace_back(std::move(other.Items_.front()));
  56. Size_ += Items_.back().Buf.size();
  57. other.Items_.pop_front();
  58. }
  59. other.Size_ = 0;
  60. return *this;
  61. }
  62. TChunkedBuffer& TChunkedBuffer::Clear() {
  63. Items_.clear();
  64. Size_ = 0;
  65. return *this;
  66. }
  67. TChunkedBuffer& TChunkedBuffer::Erase(size_t size) {
  68. while (size && !Items_.empty()) {
  69. TStringBuf& buf = Items_.front().Buf;
  70. size_t toErase = std::min(buf.size(), size);
  71. buf.Skip(toErase);
  72. size -= toErase;
  73. Size_ -= toErase;
  74. if (buf.empty()) {
  75. Items_.pop_front();
  76. }
  77. }
  78. return *this;
  79. }
  80. TChunkedBufferOutput::TChunkedBufferOutput(TChunkedBuffer& dst)
  81. : Dst_(dst)
  82. {
  83. }
  84. void TChunkedBufferOutput::DoWrite(const void* buf, size_t len) {
  85. TString str(static_cast<const char*>(buf), len);
  86. Dst_.Append(std::move(str));
  87. }
  88. TChunkedBuffer CopyData(const TChunkedBuffer& src) {
  89. TChunkedBuffer result;
  90. TChunkedBufferOutput out(result);
  91. src.CopyTo(out);
  92. return result;
  93. }
  94. TChunkedBuffer CopyData(TChunkedBuffer&& src) {
  95. TChunkedBuffer result = CopyData(src);
  96. src.Clear();
  97. return result;
  98. }
  99. }