* Stream for writing data into a string. */
| 62 | * Stream for writing data into a string. |
| 63 | */ |
| 64 | class TStringOutput: public IZeroCopyOutput { |
| 65 | public: |
| 66 | /** |
| 67 | * Constructs a string output stream that appends character data to the |
| 68 | * provided string. |
| 69 | * |
| 70 | * Note that this stream keeps a reference to the provided string, so it's |
| 71 | * up to the user to make sure that the string doesn't get destroyed while |
| 72 | * this stream is in use. |
| 73 | * |
| 74 | * @param s String to append to. |
| 75 | */ |
| 76 | inline TStringOutput(TString& s Y_LIFETIME_BOUND) noexcept |
| 77 | : S_(&s) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | TStringOutput(TStringOutput&& s) noexcept = default; |
| 82 | |
| 83 | ~TStringOutput() override; |
| 84 | |
| 85 | /** |
| 86 | * @param size Number of additional characters to |
| 87 | * reserve in output string. |
| 88 | */ |
| 89 | inline void Reserve(size_t size) { |
| 90 | S_->reserve(S_->size() + size); |
| 91 | } |
| 92 | |
| 93 | inline void Swap(TStringOutput& s) noexcept { |
| 94 | DoSwap(S_, s.S_); |
| 95 | } |
| 96 | |
| 97 | protected: |
| 98 | size_t DoNext(void** ptr) override; |
| 99 | void DoUndo(size_t len) override; |
| 100 | void DoWrite(const void* buf, size_t len) override; |
| 101 | void DoWriteC(char c) override; |
| 102 | |
| 103 | private: |
| 104 | TString* S_; |
| 105 | }; |
| 106 | |
| 107 | /** |
| 108 | * String input/output stream, similar to `std::stringstream`. |