str.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "str.h"
  2. static constexpr size_t MIN_BUFFER_GROW_SIZE = 16;
  3. TStringInput::~TStringInput() = default;
  4. size_t TStringInput::DoNext(const void** ptr, size_t len) {
  5. len = Min(len, S_->size() - Pos_);
  6. *ptr = S_->data() + Pos_;
  7. Pos_ += len;
  8. return len;
  9. }
  10. void TStringInput::DoUndo(size_t len) {
  11. Y_ABORT_UNLESS(len <= Pos_);
  12. Pos_ -= len;
  13. }
  14. TStringOutput::~TStringOutput() = default;
  15. size_t TStringOutput::DoNext(void** ptr) {
  16. if (S_->size() == S_->capacity()) {
  17. S_->reserve(FastClp2(S_->capacity() + MIN_BUFFER_GROW_SIZE));
  18. }
  19. size_t previousSize = S_->size();
  20. ResizeUninitialized(*S_, S_->capacity());
  21. *ptr = S_->begin() + previousSize;
  22. return S_->size() - previousSize;
  23. }
  24. void TStringOutput::DoUndo(size_t len) {
  25. Y_ABORT_UNLESS(len <= S_->size(), "trying to undo more bytes than actually written");
  26. S_->resize(S_->size() - len);
  27. }
  28. void TStringOutput::DoWrite(const void* buf, size_t len) {
  29. S_->append((const char*)buf, len);
  30. }
  31. void TStringOutput::DoWriteC(char c) {
  32. S_->push_back(c);
  33. }
  34. TStringStream::~TStringStream() = default;