diff options
author | Alexander Fokin <apfokin@gmail.com> | 2022-02-10 16:45:38 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:38 +0300 |
commit | 863a59a65247c24db7cb06789bc5cf79d04da32f (patch) | |
tree | 139dc000c8cd4a40f5659e421b7c75135d080307 /util/stream | |
parent | f64e95a9eb9ab03240599eb9581c5a9102426a96 (diff) | |
download | ydb-863a59a65247c24db7cb06789bc5cf79d04da32f.tar.gz |
Restoring authorship annotation for Alexander Fokin <apfokin@gmail.com>. Commit 1 of 2.
Diffstat (limited to 'util/stream')
49 files changed, 1854 insertions, 1854 deletions
diff --git a/util/stream/aligned.cpp b/util/stream/aligned.cpp index 2fd12d15b7..9e280858a7 100644 --- a/util/stream/aligned.cpp +++ b/util/stream/aligned.cpp @@ -1,30 +1,30 @@ #include "aligned.h" - -size_t TAlignedInput::DoRead(void* ptr, size_t len) { - size_t ret = Stream_->Read(ptr, len); - Position_ += ret; - return ret; -} - -size_t TAlignedInput::DoSkip(size_t len) { - size_t ret = Stream_->Skip(len); - Position_ += ret; - return ret; -} - + +size_t TAlignedInput::DoRead(void* ptr, size_t len) { + size_t ret = Stream_->Read(ptr, len); + Position_ += ret; + return ret; +} + +size_t TAlignedInput::DoSkip(size_t len) { + size_t ret = Stream_->Skip(len); + Position_ += ret; + return ret; +} + size_t TAlignedInput::DoReadTo(TString& st, char ch) { - size_t ret = Stream_->ReadTo(st, ch); - Position_ += ret; - return ret; -} - + size_t ret = Stream_->ReadTo(st, ch); + Position_ += ret; + return ret; +} + ui64 TAlignedInput::DoReadAll(IOutputStream& out) { - ui64 ret = Stream_->ReadAll(out); - Position_ += ret; - return ret; -} - -void TAlignedOutput::DoWrite(const void* ptr, size_t len) { - Stream_->Write(ptr, len); - Position_ += len; -} + ui64 ret = Stream_->ReadAll(out); + Position_ += ret; + return ret; +} + +void TAlignedOutput::DoWrite(const void* ptr, size_t len) { + Stream_->Write(ptr, len); + Position_ += len; +} diff --git a/util/stream/aligned.h b/util/stream/aligned.h index 70e7be05a9..04b420c1db 100644 --- a/util/stream/aligned.h +++ b/util/stream/aligned.h @@ -1,99 +1,99 @@ #pragma once -#include "input.h" -#include "output.h" - -#include <util/system/yassert.h> -#include <util/generic/bitops.h> - -/** - * @addtogroup Streams - * @{ - */ - -/** - * Proxy input stream that provides additional functions that make reading - * aligned data easier. - */ +#include "input.h" +#include "output.h" + +#include <util/system/yassert.h> +#include <util/generic/bitops.h> + +/** + * @addtogroup Streams + * @{ + */ + +/** + * Proxy input stream that provides additional functions that make reading + * aligned data easier. + */ class TAlignedInput: public IInputStream { public: TAlignedInput(IInputStream* s) - : Stream_(s) - , Position_(0) + : Stream_(s) + , Position_(0) { } - /** - * Ensures alignment of the position in the input stream by skipping - * some input. - * - * @param alignment Alignment. Must be a power of 2. - */ - void Align(size_t alignment = sizeof(void*)) { + /** + * Ensures alignment of the position in the input stream by skipping + * some input. + * + * @param alignment Alignment. Must be a power of 2. + */ + void Align(size_t alignment = sizeof(void*)) { Y_ASSERT(IsPowerOf2(alignment)); - - if (Position_ & (alignment - 1)) { - size_t len = alignment - (Position_ & (alignment - 1)); - - do { - len -= DoSkip(len); - } while (len); - } + + if (Position_ & (alignment - 1)) { + size_t len = alignment - (Position_ & (alignment - 1)); + + do { + len -= DoSkip(len); + } while (len); + } } - -private: + +private: size_t DoRead(void* ptr, size_t len) override; size_t DoSkip(size_t len) override; size_t DoReadTo(TString& st, char ch) override; ui64 DoReadAll(IOutputStream& out) override; - -private: + +private: IInputStream* Stream_; - ui64 Position_; + ui64 Position_; }; -/** - * Proxy output stream that provides additional functions that make writing - * aligned data easier. - */ +/** + * Proxy output stream that provides additional functions that make writing + * aligned data easier. + */ class TAlignedOutput: public IOutputStream { public: TAlignedOutput(IOutputStream* s) - : Stream_(s) - , Position_(0) + : Stream_(s) + , Position_(0) { } TAlignedOutput(TAlignedOutput&&) noexcept = default; TAlignedOutput& operator=(TAlignedOutput&&) noexcept = default; - size_t GetCurrentOffset() const { - return Position_; + size_t GetCurrentOffset() const { + return Position_; } - /** - * Ensures alignment of the position in the output stream by writing - * some data. - * - * @param alignment Alignment. Must be a power of 2. - */ - void Align(size_t alignment = sizeof(void*)) { + /** + * Ensures alignment of the position in the output stream by writing + * some data. + * + * @param alignment Alignment. Must be a power of 2. + */ + void Align(size_t alignment = sizeof(void*)) { Y_ASSERT(IsPowerOf2(alignment)); - + static char unused[sizeof(void*) * 2]; Y_ASSERT(alignment <= sizeof(unused)); - + if (Position_ & (alignment - 1)) { - DoWrite(unused, alignment - (Position_ & (alignment - 1))); + DoWrite(unused, alignment - (Position_ & (alignment - 1))); } } - -private: + +private: void DoWrite(const void* ptr, size_t len) override; - -private: + +private: IOutputStream* Stream_; - ui64 Position_; + ui64 Position_; }; -/** @} */ +/** @} */ diff --git a/util/stream/aligned_ut.cpp b/util/stream/aligned_ut.cpp index e980d05cf7..cf2bf6f2a7 100644 --- a/util/stream/aligned_ut.cpp +++ b/util/stream/aligned_ut.cpp @@ -1,63 +1,63 @@ -#include "aligned.h" - +#include "aligned.h" + #include <library/cpp/testing/unittest/registar.h> - + class TNastyInputStream: public IInputStream { -public: +public: TNastyInputStream() : Pos_(0) { } - -protected: + +protected: size_t DoRead(void* buf, size_t len) override { if (len == 0) { - return 0; + return 0; } - - *static_cast<unsigned char*>(buf) = static_cast<unsigned char>(Pos_); + + *static_cast<unsigned char*>(buf) = static_cast<unsigned char>(Pos_); ++Pos_; - return 1; - } - + return 1; + } + size_t DoSkip(size_t len) override { if (len == 0) { - return 0; + return 0; } - + ++Pos_; - return 1; - } - -private: - size_t Pos_; -}; - + return 1; + } + +private: + size_t Pos_; +}; + Y_UNIT_TEST_SUITE(TAlignedTest) { Y_UNIT_TEST(AlignInput) { - TNastyInputStream input0; - TAlignedInput alignedInput(&input0); - - char c = '\1'; - - alignedInput.Align(2); - alignedInput.ReadChar(c); - UNIT_ASSERT_VALUES_EQUAL(c, '\x0'); - - alignedInput.Align(2); - alignedInput.ReadChar(c); - UNIT_ASSERT_VALUES_EQUAL(c, '\x2'); - - alignedInput.Align(4); - alignedInput.ReadChar(c); - UNIT_ASSERT_VALUES_EQUAL(c, '\x4'); - - alignedInput.Align(16); - alignedInput.ReadChar(c); - UNIT_ASSERT_VALUES_EQUAL(c, '\x10'); - - alignedInput.Align(128); - alignedInput.ReadChar(c); - UNIT_ASSERT_VALUES_EQUAL(c, '\x80'); - } -} + TNastyInputStream input0; + TAlignedInput alignedInput(&input0); + + char c = '\1'; + + alignedInput.Align(2); + alignedInput.ReadChar(c); + UNIT_ASSERT_VALUES_EQUAL(c, '\x0'); + + alignedInput.Align(2); + alignedInput.ReadChar(c); + UNIT_ASSERT_VALUES_EQUAL(c, '\x2'); + + alignedInput.Align(4); + alignedInput.ReadChar(c); + UNIT_ASSERT_VALUES_EQUAL(c, '\x4'); + + alignedInput.Align(16); + alignedInput.ReadChar(c); + UNIT_ASSERT_VALUES_EQUAL(c, '\x10'); + + alignedInput.Align(128); + alignedInput.ReadChar(c); + UNIT_ASSERT_VALUES_EQUAL(c, '\x80'); + } +} diff --git a/util/stream/buffer.cpp b/util/stream/buffer.cpp index 2facece4ea..9c353af400 100644 --- a/util/stream/buffer.cpp +++ b/util/stream/buffer.cpp @@ -66,15 +66,15 @@ TBufferOutput::TBufferOutput(TBuffer& buffer) { } -TBufferOutput::TBufferOutput(TBufferOutput&&) noexcept = default; -TBufferOutput& TBufferOutput::operator=(TBufferOutput&&) noexcept = default; - +TBufferOutput::TBufferOutput(TBufferOutput&&) noexcept = default; +TBufferOutput& TBufferOutput::operator=(TBufferOutput&&) noexcept = default; + TBufferOutput::~TBufferOutput() = default; TBuffer& TBufferOutput::Buffer() const noexcept { - return Impl_->Buffer(); -} - + return Impl_->Buffer(); +} + size_t TBufferOutput::DoNext(void** ptr) { return Impl_->DoNext(ptr); } @@ -91,28 +91,28 @@ void TBufferOutput::DoWriteC(char c) { Impl_->DoWriteC(c); } -TBufferInput::TBufferInput(const TBuffer& buffer) - : Buf_(buffer) - , Readed_(0) -{ +TBufferInput::TBufferInput(const TBuffer& buffer) + : Buf_(buffer) + , Readed_(0) +{ } TBufferInput::~TBufferInput() = default; - + const TBuffer& TBufferInput::Buffer() const noexcept { - return Buf_; -} - + return Buf_; +} + void TBufferInput::Rewind() noexcept { - Readed_ = 0; -} - -size_t TBufferInput::DoNext(const void** ptr, size_t len) { - len = Min(Buf_.Size() - Readed_, len); + Readed_ = 0; +} + +size_t TBufferInput::DoNext(const void** ptr, size_t len) { + len = Min(Buf_.Size() - Readed_, len); *ptr = Buf_.data() + Readed_; - Readed_ += len; - return len; -} + Readed_ += len; + return len; +} void TBufferInput::DoUndo(size_t len) { Y_VERIFY(len <= Readed_); diff --git a/util/stream/buffer.h b/util/stream/buffer.h index 9dc99dbe49..35d9db3018 100644 --- a/util/stream/buffer.h +++ b/util/stream/buffer.h @@ -1,48 +1,48 @@ #pragma once -#include "zerocopy.h" +#include "zerocopy.h" #include "zerocopy_output.h" #include <util/generic/ptr.h> class TBuffer; - -/** - * @addtogroup Streams_Buffers - * @{ - */ - -/** - * Output stream that writes into a `TBuffer`. - */ + +/** + * @addtogroup Streams_Buffers + * @{ + */ + +/** + * Output stream that writes into a `TBuffer`. + */ class TBufferOutput: public IZeroCopyOutput { public: class TImpl; - /** - * Constructs a stream that writes into an internal buffer. - * - * @param buflen Initial size of the internal buffer. + /** + * Constructs a stream that writes into an internal buffer. + * + * @param buflen Initial size of the internal buffer. */ TBufferOutput(size_t buflen = 1024); - /** - * Constructs a stream that writes into the provided buffer. It's up to the - * user to make sure that the buffer doesn't get destroyed while this stream - * is in use. - * - * @param buffer Buffer to write into. + /** + * Constructs a stream that writes into the provided buffer. It's up to the + * user to make sure that the buffer doesn't get destroyed while this stream + * is in use. + * + * @param buffer Buffer to write into. */ TBufferOutput(TBuffer& buffer); - TBufferOutput(TBufferOutput&&) noexcept; - TBufferOutput& operator=(TBufferOutput&&) noexcept; + TBufferOutput(TBufferOutput&&) noexcept; + TBufferOutput& operator=(TBufferOutput&&) noexcept; ~TBufferOutput() override; - /** - * @returns Buffer that this stream writes into. - */ + /** + * @returns Buffer that this stream writes into. + */ TBuffer& Buffer() const noexcept; private: @@ -55,65 +55,65 @@ private: THolder<TImpl> Impl_; }; -/** - * Input stream that reads from an external `TBuffer`. - */ +/** + * Input stream that reads from an external `TBuffer`. + */ class TBufferInput: public IZeroCopyInputFastReadTo { public: - /** - * Constructs a stream that reads from an external buffer. It's up to the - * user to make sure that the buffer doesn't get destroyed before this - * stream. - * - * @param buffer External buffer to read from. - */ - TBufferInput(const TBuffer& buffer); + /** + * Constructs a stream that reads from an external buffer. It's up to the + * user to make sure that the buffer doesn't get destroyed before this + * stream. + * + * @param buffer External buffer to read from. + */ + TBufferInput(const TBuffer& buffer); ~TBufferInput() override; const TBuffer& Buffer() const noexcept; - + void Rewind() noexcept; -protected: +protected: size_t DoNext(const void** ptr, size_t len) override; void DoUndo(size_t len) override; - + private: const TBuffer& Buf_; size_t Readed_; }; -/** - * Input/output stream that works with a `TBuffer`. - */ +/** + * Input/output stream that works with a `TBuffer`. + */ class TBufferStream: public TBufferOutput, public TBufferInput { public: - /** - * Constructs a stream that works with an internal buffer. - * - * @param buflen Initial size of the internal buffer. - */ + /** + * Constructs a stream that works with an internal buffer. + * + * @param buflen Initial size of the internal buffer. + */ inline TBufferStream(size_t buflen = 1024) : TBufferOutput(buflen) - , TBufferInput(TBufferOutput::Buffer()) + , TBufferInput(TBufferOutput::Buffer()) { } - /** - * Constructs a stream that works with the provided buffer. - * - * @param buffer Buffer to work with. - */ + /** + * Constructs a stream that works with the provided buffer. + * + * @param buffer Buffer to work with. + */ inline TBufferStream(TBuffer& buffer) : TBufferOutput(buffer) - , TBufferInput(TBufferOutput::Buffer()) + , TBufferInput(TBufferOutput::Buffer()) { } ~TBufferStream() override = default; - - using TBufferOutput::Buffer; + + using TBufferOutput::Buffer; }; -/** @} */ +/** @} */ diff --git a/util/stream/buffer_ut.cpp b/util/stream/buffer_ut.cpp index 3494696190..eea214142e 100644 --- a/util/stream/buffer_ut.cpp +++ b/util/stream/buffer_ut.cpp @@ -1,37 +1,37 @@ -#include "buffer.h" - +#include "buffer.h" + #include <library/cpp/testing/unittest/registar.h> - -#include <util/generic/buffer.h> - + +#include <util/generic/buffer.h> + #include <cstring> -#include "str.h" - +#include "str.h" + Y_UNIT_TEST_SUITE(TBufferTest) { Y_UNIT_TEST(Transfer) { - TBuffer buffer("razrazraz", 9); - TBufferInput input(buffer); - - input.Skip(3); - - TStringStream output; - TransferData(&input, &output); - - UNIT_ASSERT_VALUES_EQUAL(output.Str(), "razraz"); - } - + TBuffer buffer("razrazraz", 9); + TBufferInput input(buffer); + + input.Skip(3); + + TStringStream output; + TransferData(&input, &output); + + UNIT_ASSERT_VALUES_EQUAL(output.Str(), "razraz"); + } + Y_UNIT_TEST(ReadTo) { - TBuffer buffer("1234567890", 10); - TBufferInput input(buffer); - + TBuffer buffer("1234567890", 10); + TBufferInput input(buffer); + TString tmp; - UNIT_ASSERT_VALUES_EQUAL(input.ReadTo(tmp, '3'), 3); - UNIT_ASSERT_VALUES_EQUAL(tmp, "12"); - - UNIT_ASSERT_VALUES_EQUAL(input.ReadTo(tmp, 'z'), 7); - UNIT_ASSERT_VALUES_EQUAL(tmp, "4567890"); - } + UNIT_ASSERT_VALUES_EQUAL(input.ReadTo(tmp, '3'), 3); + UNIT_ASSERT_VALUES_EQUAL(tmp, "12"); + + UNIT_ASSERT_VALUES_EQUAL(input.ReadTo(tmp, 'z'), 7); + UNIT_ASSERT_VALUES_EQUAL(tmp, "4567890"); + } Y_UNIT_TEST(WriteViaNextAndUndo) { TBuffer buffer; @@ -82,4 +82,4 @@ Y_UNIT_TEST_SUITE(TBufferTest) { UNIT_ASSERT(0 == memcmp(buffer.data(), "1234567890", buffer.size())); } -} +} diff --git a/util/stream/buffered.cpp b/util/stream/buffered.cpp index a00e592e1c..8a00fe56c8 100644 --- a/util/stream/buffered.cpp +++ b/util/stream/buffered.cpp @@ -15,12 +15,12 @@ public: inline ~TImpl() = default; - inline size_t Next(const void** ptr, size_t len) { + inline size_t Next(const void** ptr, size_t len) { if (MemInput_.Exhausted()) { MemInput_.Reset(Buf(), Slave_->Read(Buf(), BufLen())); } - return MemInput_.Next(ptr, len); + return MemInput_.Next(ptr, len); } inline size_t Read(void* buf, size_t len) { @@ -67,7 +67,7 @@ public: TString s_tmp; - size_t ret = 0; + size_t ret = 0; while (true) { if (MemInput_.Exhausted()) { @@ -135,7 +135,7 @@ size_t TBufferedInput::DoSkip(size_t len) { return Impl_->Skip(len); } -size_t TBufferedInput::DoNext(const void** ptr, size_t len) { +size_t TBufferedInput::DoNext(const void** ptr, size_t len) { return Impl_->Next(ptr, len); } diff --git a/util/stream/buffered.h b/util/stream/buffered.h index 0847186141..a11da0ccf0 100644 --- a/util/stream/buffered.h +++ b/util/stream/buffered.h @@ -1,6 +1,6 @@ #pragma once -#include "zerocopy.h" +#include "zerocopy.h" #include "zerocopy_output.h" #include <utility> @@ -8,18 +8,18 @@ #include <util/generic/typetraits.h> #include <util/generic/store_policy.h> -/** - * @addtogroup Streams_Buffered - * @{ - */ - -/** - * Input stream that wraps the given stream and adds a buffer on top of it, - * thus making sure that data is read from the underlying stream in big chunks. - * - * Note that it does not claim ownership of the underlying stream, so it's up - * to the user to free it. - */ +/** + * @addtogroup Streams_Buffered + * @{ + */ + +/** + * Input stream that wraps the given stream and adds a buffer on top of it, + * thus making sure that data is read from the underlying stream in big chunks. + * + * Note that it does not claim ownership of the underlying stream, so it's up + * to the user to free it. + */ class TBufferedInput: public IZeroCopyInput { public: TBufferedInput(IInputStream* slave, size_t buflen = 8192); @@ -29,11 +29,11 @@ public: ~TBufferedInput() override; - /** - * Switches the underlying stream to the one provided. Does not clear the - * data that was already buffered. - * - * @param slave New underlying stream. + /** + * Switches the underlying stream to the one provided. Does not clear the + * data that was already buffered. + * + * @param slave New underlying stream. */ void Reset(IInputStream* slave); @@ -48,64 +48,64 @@ private: THolder<TImpl> Impl_; }; -/** - * Output stream that wraps the given stream and adds a buffer on top of it, - * thus making sure that data is written to the underlying stream in big chunks. - * - * Note that by default this stream does not propagate `Flush` and `Finish` - * calls to the underlying stream, instead simply flushing out the buffer. - * You can change this behavior by using propagation mode setters. - * - * Also note that this stream does not claim ownership of the underlying stream, - * so it's up to the user to free it. - */ +/** + * Output stream that wraps the given stream and adds a buffer on top of it, + * thus making sure that data is written to the underlying stream in big chunks. + * + * Note that by default this stream does not propagate `Flush` and `Finish` + * calls to the underlying stream, instead simply flushing out the buffer. + * You can change this behavior by using propagation mode setters. + * + * Also note that this stream does not claim ownership of the underlying stream, + * so it's up to the user to free it. + */ class TBufferedOutputBase: public IZeroCopyOutput { public: - /** - * Constructs a buffered stream that dynamically adjusts the size of the - * buffer. This works best when the amount of data that will be passed - * through this stream is not known and can range in size from several - * kilobytes to several gigabytes. - * - * @param slave Underlying stream. - */ + /** + * Constructs a buffered stream that dynamically adjusts the size of the + * buffer. This works best when the amount of data that will be passed + * through this stream is not known and can range in size from several + * kilobytes to several gigabytes. + * + * @param slave Underlying stream. + */ TBufferedOutputBase(IOutputStream* slave); - - /** - * Constructs a buffered stream with the given size of the buffer. - * - * @param slave Underlying stream. - * @param buflen Size of the buffer. - */ + + /** + * Constructs a buffered stream with the given size of the buffer. + * + * @param slave Underlying stream. + * @param buflen Size of the buffer. + */ TBufferedOutputBase(IOutputStream* slave, size_t buflen); - + TBufferedOutputBase(TBufferedOutputBase&&) noexcept; TBufferedOutputBase& operator=(TBufferedOutputBase&&) noexcept; ~TBufferedOutputBase() override; - /** - * @param propagate Whether `Flush` and `Finish` calls should - * be propagated to the underlying stream. - * By default they are not. + /** + * @param propagate Whether `Flush` and `Finish` calls should + * be propagated to the underlying stream. + * By default they are not. */ inline void SetPropagateMode(bool propagate) noexcept { SetFlushPropagateMode(propagate); SetFinishPropagateMode(propagate); } - /** - * @param propagate Whether `Flush` calls should be propagated - * to the underlying stream. By default they - * are not. - */ + /** + * @param propagate Whether `Flush` calls should be propagated + * to the underlying stream. By default they + * are not. + */ void SetFlushPropagateMode(bool propagate) noexcept; - - /** - * @param propagate Whether `Finish` calls should be propagated - * to the underlying stream. By default they - * are not. - */ + + /** + * @param propagate Whether `Finish` calls should be propagated + * to the underlying stream. By default they + * are not. + */ void SetFinishPropagateMode(bool propagate) noexcept; class TImpl; @@ -122,11 +122,11 @@ private: THolder<TImpl> Impl_; }; -/** - * Buffered output stream with a fixed-size buffer. - * - * @see TBufferedOutputBase - */ +/** + * Buffered output stream with a fixed-size buffer. + * + * @see TBufferedOutputBase + */ class TBufferedOutput: public TBufferedOutputBase { public: TBufferedOutput(IOutputStream* slave, size_t buflen = 8192); @@ -136,12 +136,12 @@ public: TBufferedOutput& operator=(TBufferedOutput&&) noexcept = default; }; -/** - * Buffered output stream that dynamically adjusts the size of the buffer based - * on the amount of data that's passed through it. - * - * @see TBufferedOutputBase - */ +/** + * Buffered output stream that dynamically adjusts the size of the buffer based + * on the amount of data that's passed through it. + * + * @see TBufferedOutputBase + */ class TAdaptiveBufferedOutput: public TBufferedOutputBase { public: TAdaptiveBufferedOutput(IOutputStream* slave); @@ -166,19 +166,19 @@ namespace NPrivate { }; } -/** - * A mixin class that turns unbuffered stream into a buffered one. - * - * Note that using this mixin with a stream that is already buffered won't +/** + * A mixin class that turns unbuffered stream into a buffered one. + * + * Note that using this mixin with a stream that is already buffered won't * result in double buffering, e.g. `TBuffered<TBuffered<TUnbufferedFileInput>>` and * `TBuffered<TUnbufferedFileInput>` are basically the same types. - * - * Example usage: - * @code + * + * Example usage: + * @code * TBuffered<TUnbufferedFileInput> file_input(1024, "/path/to/file"); * TBuffered<TUnbufferedFileOutput> file_output(1024, "/path/to/file"); - * @endcode - * Here 1024 is the size of the buffer. + * @endcode + * Here 1024 is the size of the buffer. */ template <class TSlave> class TBuffered: private TEmbedPolicy<TSlave>, public ::NPrivate::TBufferedStreamFor<TSlave>::TResult { @@ -203,17 +203,17 @@ public: TBuffered& operator=(TBuffered&&) = delete; }; -/** - * A mixin class that turns unbuffered stream into an adaptively buffered one. - * Created stream differs from the one created via `TBuffered` template in that - * it dynamically adjusts the size of the buffer based on the amount of data - * that's passed through it. - * - * Example usage: - * @code +/** + * A mixin class that turns unbuffered stream into an adaptively buffered one. + * Created stream differs from the one created via `TBuffered` template in that + * it dynamically adjusts the size of the buffer based on the amount of data + * that's passed through it. + * + * Example usage: + * @code * TAdaptivelyBuffered<TUnbufferedFileOutput> file_output("/path/to/file"); - * @endcode - */ + * @endcode + */ template <class TSlave> class TAdaptivelyBuffered: private TEmbedPolicy<TSlave>, public TAdaptiveBufferedOutput { using TSlaveBase = TEmbedPolicy<TSlave>; @@ -232,4 +232,4 @@ public: TAdaptivelyBuffered& operator=(TAdaptivelyBuffered&& other) = delete; }; -/** @} */ +/** @} */ diff --git a/util/stream/buffered_ut.cpp b/util/stream/buffered_ut.cpp index 41d2fc3030..8da9761943 100644 --- a/util/stream/buffered_ut.cpp +++ b/util/stream/buffered_ut.cpp @@ -127,16 +127,16 @@ Y_UNIT_TEST_SUITE(TestBufferedIO) { UNIT_ASSERT_VALUES_EQUAL(c, 'k'); UNIT_ASSERT_VALUES_EQUAL(in.Skip(6), 3); //24 eof } - + Y_UNIT_TEST(TestReadTo) { TString s("0123456789abc"); - TBuffered<TStringInput> in(2, s); + TBuffered<TStringInput> in(2, s); TString t; - UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8); - UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); - UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '8'), 1); - UNIT_ASSERT_VALUES_EQUAL(t, ""); - UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 4); - UNIT_ASSERT_VALUES_EQUAL(t, "9abc"); - } + UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8); + UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); + UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '8'), 1); + UNIT_ASSERT_VALUES_EQUAL(t, ""); + UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 4); + UNIT_ASSERT_VALUES_EQUAL(t, "9abc"); + } } diff --git a/util/stream/debug.h b/util/stream/debug.h index 92d6d4b42d..c5269d6e82 100644 --- a/util/stream/debug.h +++ b/util/stream/debug.h @@ -2,14 +2,14 @@ #include "output.h" -/** - * @addtogroup Streams - * @{ - */ - -/** - * Debug output stream. Writes into `stderr`. - */ +/** + * @addtogroup Streams + * @{ + */ + +/** + * Debug output stream. Writes into `stderr`. + */ class TDebugOutput: public IOutputStream { public: inline TDebugOutput() noexcept = default; @@ -22,32 +22,32 @@ private: void DoWrite(const void* buf, size_t len) override; }; -/** - * @returns Standard debug stream. - * @see Cdbg - */ +/** + * @returns Standard debug stream. + * @see Cdbg + */ IOutputStream& StdDbgStream() noexcept; -/** - * This function returns the current debug level as set via `DBGOUT` environment - * variable. - * - * Note that the proper way to use this function is via `Y_DBGTRACE` macro. - * There are very few cases when there is a need to use it directly. - * - * @returns Debug level. - * @see ETraceLevel - * @see DBGTRACE - */ +/** + * This function returns the current debug level as set via `DBGOUT` environment + * variable. + * + * Note that the proper way to use this function is via `Y_DBGTRACE` macro. + * There are very few cases when there is a need to use it directly. + * + * @returns Debug level. + * @see ETraceLevel + * @see DBGTRACE + */ int StdDbgLevel() noexcept; -/** - * Standard debug stream. - * - * Behavior of this stream is controlled via `DBGOUT` environment variable. - * If this variable is set, then this stream is redirected into `stderr`, - * otherwise whatever is written into it is simply ignored. - */ +/** + * Standard debug stream. + * + * Behavior of this stream is controlled via `DBGOUT` environment variable. + * If this variable is set, then this stream is redirected into `stderr`, + * otherwise whatever is written into it is simply ignored. + */ #define Cdbg (StdDbgStream()) -/** @} */ +/** @} */ diff --git a/util/stream/file.cpp b/util/stream/file.cpp index dc5d2f6311..3e8b881a8b 100644 --- a/util/stream/file.cpp +++ b/util/stream/file.cpp @@ -25,13 +25,13 @@ size_t TUnbufferedFileInput::DoRead(void* buf, size_t len) { size_t TUnbufferedFileInput::DoSkip(size_t len) { if (len < 384) { - /* Base implementation calls DoRead, which results in one system call - * instead of three as in fair skip implementation. For small sizes - * actually doing one read is cheaper. Experiments show that the - * border that separates two implementations performance-wise lies - * in the range of 384-512 bytes (assuming that the file is in OS cache). */ + /* Base implementation calls DoRead, which results in one system call + * instead of three as in fair skip implementation. For small sizes + * actually doing one read is cheaper. Experiments show that the + * border that separates two implementations performance-wise lies + * in the range of 384-512 bytes (assuming that the file is in OS cache). */ return IInputStream::DoSkip(len); - } + } /* TFile::Seek can seek beyond the end of file, so we need to do * size check here. */ @@ -40,8 +40,8 @@ size_t TUnbufferedFileInput::DoSkip(size_t len) { i64 newPos = File_.Seek(Min<i64>(size, oldPos + len), sSet); return newPos - oldPos; -} - +} + TUnbufferedFileOutput::TUnbufferedFileOutput(const TString& path) : File_(path, CreateAlways | WrOnly | Seq) { diff --git a/util/stream/file.h b/util/stream/file.h index c1cf4f591d..8a667c4241 100644 --- a/util/stream/file.h +++ b/util/stream/file.h @@ -1,25 +1,25 @@ #pragma once #include "fwd.h" -#include "input.h" -#include "output.h" +#include "input.h" +#include "output.h" #include "buffered.h" -#include "mem.h" +#include "mem.h" #include <util/system/file.h> #include <utility> -/** - * @addtogroup Streams_Files - * @{ +/** + * @addtogroup Streams_Files + * @{ */ - -/** + +/** * Unbuffered file input stream. - * - * Note that the input is not buffered, which means that `ReadLine` calls will - * be _very_ slow. - */ + * + * Note that the input is not buffered, which means that `ReadLine` calls will + * be _very_ slow. + */ class TUnbufferedFileInput: public IInputStream { public: TUnbufferedFileInput(const TFile& file); @@ -33,9 +33,9 @@ private: TFile File_; }; -/** - * Memory-mapped file input stream. - */ +/** + * Memory-mapped file input stream. + */ class TMappedFileInput: public TMemoryInput { public: TMappedFileInput(const TFile& file); @@ -47,12 +47,12 @@ private: THolder<TImpl> Impl_; }; -/** - * File output stream. - * - * Note that the output is unbuffered, thus writing in many small chunks is - * likely to be quite slow. - */ +/** + * File output stream. + * + * Note that the output is unbuffered, thus writing in many small chunks is + * likely to be quite slow. + */ class TUnbufferedFileOutput: public IOutputStream { public: TUnbufferedFileOutput(const TString& path); @@ -70,11 +70,11 @@ private: TFile File_; }; -/** - * Buffered file input stream. - * - * @see TBuffered - */ +/** + * Buffered file input stream. + * + * @see TBuffered + */ class TFileInput: public TBuffered<TUnbufferedFileInput> { public: template <class T> @@ -86,14 +86,14 @@ public: ~TFileInput() override = default; }; -/** - * Buffered file output stream. - * +/** + * Buffered file output stream. + * * Currently deprecated, please use TFileOutput in new code. - * - * @deprecated - * @see TBuffered - */ + * + * @deprecated + * @see TBuffered + */ class TFixedBufferFileOutput: public TBuffered<TUnbufferedFileOutput> { public: template <class T> @@ -105,4 +105,4 @@ public: ~TFixedBufferFileOutput() override = default; }; -/** @} */ +/** @} */ diff --git a/util/stream/file_ut.cpp b/util/stream/file_ut.cpp index ac0f09796e..1ed0beb1e5 100644 --- a/util/stream/file_ut.cpp +++ b/util/stream/file_ut.cpp @@ -1,61 +1,61 @@ -#include "file.h" - +#include "file.h" + #include <library/cpp/testing/unittest/registar.h> - -#include <util/system/tempfile.h> - -static const char* TmpFileName = "./fileio"; -static const char* TmpFileContents = "To do good to Mankind is the chivalrous plan"; -static const char* TmpFileSubstring = strstr(TmpFileContents, "chivalrous"); - + +#include <util/system/tempfile.h> + +static const char* TmpFileName = "./fileio"; +static const char* TmpFileContents = "To do good to Mankind is the chivalrous plan"; +static const char* TmpFileSubstring = strstr(TmpFileContents, "chivalrous"); + Y_UNIT_TEST_SUITE(TFileTest) { Y_UNIT_TEST(InputTest) { - TTempFile tmp(TmpFileName); - - { + TTempFile tmp(TmpFileName); + + { TUnbufferedFileOutput output(TmpFileName); - output.Write(TmpFileContents, strlen(TmpFileContents)); - } - - { + output.Write(TmpFileContents, strlen(TmpFileContents)); + } + + { TUnbufferedFileInput input(TmpFileName); TString s = input.ReadAll(); - UNIT_ASSERT_VALUES_EQUAL(s, TmpFileContents); - } - - { + UNIT_ASSERT_VALUES_EQUAL(s, TmpFileContents); + } + + { TUnbufferedFileInput input(TmpFileName); - input.Skip(TmpFileSubstring - TmpFileContents); + input.Skip(TmpFileSubstring - TmpFileContents); TString s = input.ReadAll(); - UNIT_ASSERT_VALUES_EQUAL(s, "chivalrous plan"); - } - - { + UNIT_ASSERT_VALUES_EQUAL(s, "chivalrous plan"); + } + + { TUnbufferedFileOutput output(TFile::ForAppend(TmpFileName)); - output.Write(TmpFileContents, strlen(TmpFileContents)); - } - - { + output.Write(TmpFileContents, strlen(TmpFileContents)); + } + + { TUnbufferedFileInput input(TmpFileName); TString s = input.ReadAll(); UNIT_ASSERT_VALUES_EQUAL(s, TString::Join(TmpFileContents, TmpFileContents)); - } - } - + } + } + Y_UNIT_TEST(EmptyMapTest) { - TTempFile tmp(TmpFileName); - - { + TTempFile tmp(TmpFileName); + + { TUnbufferedFileOutput output(TmpFileName); - /* Write nothing. */ - } - - { - TMappedFileInput input(TmpFileName); + /* Write nothing. */ + } + + { + TMappedFileInput input(TmpFileName); TString s = input.ReadAll(); - UNIT_ASSERT(s.empty()); - } - } + UNIT_ASSERT(s.empty()); + } + } #ifdef _unix_ Y_UNIT_TEST(PipeReadLineTest) { @@ -71,4 +71,4 @@ Y_UNIT_TEST_SUITE(TFileTest) { close(fds[1]); } #endif -} +} diff --git a/util/stream/format.cpp b/util/stream/format.cpp index 3996130df5..35b2c83444 100644 --- a/util/stream/format.cpp +++ b/util/stream/format.cpp @@ -38,10 +38,10 @@ namespace NFormatPrivate { template <> void Out<NFormatPrivate::THumanReadableSize>(IOutputStream& stream, const NFormatPrivate::THumanReadableSize& value) { - ui64 base = value.Format == SF_BYTES ? 1024 : 1000; - ui64 base2 = base * base; - ui64 base3 = base * base2; - ui64 base4 = base * base3; + ui64 base = value.Format == SF_BYTES ? 1024 : 1000; + ui64 base2 = base * base; + ui64 base3 = base * base2; + ui64 base4 = base * base3; double v = value.Value; if (v < 0) { @@ -61,13 +61,13 @@ void Out<NFormatPrivate::THumanReadableSize>(IOutputStream& stream, const NForma NFormatPrivate::PrintDoubleShortly(stream, v / (double)base4) << 'T'; } - if (value.Format == SF_BYTES) { + if (value.Format == SF_BYTES) { if (v < base) { - stream << "B"; - } else { - stream << "iB"; - } - } + stream << "B"; + } else { + stream << "iB"; + } + } } template <> diff --git a/util/stream/format.h b/util/stream/format.h index b033208a1b..8f8526f479 100644 --- a/util/stream/format.h +++ b/util/stream/format.h @@ -5,29 +5,29 @@ #include <util/datetime/base.h> #include <util/generic/strbuf.h> -#include <util/generic/flags.h> +#include <util/generic/flags.h> #include <util/memory/tempbuf.h> #include <util/string/cast.h> -enum ENumberFormatFlag { - HF_FULL = 0x01, /**< Output number with leading zeros. */ - HF_ADDX = 0x02, /**< Output '0x' or '0b' before hex/bin digits. */ +enum ENumberFormatFlag { + HF_FULL = 0x01, /**< Output number with leading zeros. */ + HF_ADDX = 0x02, /**< Output '0x' or '0b' before hex/bin digits. */ }; -Y_DECLARE_FLAGS(ENumberFormat, ENumberFormatFlag) -Y_DECLARE_OPERATORS_FOR_FLAGS(ENumberFormat) +Y_DECLARE_FLAGS(ENumberFormat, ENumberFormatFlag) +Y_DECLARE_OPERATORS_FOR_FLAGS(ENumberFormat) -enum ESizeFormat { +enum ESizeFormat { SF_QUANTITY, /**< Base 1000, usual suffixes. 1100 gets turned into "1.1K". */ SF_BYTES, /**< Base 1024, byte suffix. 1100 gets turned into "1.07KiB". */ -}; - +}; + namespace NFormatPrivate { template <size_t Value> struct TLog2: std::integral_constant<size_t, TLog2<Value / 2>::value + 1> {}; - + template <> struct TLog2<1>: std::integral_constant<size_t, 0> {}; - + static inline void WriteChars(IOutputStream& os, char c, size_t count) { if (count == 0) return; @@ -90,65 +90,65 @@ namespace NFormatPrivate { return o; } - template <typename T, size_t Base> - struct TBaseNumber { - T Value; - ENumberFormat Flags; + template <typename T, size_t Base> + struct TBaseNumber { + T Value; + ENumberFormat Flags; - template <typename OtherT> - inline TBaseNumber(OtherT value, ENumberFormat flags) + template <typename OtherT> + inline TBaseNumber(OtherT value, ENumberFormat flags) : Value(value) , Flags(flags) { } }; - template <typename T, size_t Base> + template <typename T, size_t Base> using TUnsignedBaseNumber = TBaseNumber<std::make_unsigned_t<std::remove_cv_t<T>>, Base>; - template <typename T, size_t Base> + template <typename T, size_t Base> IOutputStream& operator<<(IOutputStream& stream, const TBaseNumber<T, Base>& value) { - char buf[8 * sizeof(T) + 1]; /* Add 1 for sign. */ - TStringBuf str(buf, IntToString<Base>(value.Value, buf, sizeof(buf))); + char buf[8 * sizeof(T) + 1]; /* Add 1 for sign. */ + TStringBuf str(buf, IntToString<Base>(value.Value, buf, sizeof(buf))); - if (str[0] == '-') { - stream << '-'; + if (str[0] == '-') { + stream << '-'; str.Skip(1); } - if (value.Flags & HF_ADDX) { - if (Base == 16) { + if (value.Flags & HF_ADDX) { + if (Base == 16) { stream << TStringBuf("0x"); - } else if (Base == 2) { + } else if (Base == 2) { stream << TStringBuf("0b"); - } + } } - if (value.Flags & HF_FULL) { - WriteChars(stream, '0', (8 * sizeof(T) + TLog2<Base>::value - 1) / TLog2<Base>::value - str.size()); + if (value.Flags & HF_FULL) { + WriteChars(stream, '0', (8 * sizeof(T) + TLog2<Base>::value - 1) / TLog2<Base>::value - str.size()); } - stream << str; - return stream; + stream << str; + return stream; } - template <typename Char, size_t Base> - struct TBaseText { - TBasicStringBuf<Char> Text; + template <typename Char, size_t Base> + struct TBaseText { + TBasicStringBuf<Char> Text; - inline TBaseText(const TBasicStringBuf<Char> text) + inline TBaseText(const TBasicStringBuf<Char> text) : Text(text) { } }; - template <typename Char, size_t Base> + template <typename Char, size_t Base> IOutputStream& operator<<(IOutputStream& os, const TBaseText<Char, Base>& text) { for (size_t i = 0; i < text.Text.size(); ++i) { if (i != 0) { os << ' '; } - os << TUnsignedBaseNumber<Char, Base>(text.Text[i], HF_FULL); + os << TUnsignedBaseNumber<Char, Base>(text.Text[i], HF_FULL); } return os; } @@ -182,27 +182,27 @@ namespace NFormatPrivate { struct THumanReadableSize { double Value; - ESizeFormat Format; + ESizeFormat Format; }; } -/** - * Output manipulator basically equivalent to `std::setw` and `std::setfill` - * combined. - * +/** + * Output manipulator basically equivalent to `std::setw` and `std::setfill` + * combined. + * * When written into a `IOutputStream`, writes out padding characters first, - * and then provided value. - * - * Example usage: - * @code - * stream << LeftPad(12345, 10, '0'); // Will output "0000012345" - * @endcode - * - * @param value Value to output. - * @param width Target total width. - * @param padc Character to use for padding. + * and then provided value. + * + * Example usage: + * @code + * stream << LeftPad(12345, 10, '0'); // Will output "0000012345" + * @endcode + * + * @param value Value to output. + * @param width Target total width. + * @param padc Character to use for padding. * @see RightPad - */ + */ template <typename T> static constexpr ::NFormatPrivate::TLeftPad<T> LeftPad(const T& value, const size_t width, const char padc = ' ') noexcept { return ::NFormatPrivate::TLeftPad<T>(value, width, padc); @@ -213,22 +213,22 @@ static constexpr ::NFormatPrivate::TLeftPad<const T*> LeftPad(const T (&value)[N return ::NFormatPrivate::TLeftPad<const T*>(value, width, padc); } -/** - * Output manipulator similar to `std::setw` and `std::setfill`. - * +/** + * Output manipulator similar to `std::setw` and `std::setfill`. + * * When written into a `IOutputStream`, writes provided value first, and then - * the padding characters. - * - * Example usage: - * @code + * the padding characters. + * + * Example usage: + * @code * stream << RightPad("column1", 10, ' '); // Will output "column1 " - * @endcode - * - * @param value Value to output. - * @param width Target total width. - * @param padc Character to use for padding. - * @see LeftPad - */ + * @endcode + * + * @param value Value to output. + * @param width Target total width. + * @param padc Character to use for padding. + * @see LeftPad + */ template <typename T> static constexpr ::NFormatPrivate::TRightPad<T> RightPad(const T& value, const size_t width, const char padc = ' ') noexcept { return ::NFormatPrivate::TRightPad<T>(value, width, padc); @@ -239,163 +239,163 @@ static constexpr ::NFormatPrivate::TRightPad<const T*> RightPad(const T (&value) return ::NFormatPrivate::TRightPad<const T*>(value, width, padc); } -/** - * Output manipulator similar to `std::setbase(16)`. - * +/** + * Output manipulator similar to `std::setbase(16)`. + * * When written into a `IOutputStream`, writes out the provided value in - * hexadecimal form. The value is treated as unsigned, even if its type is in - * fact signed. - * - * Example usage: - * @code - * stream << Hex(-1); // Will output "0xFFFFFFFF" - * stream << Hex(1ull); // Will output "0x0000000000000001" - * @endcode - * - * @param value Value to output. - * @param flags Output flags. - */ + * hexadecimal form. The value is treated as unsigned, even if its type is in + * fact signed. + * + * Example usage: + * @code + * stream << Hex(-1); // Will output "0xFFFFFFFF" + * stream << Hex(1ull); // Will output "0x0000000000000001" + * @endcode + * + * @param value Value to output. + * @param flags Output flags. + */ template <typename T> -static constexpr ::NFormatPrivate::TUnsignedBaseNumber<T, 16> Hex(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept { - return {value, flags}; +static constexpr ::NFormatPrivate::TUnsignedBaseNumber<T, 16> Hex(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept { + return {value, flags}; } -/** - * Output manipulator similar to `std::setbase(16)`. - * +/** + * Output manipulator similar to `std::setbase(16)`. + * * When written into a `IOutputStream`, writes out the provided value in - * hexadecimal form. - * - * Example usage: - * @code - * stream << SHex(-1); // Will output "-0x00000001" - * stream << SHex(1ull); // Will output "0x0000000000000001" - * @endcode - * - * @param value Value to output. - * @param flags Output flags. - */ + * hexadecimal form. + * + * Example usage: + * @code + * stream << SHex(-1); // Will output "-0x00000001" + * stream << SHex(1ull); // Will output "0x0000000000000001" + * @endcode + * + * @param value Value to output. + * @param flags Output flags. + */ template <typename T> -static constexpr ::NFormatPrivate::TBaseNumber<T, 16> SHex(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept { - return {value, flags}; +static constexpr ::NFormatPrivate::TBaseNumber<T, 16> SHex(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept { + return {value, flags}; } /** - * Output manipulator similar to `std::setbase(2)`. - * + * Output manipulator similar to `std::setbase(2)`. + * * When written into a `IOutputStream`, writes out the provided value in - * binary form. The value is treated as unsigned, even if its type is in - * fact signed. - * - * Example usage: - * @code - * stream << Bin(-1); // Will output "0b11111111111111111111111111111111" - * stream << Bin(1); // Will output "0b00000000000000000000000000000001" - * @endcode - * - * @param value Value to output. - * @param flags Output flags. - */ -template <typename T> -static constexpr ::NFormatPrivate::TUnsignedBaseNumber<T, 2> Bin(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept { - return {value, flags}; -} - -/** - * Output manipulator similar to `std::setbase(2)`. - * + * binary form. The value is treated as unsigned, even if its type is in + * fact signed. + * + * Example usage: + * @code + * stream << Bin(-1); // Will output "0b11111111111111111111111111111111" + * stream << Bin(1); // Will output "0b00000000000000000000000000000001" + * @endcode + * + * @param value Value to output. + * @param flags Output flags. + */ +template <typename T> +static constexpr ::NFormatPrivate::TUnsignedBaseNumber<T, 2> Bin(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept { + return {value, flags}; +} + +/** + * Output manipulator similar to `std::setbase(2)`. + * * When written into a `IOutputStream`, writes out the provided value in - * binary form. - * - * Example usage: - * @code - * stream << SBin(-1); // Will output "-0b00000000000000000000000000000001" - * stream << SBin(1); // Will output "0b00000000000000000000000000000001" - * @endcode - * - * @param value Value to output. - * @param flags Output flags. - */ -template <typename T> -static constexpr ::NFormatPrivate::TBaseNumber<T, 2> SBin(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept { - return {value, flags}; -} - -/** - * Output manipulator for hexadecimal string output. - * + * binary form. + * + * Example usage: + * @code + * stream << SBin(-1); // Will output "-0b00000000000000000000000000000001" + * stream << SBin(1); // Will output "0b00000000000000000000000000000001" + * @endcode + * + * @param value Value to output. + * @param flags Output flags. + */ +template <typename T> +static constexpr ::NFormatPrivate::TBaseNumber<T, 2> SBin(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept { + return {value, flags}; +} + +/** + * Output manipulator for hexadecimal string output. + * * When written into a `IOutputStream`, writes out the provided characters - * in hexadecimal form divided by space character. - * - * Example usage: - * @code + * in hexadecimal form divided by space character. + * + * Example usage: + * @code * stream << HexText(TStringBuf("abcи")); // Will output "61 62 63 D0 B8" - * stream << HexText(TWtringBuf(u"abcи")); // Will output "0061 0062 0063 0438" - * @endcode - * - * @param value String to output. - */ + * stream << HexText(TWtringBuf(u"abcи")); // Will output "0061 0062 0063 0438" + * @endcode + * + * @param value String to output. + */ template <typename TChar> -static inline ::NFormatPrivate::TBaseText<TChar, 16> HexText(const TBasicStringBuf<TChar> value) { - return ::NFormatPrivate::TBaseText<TChar, 16>(value); +static inline ::NFormatPrivate::TBaseText<TChar, 16> HexText(const TBasicStringBuf<TChar> value) { + return ::NFormatPrivate::TBaseText<TChar, 16>(value); } /** - * Output manipulator for binary string output. - * + * Output manipulator for binary string output. + * * When written into a `IOutputStream`, writes out the provided characters - * in binary form divided by space character. - * - * Example usage: - * @code + * in binary form divided by space character. + * + * Example usage: + * @code * stream << BinText(TStringBuf("aaa")); // Will output "01100001 01100001 01100001" - * @endcode - * - * @param value String to output. - */ -template <typename TChar> -static inline ::NFormatPrivate::TBaseText<TChar, 2> BinText(const TBasicStringBuf<TChar> value) { - return ::NFormatPrivate::TBaseText<TChar, 2>(value); -} - -/** - * Output manipulator for printing `TDuration` values. - * + * @endcode + * + * @param value String to output. + */ +template <typename TChar> +static inline ::NFormatPrivate::TBaseText<TChar, 2> BinText(const TBasicStringBuf<TChar> value) { + return ::NFormatPrivate::TBaseText<TChar, 2>(value); +} + +/** + * Output manipulator for printing `TDuration` values. + * * When written into a `IOutputStream`, writes out the provided `TDuration` - * in auto-adjusted human-readable format. - * - * Example usage: - * @code - * stream << HumanReadable(TDuration::MicroSeconds(100)); // Will output "100us" - * stream << HumanReadable(TDuration::Seconds(3672)); // Will output "1h 1m 12s" - * @endcode - * - * @param value Value to output. - */ + * in auto-adjusted human-readable format. + * + * Example usage: + * @code + * stream << HumanReadable(TDuration::MicroSeconds(100)); // Will output "100us" + * stream << HumanReadable(TDuration::Seconds(3672)); // Will output "1h 1m 12s" + * @endcode + * + * @param value Value to output. + */ static constexpr ::NFormatPrivate::THumanReadableDuration HumanReadable(const TDuration duration) noexcept { return ::NFormatPrivate::THumanReadableDuration(duration); } /** - * Output manipulator for writing out human-readable number of elements / memory - * amount in `ls -h` style. - * + * Output manipulator for writing out human-readable number of elements / memory + * amount in `ls -h` style. + * * When written into a `IOutputStream`, writes out the provided unsigned integer - * variable with small precision and a suffix (like 'K', 'M', 'G' for numbers, or - * 'B', 'KiB', 'MiB', 'GiB' for bytes). - * - * For quantities, base 1000 is used. For bytes, base is 1024. - * - * Example usage: - * @code + * variable with small precision and a suffix (like 'K', 'M', 'G' for numbers, or + * 'B', 'KiB', 'MiB', 'GiB' for bytes). + * + * For quantities, base 1000 is used. For bytes, base is 1024. + * + * Example usage: + * @code * stream << HumanReadableSize(1024, SF_QUANTITY); // Will output "1.02K" * stream << HumanReadableSize(1024, SF_BYTES); // Will output "1KiB" * stream << "average usage " << HumanReadableSize(100 / 3., SF_BYTES); // Will output "average usage "33.3B"" - * @endcode - * - * @param value Value to output. - * @param format Format to use. - */ + * @endcode + * + * @param value Value to output. + * @param format Format to use. + */ static constexpr ::NFormatPrivate::THumanReadableSize HumanReadableSize(const double size, ESizeFormat format) noexcept { return {size, format}; } @@ -404,40 +404,40 @@ void Time(IOutputStream& l); void TimeHumanReadable(IOutputStream& l); /** - * Output manipulator for adjusting precision of floating point values. - * + * Output manipulator for adjusting precision of floating point values. + * * When written into a `IOutputStream`, writes out the provided floating point - * variable with given precision. The behavior depends on provided `mode`. - * - * Example usage: - * @code - * stream << Prec(1.2345678901234567, PREC_AUTO); // Will output "1.2345678901234567" - * @endcode - * - * @param value float or double to output. - * @param mode Output mode. - * @param ndigits Number of significant digits (in `PREC_NDIGITS` and `PREC_POINT_DIGITS` mode). - * @see EFloatToStringMode - */ + * variable with given precision. The behavior depends on provided `mode`. + * + * Example usage: + * @code + * stream << Prec(1.2345678901234567, PREC_AUTO); // Will output "1.2345678901234567" + * @endcode + * + * @param value float or double to output. + * @param mode Output mode. + * @param ndigits Number of significant digits (in `PREC_NDIGITS` and `PREC_POINT_DIGITS` mode). + * @see EFloatToStringMode + */ template <typename T> static constexpr ::NFormatPrivate::TFloatPrecision<T> Prec(const T& value, const EFloatToStringMode mode, const int ndigits = 0) noexcept { return {value, mode, ndigits}; } /** - * Output manipulator for adjusting precision of floating point values. - * + * Output manipulator for adjusting precision of floating point values. + * * When written into a `IOutputStream`, writes out the provided floating point - * variable with given precision. The behavior is equivalent to `Prec(value, PREC_NDIGITS, ndigits)`. - * - * Example usage: - * @code - * stream << Prec(1.2345678901234567, 3); // Will output "1.23" - * @endcode - * - * @param value float or double to output. - * @param ndigits Number of significant digits. - */ + * variable with given precision. The behavior is equivalent to `Prec(value, PREC_NDIGITS, ndigits)`. + * + * Example usage: + * @code + * stream << Prec(1.2345678901234567, 3); // Will output "1.23" + * @endcode + * + * @param value float or double to output. + * @param ndigits Number of significant digits. + */ template <typename T> static constexpr ::NFormatPrivate::TFloatPrecision<T> Prec(const T& value, const int ndigits) noexcept { return {value, PREC_NDIGITS, ndigits}; diff --git a/util/stream/format_ut.cpp b/util/stream/format_ut.cpp index 43245aeb48..85fae033c2 100644 --- a/util/stream/format_ut.cpp +++ b/util/stream/format_ut.cpp @@ -9,7 +9,7 @@ Y_UNIT_TEST_SUITE(TOutputStreamFormattingTest) { ss << LeftPad(10, 4, '0'); UNIT_ASSERT_VALUES_EQUAL("0010", ss.Str()); - ss.Clear(); + ss.Clear(); ss << LeftPad(222, 1); UNIT_ASSERT_VALUES_EQUAL("222", ss.Str()); } @@ -19,7 +19,7 @@ Y_UNIT_TEST_SUITE(TOutputStreamFormattingTest) { ss << RightPad("aa", 4); UNIT_ASSERT_VALUES_EQUAL("aa ", ss.Str()); - ss.Clear(); + ss.Clear(); ss << RightPad("aa", 1); UNIT_ASSERT_VALUES_EQUAL("aa", ss.Str()); } @@ -63,24 +63,24 @@ Y_UNIT_TEST_SUITE(TOutputStreamFormattingTest) { Y_UNIT_TEST(TestBin) { UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui32>(2), nullptr)), "10"); UNIT_ASSERT_VALUES_EQUAL(ToString(SBin(static_cast<i32>(-2), nullptr)), "-10"); - UNIT_ASSERT_VALUES_EQUAL(ToString(SBin(static_cast<i32>(-2))), "-0b00000000000000000000000000000010"); - UNIT_ASSERT_VALUES_EQUAL(ToString(SBin(static_cast<i32>(-2), HF_FULL)), "-00000000000000000000000000000010"); + UNIT_ASSERT_VALUES_EQUAL(ToString(SBin(static_cast<i32>(-2))), "-0b00000000000000000000000000000010"); + UNIT_ASSERT_VALUES_EQUAL(ToString(SBin(static_cast<i32>(-2), HF_FULL)), "-00000000000000000000000000000010"); UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui32>(15), nullptr)), "1111"); UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui32>(1))), "0b00000000000000000000000000000001"); - UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui32>(-1))), "0b11111111111111111111111111111111"); - UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<i32>(-1))), "0b11111111111111111111111111111111"); + UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui32>(-1))), "0b11111111111111111111111111111111"); + UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<i32>(-1))), "0b11111111111111111111111111111111"); UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<i32>(-1), nullptr)), "11111111111111111111111111111111"); - UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui32>(256))), "0b00000000000000000000000100000000"); - UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui8>(16))), "0b00010000"); - UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui64>(1234587912357ull))), "0b0000000000000000000000010001111101110011001011001000100010100101"); - } - + UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui32>(256))), "0b00000000000000000000000100000000"); + UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui8>(16))), "0b00010000"); + UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui64>(1234587912357ull))), "0b0000000000000000000000010001111101110011001011001000100010100101"); + } + Y_UNIT_TEST(TestBinText) { UNIT_ASSERT_VALUES_EQUAL(ToString(BinText(TStringBuf("\1"))), "00000001"); UNIT_ASSERT_VALUES_EQUAL(ToString(BinText(TStringBuf("\1\1"))), "00000001 00000001"); UNIT_ASSERT_VALUES_EQUAL(ToString(BinText(TStringBuf("aaa"))), "01100001 01100001 01100001"); - } - + } + Y_UNIT_TEST(TestPrec) { TStringStream ss; ss << Prec(1.2345678901234567, PREC_AUTO); @@ -96,13 +96,13 @@ Y_UNIT_TEST_SUITE(TOutputStreamFormattingTest) { } Y_UNIT_TEST(TestHumanReadableSize1000) { - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(0, SF_QUANTITY)), "0"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(0, SF_QUANTITY)), "0"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1, SF_QUANTITY)), "1"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1000, SF_QUANTITY)), "1K"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1234567, SF_QUANTITY)), "1.23M"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(12345678, SF_QUANTITY)), "12.3M"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(12345678 * 1000ull, SF_QUANTITY)), "12.3G"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1, SF_QUANTITY)), "1"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1000, SF_QUANTITY)), "1K"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1234567, SF_QUANTITY)), "1.23M"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(12345678, SF_QUANTITY)), "12.3M"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(12345678 * 1000ull, SF_QUANTITY)), "12.3G"); UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(-1, SF_QUANTITY)), "-1"); UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(-1000, SF_QUANTITY)), "-1K"); @@ -112,17 +112,17 @@ Y_UNIT_TEST_SUITE(TOutputStreamFormattingTest) { } Y_UNIT_TEST(TestHumanReadableSize1024) { - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(0, SF_BYTES)), "0B"); - - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(100, SF_BYTES)), "100B"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1024, SF_BYTES)), "1KiB"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(2.25 * 1024 * 1024, SF_BYTES)), "2.25MiB"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(2.5 * 1024, SF_BYTES)), "2.5KiB"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(45.3 * 1024, SF_BYTES)), "45.3KiB"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1024 * 1024, SF_BYTES)), "1MiB"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(5 * 1024 * 1024, SF_BYTES)), "5MiB"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1236 * 1024 * 1024, SF_BYTES)), "1.21GiB"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1024ull * 1024 * 1024 * 1024, SF_BYTES)), "1TiB"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(0, SF_BYTES)), "0B"); + + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(100, SF_BYTES)), "100B"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1024, SF_BYTES)), "1KiB"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(2.25 * 1024 * 1024, SF_BYTES)), "2.25MiB"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(2.5 * 1024, SF_BYTES)), "2.5KiB"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(45.3 * 1024, SF_BYTES)), "45.3KiB"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1024 * 1024, SF_BYTES)), "1MiB"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(5 * 1024 * 1024, SF_BYTES)), "5MiB"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1236 * 1024 * 1024, SF_BYTES)), "1.21GiB"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1024ull * 1024 * 1024 * 1024, SF_BYTES)), "1TiB"); UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(100 / 3., SF_BYTES)), "33.3B"); UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(-100, SF_BYTES)), "-100B"); diff --git a/util/stream/holder.h b/util/stream/holder.h index c60a4e510c..edd4e98ea6 100644 --- a/util/stream/holder.h +++ b/util/stream/holder.h @@ -1,44 +1,44 @@ #pragma once #include <util/generic/ptr.h> - + #include <utility> -#include <type_traits> +#include <type_traits> class IInputStream; class IOutputStream; -namespace NPrivate { +namespace NPrivate { template <class Stream, bool isInput = std::is_base_of<IInputStream, Stream>::value> struct TStreamBase { using TType = IInputStream; }; - + template <class Stream> struct TStreamBase<Stream, false> { using TType = IOutputStream; }; - + } - -/** - * An ownership-gaining wrapper for proxy streams. - * - * Example usage: - * \code - * TCountingInput* input = new THoldingStream<TCountingInput>(new TStringInput(s)); - * \encode - * - * In this example, resulting counting input also owns a string input that it - * was constructed on top of. - */ -template <class Base, class StreamBase = typename ::NPrivate::TStreamBase<Base>::TType> + +/** + * An ownership-gaining wrapper for proxy streams. + * + * Example usage: + * \code + * TCountingInput* input = new THoldingStream<TCountingInput>(new TStringInput(s)); + * \encode + * + * In this example, resulting counting input also owns a string input that it + * was constructed on top of. + */ +template <class Base, class StreamBase = typename ::NPrivate::TStreamBase<Base>::TType> class THoldingStream: private THolder<StreamBase>, public Base { public: - template <class... Args> + template <class... Args> inline THoldingStream(THolder<StreamBase> stream, Args&&... args) : THolder<StreamBase>(std::move(stream)) - , Base(this->Get(), std::forward<Args>(args)...) + , Base(this->Get(), std::forward<Args>(args)...) { } }; diff --git a/util/stream/input.cpp b/util/stream/input.cpp index 6e8170f2f9..a0e4ce36b8 100644 --- a/util/stream/input.cpp +++ b/util/stream/input.cpp @@ -1,6 +1,6 @@ #include "input.h" -#include "output.h" -#include "str.h" +#include "output.h" +#include "str.h" #include <util/charset/wide.h> #include <util/memory/tempbuf.h> @@ -21,15 +21,15 @@ size_t IInputStream::DoReadTo(TString& st, char to) { char ch; if (!Read(&ch, 1)) { - return 0; + return 0; } st.clear(); - size_t result = 0; + size_t result = 0; do { ++result; - + if (ch == to) { break; } @@ -37,23 +37,23 @@ size_t IInputStream::DoReadTo(TString& st, char to) { st += ch; } while (Read(&ch, 1)); - return result; + return result; } ui64 IInputStream::DoReadAll(IOutputStream& out) { - TTempBuf buffer; - void* ptr = buffer.Data(); - size_t size = buffer.Size(); - - ui64 result = 0; - while (size_t read = Read(ptr, size)) { - out.Write(ptr, read); - result += read; - } - - return result; -} - + TTempBuf buffer; + void* ptr = buffer.Data(); + size_t size = buffer.Size(); + + ui64 result = 0; + while (size_t read = Read(ptr, size)) { + out.Write(ptr, read); + result += read; + } + + return result; +} + size_t IInputStream::Load(void* buf_in, size_t len) { char* buf = (char*)buf_in; @@ -79,24 +79,24 @@ void IInputStream::LoadOrFail(void* buf, size_t len) { } size_t IInputStream::ReadLine(TString& st) { - const size_t ret = ReadTo(st, '\n'); + const size_t ret = ReadTo(st, '\n'); if (ret && !st.empty() && st.back() == '\r') { st.pop_back(); } - return ret; + return ret; } size_t IInputStream::ReadLine(TUtf16String& w) { TString s; - size_t result = ReadLine(s); + size_t result = ReadLine(s); - if (result) { - UTF8ToWide(s, w); - } - - return result; + if (result) { + UTF8ToWide(s, w); + } + + return result; } TString IInputStream::ReadLine() { @@ -147,21 +147,21 @@ size_t IInputStream::DoSkip(size_t sz) { TString IInputStream::ReadAll() { TString result; - TStringOutput stream(result); - - DoReadAll(stream); + TStringOutput stream(result); - return result; -} + DoReadAll(stream); + + return result; +} ui64 IInputStream::ReadAll(IOutputStream& out) { - return DoReadAll(out); + return DoReadAll(out); } ui64 TransferData(IInputStream* in, IOutputStream* out) { - return in->ReadAll(*out); -} - + return in->ReadAll(*out); +} + namespace { struct TStdIn: public IInputStream { ~TStdIn() override = default; diff --git a/util/stream/input.h b/util/stream/input.h index f0d5807ed2..afedc6fb29 100644 --- a/util/stream/input.h +++ b/util/stream/input.h @@ -5,15 +5,15 @@ #include <util/system/defaults.h> class IOutputStream; - -/** - * @addtogroup Streams_Base - * @{ - */ - -/** - * Abstract input stream. - */ + +/** + * @addtogroup Streams_Base + * @{ + */ + +/** + * Abstract input stream. + */ class IInputStream: public TNonCopyable { public: IInputStream() noexcept; @@ -26,16 +26,16 @@ public: return *this; } - /** - * Reads some data from the stream. Note that this function might read less - * data than what was requested. Use `Load` function if you want to read as - * much data as possible. - * - * @param buf Buffer to read into. - * @param len Number of bytes to read. - * @returns Number of bytes that were actually read. - * A return value of zero signals end of stream. - */ + /** + * Reads some data from the stream. Note that this function might read less + * data than what was requested. Use `Load` function if you want to read as + * much data as possible. + * + * @param buf Buffer to read into. + * @param len Number of bytes to read. + * @returns Number of bytes that were actually read. + * A return value of zero signals end of stream. + */ inline size_t Read(void* buf, size_t len) { if (len == 0) { return 0; @@ -44,230 +44,230 @@ public: return DoRead(buf, len); } - /** - * Reads one character from the stream. - * - * @param[out] c Character to read. - * @returns Whether the character was read. - * A return value of false signals the end - * of stream. - */ + /** + * Reads one character from the stream. + * + * @param[out] c Character to read. + * @returns Whether the character was read. + * A return value of false signals the end + * of stream. + */ inline bool ReadChar(char& c) { return DoRead(&c, 1) > 0; } - /** - * Reads all characters from the stream until the given character is - * encountered, and stores them into the given string. The character itself - * is read from the stream, but not stored in the string. - * - * @param[out] st String to read into. - * @param ch Character to stop at. - * @returns Total number of characters read from the stream. - * A return value of zero signals end of stream. - */ + /** + * Reads all characters from the stream until the given character is + * encountered, and stores them into the given string. The character itself + * is read from the stream, but not stored in the string. + * + * @param[out] st String to read into. + * @param ch Character to stop at. + * @returns Total number of characters read from the stream. + * A return value of zero signals end of stream. + */ inline size_t ReadTo(TString& st, char ch) { return DoReadTo(st, ch); } - /** - * Reads the requested amount of data from the stream. Unlike `Read`, this - * function stops only when the requested amount of data is read, or when - * end of stream is reached. - * - * @param buf Buffer to read into. - * @param len Number of bytes to read. - * @returns Number of bytes that were actually read. - * A return value different from `len` - * signals end of stream. - */ + /** + * Reads the requested amount of data from the stream. Unlike `Read`, this + * function stops only when the requested amount of data is read, or when + * end of stream is reached. + * + * @param buf Buffer to read into. + * @param len Number of bytes to read. + * @returns Number of bytes that were actually read. + * A return value different from `len` + * signals end of stream. + */ size_t Load(void* buf, size_t len); - /** - * Reads the requested amount of data from the stream, or fails with an - * exception if unable to do so. - * - * @param buf Buffer to read into. - * @param len Number of bytes to read. - * @see Load - */ + /** + * Reads the requested amount of data from the stream, or fails with an + * exception if unable to do so. + * + * @param buf Buffer to read into. + * @param len Number of bytes to read. + * @see Load + */ void LoadOrFail(void* buf, size_t len); - /** - * Reads all data from this stream and returns it as a string. - * - * @returns Contents of this stream as a string. - */ + /** + * Reads all data from this stream and returns it as a string. + * + * @returns Contents of this stream as a string. + */ TString ReadAll(); - /** - * Reads all data from this stream and writes it into a provided output - * stream. - * - * @param out Output stream to use. - * @returns Total number of characters read from the stream. - */ + /** + * Reads all data from this stream and writes it into a provided output + * stream. + * + * @param out Output stream to use. + * @returns Total number of characters read from the stream. + */ ui64 ReadAll(IOutputStream& out); - - /** - * Reads all data from the stream until the first occurrence of '\n'. Also - * handles Windows line breaks correctly. - * - * @returns Next line read from this stream, - * excluding the line terminator. - * @throws yexception If no data could be read from a stream - * because end of stream has already been - * reached. - */ + + /** + * Reads all data from the stream until the first occurrence of '\n'. Also + * handles Windows line breaks correctly. + * + * @returns Next line read from this stream, + * excluding the line terminator. + * @throws yexception If no data could be read from a stream + * because end of stream has already been + * reached. + */ TString ReadLine(); - /** - * Reads all characters from the stream until the given character is - * encountered and returns them as a string. The character itself is read - * from the stream, but not stored in the string. - * - * @param ch Character to stop at. - * @returns String containing all the characters read. - * @throws yexception If no data could be read from a stream - * because end of stream has already been - * reached. - */ + /** + * Reads all characters from the stream until the given character is + * encountered and returns them as a string. The character itself is read + * from the stream, but not stored in the string. + * + * @param ch Character to stop at. + * @returns String containing all the characters read. + * @throws yexception If no data could be read from a stream + * because end of stream has already been + * reached. + */ TString ReadTo(char ch); - /** - * Reads all data from the stream until the first occurrence of '\n' and - * stores it into provided string. Also handles Windows line breaks correctly. - * - * @param[out] st String to store read characters into, - * excluding the line terminator. - * @returns Total number of characters read from the stream. - * A return value of zero signals end of stream. - */ + /** + * Reads all data from the stream until the first occurrence of '\n' and + * stores it into provided string. Also handles Windows line breaks correctly. + * + * @param[out] st String to store read characters into, + * excluding the line terminator. + * @returns Total number of characters read from the stream. + * A return value of zero signals end of stream. + */ size_t ReadLine(TString& st); - /** - * Reads UTF8 encoded characters from the stream the first occurrence of '\n', - * converts them into wide ones, and stores into provided string. Also handles - * Windows line breaks correctly. - * - * @param[out] w Wide string to store read characters into, - * excluding the line terminator. - * @returns Total number of characters read from the stream. - * A return value of zero signals end of stream. - */ + /** + * Reads UTF8 encoded characters from the stream the first occurrence of '\n', + * converts them into wide ones, and stores into provided string. Also handles + * Windows line breaks correctly. + * + * @param[out] w Wide string to store read characters into, + * excluding the line terminator. + * @returns Total number of characters read from the stream. + * A return value of zero signals end of stream. + */ size_t ReadLine(TUtf16String& w); - /** - * Skips some data from the stream without reading / copying it. Note that - * this function might skip less data than what was requested. - * - * @param len Number of bytes to skip. - * @returns Number of bytes that were actually skipped. - * A return value of zero signals end of stream. - */ + /** + * Skips some data from the stream without reading / copying it. Note that + * this function might skip less data than what was requested. + * + * @param len Number of bytes to skip. + * @returns Number of bytes that were actually skipped. + * A return value of zero signals end of stream. + */ size_t Skip(size_t len); protected: /** - * Reads some data from the stream. Might read less data than what was - * requested. - * - * @param buf Buffer to read into. - * @param len Number of bytes to read. - * @returns Number of bytes that were actually read. - * A return value of zero signals end of stream. - * @throws yexception If IO error occurs. - */ + * Reads some data from the stream. Might read less data than what was + * requested. + * + * @param buf Buffer to read into. + * @param len Number of bytes to read. + * @returns Number of bytes that were actually read. + * A return value of zero signals end of stream. + * @throws yexception If IO error occurs. + */ virtual size_t DoRead(void* buf, size_t len) = 0; - + /** - * Skips some data from the stream. Might skip less data than what was - * requested. - * - * @param len Number of bytes to skip. - * @returns Number of bytes that were actually skipped. - * A return value of zero signals end of stream. - * @throws yexception If IO error occurs. - */ + * Skips some data from the stream. Might skip less data than what was + * requested. + * + * @param len Number of bytes to skip. + * @returns Number of bytes that were actually skipped. + * A return value of zero signals end of stream. + * @throws yexception If IO error occurs. + */ virtual size_t DoSkip(size_t len); - + /** - * Reads all characters from the stream until the given character is - * encountered, and stores them into the given string. The character itself - * is read from the stream, but not stored in the string. - * - * Provided string is cleared only if there is data in the stream. - * - * @param[out] st String to read into. - * @param ch Character to stop at. - * @returns Total number of characters read from the stream. - * A return value of zero signals end of stream. - * @throws yexception If IO error occurs. - */ + * Reads all characters from the stream until the given character is + * encountered, and stores them into the given string. The character itself + * is read from the stream, but not stored in the string. + * + * Provided string is cleared only if there is data in the stream. + * + * @param[out] st String to read into. + * @param ch Character to stop at. + * @returns Total number of characters read from the stream. + * A return value of zero signals end of stream. + * @throws yexception If IO error occurs. + */ virtual size_t DoReadTo(TString& st, char ch); - - /** - * Reads all data from this stream and writes it into a provided output - * stream. - * - * @param out Output stream to use. - * @returns Total number of characters read from - * this stream. - * @throws yexception If IO error occurs. - */ + + /** + * Reads all data from this stream and writes it into a provided output + * stream. + * + * @param out Output stream to use. + * @returns Total number of characters read from + * this stream. + * @throws yexception If IO error occurs. + */ virtual ui64 DoReadAll(IOutputStream& out); }; -/** - * Transfers all data from the given input stream into the given output stream. - * - * @param in Input stream. - * @param out Output stream. - */ +/** + * Transfers all data from the given input stream into the given output stream. + * + * @param in Input stream. + * @param out Output stream. + */ ui64 TransferData(IInputStream* in, IOutputStream* out); - -/** + +/** * `operator>>` for `IInputStream` by default delegates to this function. - * - * Note that while `operator>>` uses overloading (and thus argument-dependent - * lookup), `In` uses template specializations. This makes it possible to - * have a single `In` declaration, and then just provide specializations in - * cpp files, letting the linker figure everything else out. This approach - * reduces compilation times. - * - * However, if the flexibility of overload resolution is needed, then one should - * just overload `operator>>`. - * - * @param in Input stream to read from. - * @param[out] value Value to read. - * @throws `yexception` on invalid input or end of stream. + * + * Note that while `operator>>` uses overloading (and thus argument-dependent + * lookup), `In` uses template specializations. This makes it possible to + * have a single `In` declaration, and then just provide specializations in + * cpp files, letting the linker figure everything else out. This approach + * reduces compilation times. + * + * However, if the flexibility of overload resolution is needed, then one should + * just overload `operator>>`. + * + * @param in Input stream to read from. + * @param[out] value Value to read. + * @throws `yexception` on invalid input or end of stream. * @see Out(IOutputStream&, T&) - */ + */ template <typename T> void In(IInputStream& in, T& value); -/** - * Reads a value from the stream. - * - * @param in Input stream to read from. - * @param[out] value Value to read. - * @returns Input stream. - * @throws `yexception` on invalid input or end of stream. +/** + * Reads a value from the stream. + * + * @param in Input stream to read from. + * @param[out] value Value to read. + * @returns Input stream. + * @throws `yexception` on invalid input or end of stream. * @see operator<<(IOutputStream&, T&) - */ + */ template <typename T> inline IInputStream& operator>>(IInputStream& in, T& value) { - In<T>(in, value); + In<T>(in, value); return in; } -namespace NPrivate { +namespace NPrivate { IInputStream& StdInStream() noexcept; -} - -/** - * Standard input stream. - */ -#define Cin (::NPrivate::StdInStream()) +} + +/** + * Standard input stream. + */ +#define Cin (::NPrivate::StdInStream()) -/** @} */ +/** @} */ diff --git a/util/stream/input_ut.cpp b/util/stream/input_ut.cpp index 4a93f5458e..ba5c1068ee 100644 --- a/util/stream/input_ut.cpp +++ b/util/stream/input_ut.cpp @@ -1,11 +1,11 @@ -#include "input.h" -#include "output.h" - +#include "input.h" +#include "output.h" + #include <library/cpp/testing/unittest/registar.h> - + #include <util/system/file.h> -#include <util/system/yassert.h> - +#include <util/system/yassert.h> + #ifdef _win_ #include <io.h> #endif @@ -42,94 +42,94 @@ private: }; class TNoInput: public IInputStream { -public: +public: TNoInput(ui64 size) : Size_(size) { } - -protected: + +protected: size_t DoRead(void*, size_t len) override { - len = Min(static_cast<ui64>(len), Size_); - Size_ -= len; - return len; - } - -private: - ui64 Size_; -}; - + len = Min(static_cast<ui64>(len), Size_); + Size_ -= len; + return len; + } + +private: + ui64 Size_; +}; + class TNoOutput: public IOutputStream { -public: +public: TNoOutput() = default; - -protected: + +protected: void DoWrite(const void*, size_t) override { } -}; - +}; + class TSimpleStringInput: public IInputStream { -public: +public: TSimpleStringInput(const TString& string) : String_(string) { } - -protected: + +protected: size_t DoRead(void* buf, size_t len) override { Y_ASSERT(len != 0); - + if (String_.empty()) { - return 0; + return 0; } - - *static_cast<char*>(buf) = String_[0]; - String_.remove(0, 1); - return 1; - } - -private: + + *static_cast<char*>(buf) = String_[0]; + String_.remove(0, 1); + return 1; + } + +private: TString String_; -}; - +}; + Y_UNIT_TEST_SUITE(TInputTest) { Y_UNIT_TEST(BigTransfer) { - ui64 size = 1024ull * 1024ull * 1024ull * 5; - TNoInput input(size); - TNoOutput output; - - ui64 transferred = TransferData(&input, &output); - - UNIT_ASSERT_VALUES_EQUAL(transferred, size); - } - + ui64 size = 1024ull * 1024ull * 1024ull * 5; + TNoInput input(size); + TNoOutput output; + + ui64 transferred = TransferData(&input, &output); + + UNIT_ASSERT_VALUES_EQUAL(transferred, size); + } + Y_UNIT_TEST(TestReadTo) { - /* This one tests default implementation of ReadTo. */ - - TSimpleStringInput in("0123456789abc"); - + /* This one tests default implementation of ReadTo. */ + + TSimpleStringInput in("0123456789abc"); + TString t; - UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8); - UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); - UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 5); - UNIT_ASSERT_VALUES_EQUAL(t, "89abc"); - UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 0); - UNIT_ASSERT_VALUES_EQUAL(t, "89abc"); - } - + UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8); + UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); + UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 5); + UNIT_ASSERT_VALUES_EQUAL(t, "89abc"); + UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 0); + UNIT_ASSERT_VALUES_EQUAL(t, "89abc"); + } + Y_UNIT_TEST(TestReadLine) { - TSimpleStringInput in("1\n22\n333"); - + TSimpleStringInput in("1\n22\n333"); + TString t; - UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 2); - UNIT_ASSERT_VALUES_EQUAL(t, "1"); - UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 3); - UNIT_ASSERT_VALUES_EQUAL(t, "22"); - UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 3); - UNIT_ASSERT_VALUES_EQUAL(t, "333"); - UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 0); - UNIT_ASSERT_VALUES_EQUAL(t, "333"); - } + UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 2); + UNIT_ASSERT_VALUES_EQUAL(t, "1"); + UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 3); + UNIT_ASSERT_VALUES_EQUAL(t, "22"); + UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 3); + UNIT_ASSERT_VALUES_EQUAL(t, "333"); + UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 0); + UNIT_ASSERT_VALUES_EQUAL(t, "333"); + } Y_UNIT_TEST(TestStdInReadTo) { std::pair<std::pair<TStringBuf, char>, TStringBuf> testPairs[] = { @@ -154,4 +154,4 @@ Y_UNIT_TEST_SUITE(TInputTest) { }); } } -} +} diff --git a/util/stream/labeled.h b/util/stream/labeled.h index 2cc539d241..a4edffd715 100644 --- a/util/stream/labeled.h +++ b/util/stream/labeled.h @@ -2,18 +2,18 @@ #include <util/generic/va_args.h> -/** - * Generates an output sequence for the provided expressions that is formatted - * as a labeled comma-separated list. - * - * Example usage: - * @code - * int a = 1, b = 2, c = 3; - * stream << LabeledOutput(a, b, c, a + b + c); - * // Outputs "a = 1, b = 2, c = 3, a + b + c = 6" - * @endcode - */ +/** + * Generates an output sequence for the provided expressions that is formatted + * as a labeled comma-separated list. + * + * Example usage: + * @code + * int a = 1, b = 2, c = 3; + * stream << LabeledOutput(a, b, c, a + b + c); + * // Outputs "a = 1, b = 2, c = 3, a + b + c = 6" + * @endcode + */ #define LabeledOutput(...) "" Y_PASS_VA_ARGS(Y_MAP_ARGS_WITH_LAST(__LABELED_OUTPUT_NONLAST__, __LABELED_OUTPUT_IMPL__, __VA_ARGS__)) - + #define __LABELED_OUTPUT_IMPL__(x) << #x " = " << (x) #define __LABELED_OUTPUT_NONLAST__(x) __LABELED_OUTPUT_IMPL__(x) << ", " diff --git a/util/stream/length.cpp b/util/stream/length.cpp index 9907fe2ac9..1ab3c6aadf 100644 --- a/util/stream/length.cpp +++ b/util/stream/length.cpp @@ -27,21 +27,21 @@ size_t TCountingInput::DoSkip(size_t len) { Count_ += ret; return ret; } - + size_t TCountingInput::DoReadTo(TString& st, char ch) { - const size_t ret = Slave_->ReadTo(st, ch); - Count_ += ret; - return ret; -} - + const size_t ret = Slave_->ReadTo(st, ch); + Count_ += ret; + return ret; +} + ui64 TCountingInput::DoReadAll(IOutputStream& out) { - const ui64 ret = Slave_->ReadAll(out); - Count_ += ret; - return ret; -} - -void TCountingOutput::DoWrite(const void* buf, size_t len) { - Slave_->Write(buf, len); - - Count_ += len; -} + const ui64 ret = Slave_->ReadAll(out); + Count_ += ret; + return ret; +} + +void TCountingOutput::DoWrite(const void* buf, size_t len) { + Slave_->Write(buf, len); + + Count_ += len; +} diff --git a/util/stream/length.h b/util/stream/length.h index 4d508ae24d..049b91c05b 100644 --- a/util/stream/length.h +++ b/util/stream/length.h @@ -1,17 +1,17 @@ #pragma once #include "input.h" -#include "output.h" +#include "output.h" #include <util/generic/utility.h> -/** - * Proxy input stream that can read a limited number of characters from a slave - * stream. - * - * This can be useful for breaking up the slave stream into small chunks and - * treat these as separate streams. - */ +/** + * Proxy input stream that can read a limited number of characters from a slave + * stream. + * + * This can be useful for breaking up the slave stream into small chunks and + * treat these as separate streams. + */ class TLengthLimitedInput: public IInputStream { public: inline TLengthLimitedInput(IInputStream* slave, ui64 length) noexcept @@ -35,9 +35,9 @@ private: ui64 Length_; }; -/** - * Proxy input stream that counts the number of characters read. - */ +/** + * Proxy input stream that counts the number of characters read. + */ class TCountingInput: public IInputStream { public: inline TCountingInput(IInputStream* slave) noexcept @@ -48,10 +48,10 @@ public: ~TCountingInput() override = default; - /** - * \returns The total number of characters read from - * this stream. - */ + /** + * \returns The total number of characters read from + * this stream. + */ inline ui64 Counter() const noexcept { return Count_; } @@ -66,35 +66,35 @@ private: IInputStream* Slave_; ui64 Count_; }; - -/** - * Proxy output stream that counts the number of characters written. - */ + +/** + * Proxy output stream that counts the number of characters written. + */ class TCountingOutput: public IOutputStream { -public: +public: inline TCountingOutput(IOutputStream* slave) noexcept : Slave_(slave) , Count_() { - } - + } + ~TCountingOutput() override = default; - + TCountingOutput(TCountingOutput&&) noexcept = default; TCountingOutput& operator=(TCountingOutput&&) noexcept = default; - /** - * \returns The total number of characters written - * into this stream. - */ + /** + * \returns The total number of characters written + * into this stream. + */ inline ui64 Counter() const noexcept { - return Count_; - } - -private: + return Count_; + } + +private: void DoWrite(const void* buf, size_t len) override; - -private: + +private: IOutputStream* Slave_; - ui64 Count_; -}; + ui64 Count_; +}; diff --git a/util/stream/length_ut.cpp b/util/stream/length_ut.cpp index 8968448954..beecc0f4be 100644 --- a/util/stream/length_ut.cpp +++ b/util/stream/length_ut.cpp @@ -1,52 +1,52 @@ -#include "length.h" - +#include "length.h" + #include <library/cpp/testing/unittest/registar.h> - + #include <util/generic/string.h> - + Y_UNIT_TEST_SUITE(TestLengthIO) { Y_UNIT_TEST(TestLengthLimitedInput) { - char buf[16]; - - TStringStream s1("abcd"); - TLengthLimitedInput l1(&s1, 2); - UNIT_ASSERT_VALUES_EQUAL(l1.Load(buf, 3), 2); - UNIT_ASSERT_VALUES_EQUAL(l1.Read(buf, 1), 0); - } - + char buf[16]; + + TStringStream s1("abcd"); + TLengthLimitedInput l1(&s1, 2); + UNIT_ASSERT_VALUES_EQUAL(l1.Load(buf, 3), 2); + UNIT_ASSERT_VALUES_EQUAL(l1.Read(buf, 1), 0); + } + Y_UNIT_TEST(TestCountingInput) { - char buf[16]; - - TStringStream s1("abc\ndef\n"); - TCountingInput l1(&s1); - + char buf[16]; + + TStringStream s1("abc\ndef\n"); + TCountingInput l1(&s1); + TString s; - l1.ReadLine(s); - UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 4); - - l1.Load(buf, 1); - UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 5); - - l1.Skip(1); - UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 6); - - l1.ReadLine(s); - UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 8); - } - + l1.ReadLine(s); + UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 4); + + l1.Load(buf, 1); + UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 5); + + l1.Skip(1); + UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 6); + + l1.ReadLine(s); + UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 8); + } + Y_UNIT_TEST(TestCountingOutput) { - TStringStream s1; - TCountingOutput l1(&s1); - - l1.Write('1'); - UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 1); - + TStringStream s1; + TCountingOutput l1(&s1); + + l1.Write('1'); + UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 1); + l1.Write(TString("abcd")); - UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 5); - + UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 5); + TString buf("aaa"); IOutputStream::TPart parts[] = {{buf.data(), buf.size()}, {buf.data(), buf.size()}, {buf.data(), buf.size()}}; - l1.Write(parts, 3); - UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 14); - } -} + l1.Write(parts, 3); + UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 14); + } +} diff --git a/util/stream/mem.cpp b/util/stream/mem.cpp index 22a3339e27..c2c34dcc59 100644 --- a/util/stream/mem.cpp +++ b/util/stream/mem.cpp @@ -22,13 +22,13 @@ TMemoryInput::TMemoryInput(const TStringBuf buf) noexcept 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; +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) { diff --git a/util/stream/mem.h b/util/stream/mem.h index 18a5d46772..faed25c5f8 100644 --- a/util/stream/mem.h +++ b/util/stream/mem.h @@ -5,26 +5,26 @@ #include <util/generic/strbuf.h> -/** - * @addtogroup Streams_Memory - * @{ - */ - -/** - * Input stream that reads data from a memory block. - */ +/** + * @addtogroup Streams_Memory + * @{ + */ + +/** + * Input stream that reads data from a memory block. + */ class TMemoryInput: public IZeroCopyInputFastReadTo { public: TMemoryInput() noexcept; - /** - * Constructs a stream that reads from the provided memory block. It's up - * to the user to make sure that the memory block doesn't get freed while - * this stream is in use. - * - * @param buf Memory block to use. - * @param len Size of the memory block. - */ + /** + * Constructs a stream that reads from the provided memory block. It's up + * to the user to make sure that the memory block doesn't get freed while + * this stream is in use. + * + * @param buf Memory block to use. + * @param len Size of the memory block. + */ TMemoryInput(const void* buf, size_t len) noexcept; explicit TMemoryInput(const TStringBuf buf) noexcept; ~TMemoryInput() override; @@ -48,50 +48,50 @@ public: TMemoryInput(TMemoryInput&&) noexcept = default; TMemoryInput& operator=(TMemoryInput&&) noexcept = default; - /** - * Initializes this stream with a new memory block. It's up to the - * user to make sure that the memory block doesn't get freed while this - * stream is in use. - * - * @param buf New memory block to use. - * @param len Size of the new memory block. - */ + /** + * Initializes this stream with a new memory block. It's up to the + * user to make sure that the memory block doesn't get freed while this + * stream is in use. + * + * @param buf New memory block to use. + * @param len Size of the new memory block. + */ void Reset(const void* buf, size_t len) noexcept { Buf_ = (const char*)buf; Len_ = len; } - /** - * @returns Whether there is more data in the stream. - */ + /** + * @returns Whether there is more data in the stream. + */ bool Exhausted() const noexcept { return !Avail(); } - /** - * @returns Number of bytes available in the stream. - */ + /** + * @returns Number of bytes available in the stream. + */ size_t Avail() const noexcept { return Len_; } - /** - * @returns Current read position in the memory block - * used by this stream. - */ + /** + * @returns Current read position in the memory block + * used by this stream. + */ const char* Buf() const noexcept { return Buf_; } - /** - * Initializes this stream with a next chunk extracted from the given zero - * copy stream. - * - * @param stream Zero copy stream to initialize from. - */ + /** + * Initializes this stream with a next chunk extracted from the given zero + * copy stream. + * + * @param stream Zero copy stream to initialize from. + */ void Fill(IZeroCopyInput* stream) { - Len_ = stream->Next(&Buf_); - if (!Len_) { + Len_ = stream->Next(&Buf_); + if (!Len_) { Reset(nullptr, 0); } } @@ -105,19 +105,19 @@ private: size_t Len_; }; -/** - * Output stream that writes data to a memory block. - */ +/** + * Output stream that writes data to a memory block. + */ class TMemoryOutput: public IZeroCopyOutput { public: - /** - * Constructs a stream that writes to the provided memory block. It's up - * to the user to make sure that the memory block doesn't get freed while - * this stream is in use. - * - * @param buf Memory block to use. - * @param len Size of the memory block. - */ + /** + * Constructs a stream that writes to the provided memory block. It's up + * to the user to make sure that the memory block doesn't get freed while + * this stream is in use. + * + * @param buf Memory block to use. + * @param len Size of the memory block. + */ TMemoryOutput(void* buf, size_t len) noexcept : Buf_(static_cast<char*>(buf)) , End_(Buf_ + len) @@ -128,47 +128,47 @@ public: TMemoryOutput(TMemoryOutput&&) noexcept = default; TMemoryOutput& operator=(TMemoryOutput&&) noexcept = default; - /** - * Initializes this stream with a new memory block. It's up to the - * user to make sure that the memory block doesn't get freed while this - * stream is in use. - * - * @param buf New memory block to use. - * @param len Size of the new memory block. - */ + /** + * Initializes this stream with a new memory block. It's up to the + * user to make sure that the memory block doesn't get freed while this + * stream is in use. + * + * @param buf New memory block to use. + * @param len Size of the new memory block. + */ inline void Reset(void* buf, size_t len) noexcept { Buf_ = static_cast<char*>(buf); End_ = Buf_ + len; } - /** - * @returns Whether there is more space in the - * stream for writing. - */ + /** + * @returns Whether there is more space in the + * stream for writing. + */ inline bool Exhausted() const noexcept { return !Avail(); } - /** - * @returns Number of bytes available for writing - * in the stream. - */ + /** + * @returns Number of bytes available for writing + * in the stream. + */ inline size_t Avail() const noexcept { return End_ - Buf_; } - /** - * @returns Current write position in the memory block - * used by this stream. - */ + /** + * @returns Current write position in the memory block + * used by this stream. + */ inline char* Buf() const noexcept { return Buf_; } - /** - * @returns Pointer to the end of the memory block - * used by this stream. - */ + /** + * @returns Pointer to the end of the memory block + * used by this stream. + */ char* End() const { return End_; } @@ -184,12 +184,12 @@ protected: char* End_; }; -/** - * Memory output stream that supports changing the position of the - * write pointer. - * - * @see TMemoryOutput - */ +/** + * Memory output stream that supports changing the position of the + * write pointer. + * + * @see TMemoryOutput + */ class TMemoryWriteBuffer: public TMemoryOutput { public: TMemoryWriteBuffer(void* buf, size_t len) @@ -211,10 +211,10 @@ public: return Buf() == Beg(); } - /** - * @returns Data that has been written into this - * stream as a string. - */ + /** + * @returns Data that has been written into this + * stream as a string. + */ TStringBuf Str() const { return TStringBuf(Beg(), Buf()); } @@ -223,21 +223,21 @@ public: return Beg_; } - /** - * @param ptr New write position for this stream. - * Must be inside the memory block that - * this stream uses. - */ + /** + * @param ptr New write position for this stream. + * Must be inside the memory block that + * this stream uses. + */ void SetPos(char* ptr) { Y_ASSERT(Beg_ <= ptr); SetPosImpl(ptr); } - /** - * @param pos New write position for this stream, - * relative to the beginning of the memory - * block that this stream uses. - */ + /** + * @param pos New write position for this stream, + * relative to the beginning of the memory + * block that this stream uses. + */ void SetPos(size_t pos) { SetPosImpl(Beg_ + pos); } @@ -252,4 +252,4 @@ protected: char* Beg_; }; -/** @} */ +/** @} */ diff --git a/util/stream/mem_ut.cpp b/util/stream/mem_ut.cpp index f388ae66ac..ba4dfb869b 100644 --- a/util/stream/mem_ut.cpp +++ b/util/stream/mem_ut.cpp @@ -1,18 +1,18 @@ -#include "mem.h" - +#include "mem.h" + #include <library/cpp/testing/unittest/registar.h> - + Y_UNIT_TEST_SUITE(TestMemIO) { Y_UNIT_TEST(TestReadTo) { TString s("0123456789abc"); - TMemoryInput in(s); - + TMemoryInput in(s); + TString t; - UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8); - UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); - UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 5); - UNIT_ASSERT_VALUES_EQUAL(t, "89abc"); - } + UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8); + UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); + UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 5); + UNIT_ASSERT_VALUES_EQUAL(t, "89abc"); + } Y_UNIT_TEST(NextAndUndo) { char buffer[20]; @@ -75,4 +75,4 @@ Y_UNIT_TEST_SUITE(TestMemIO) { const char* const result = "1234567890"; UNIT_ASSERT(0 == memcmp(buffer, result, strlen(result))); } -} +} diff --git a/util/stream/multi.cpp b/util/stream/multi.cpp index b2354298a0..a6d19c71a9 100644 --- a/util/stream/multi.cpp +++ b/util/stream/multi.cpp @@ -21,7 +21,7 @@ size_t TMultiInput::DoRead(void* buf, size_t len) { return C_->Read(buf, len); } - + size_t TMultiInput::DoReadTo(TString& st, char ch) { size_t ret = C_->ReadTo(st, ch); if (ret == st.size() + 1) { // found a symbol, not eof @@ -42,15 +42,15 @@ size_t TMultiInput::DoReadTo(TString& st, char ch) { return ret; } -size_t TMultiInput::DoSkip(size_t len) { - const size_t ret = C_->Skip(len); - - if (ret) { - return ret; - } - - C_ = N_; - N_ = &Cnull; - - return C_->Skip(len); -} +size_t TMultiInput::DoSkip(size_t len) { + const size_t ret = C_->Skip(len); + + if (ret) { + return ret; + } + + C_ = N_; + N_ = &Cnull; + + return C_->Skip(len); +} diff --git a/util/stream/multi.h b/util/stream/multi.h index 8bfd462d99..9c53189add 100644 --- a/util/stream/multi.h +++ b/util/stream/multi.h @@ -2,14 +2,14 @@ #include "input.h" -/** - * @addtogroup Streams_Multi - * @{ - */ - -/** - * A proxy input stream that concatenates two slave streams into one. +/** + * @addtogroup Streams_Multi + * @{ */ + +/** + * A proxy input stream that concatenates two slave streams into one. + */ class TMultiInput: public IInputStream { public: TMultiInput(IInputStream* f, IInputStream* s) noexcept; @@ -29,4 +29,4 @@ private: * See also "util/stream/tee.h" for multi output. */ -/** @} */ +/** @} */ diff --git a/util/stream/null.cpp b/util/stream/null.cpp index 4e8b298145..0e438e3991 100644 --- a/util/stream/null.cpp +++ b/util/stream/null.cpp @@ -11,16 +11,16 @@ TNullInput::TNullInput() noexcept { TNullInput::~TNullInput() = default; -size_t TNullInput::DoRead(void*, size_t) { +size_t TNullInput::DoRead(void*, size_t) { return 0; } -size_t TNullInput::DoSkip(size_t) { - return 0; -} - -size_t TNullInput::DoNext(const void**, size_t) { - return 0; +size_t TNullInput::DoSkip(size_t) { + return 0; +} + +size_t TNullInput::DoNext(const void**, size_t) { + return 0; } TNullOutput::TNullOutput() noexcept = default; diff --git a/util/stream/null.h b/util/stream/null.h index 8c335a9a78..bb97148ac6 100644 --- a/util/stream/null.h +++ b/util/stream/null.h @@ -1,16 +1,16 @@ #pragma once -#include "zerocopy.h" -#include "output.h" - -/** - * @addtogroup Streams - * @{ - */ - -/** - * Null input stream. Does nothing, contains no data. - */ +#include "zerocopy.h" +#include "output.h" + +/** + * @addtogroup Streams + * @{ + */ + +/** + * Null input stream. Does nothing, contains no data. + */ class TNullInput: public IZeroCopyInput { public: TNullInput() noexcept; @@ -22,9 +22,9 @@ private: size_t DoNext(const void** ptr, size_t len) override; }; -/** - * Null output stream. Just ignores whatever is written into it. - */ +/** + * Null output stream. Just ignores whatever is written into it. + */ class TNullOutput: public IOutputStream { public: TNullOutput() noexcept; @@ -37,25 +37,25 @@ private: void DoWrite(const void* buf, size_t len) override; }; -/** - * Null input-output stream. - * - * @see TNullInput - * @see TNullOutput - */ +/** + * Null input-output stream. + * + * @see TNullInput + * @see TNullOutput + */ class TNullIO: public TNullInput, public TNullOutput { public: TNullIO() noexcept; ~TNullIO() override; }; -namespace NPrivate { +namespace NPrivate { TNullIO& StdNullStream() noexcept; -} +} -/** - * Standard null stream. - */ -#define Cnull (::NPrivate::StdNullStream()) +/** + * Standard null stream. + */ +#define Cnull (::NPrivate::StdNullStream()) -/** @} */ +/** @} */ diff --git a/util/stream/output.cpp b/util/stream/output.cpp index db81b81b70..c172c58a25 100644 --- a/util/stream/output.cpp +++ b/util/stream/output.cpp @@ -17,7 +17,7 @@ #include <cerrno> #include <string> -#include <string_view> +#include <string_view> #include <cstdio> #if defined(_win_) @@ -71,14 +71,14 @@ void Out<wchar32>(IOutputStream& o, wchar32 ch) { static void WriteString(IOutputStream& o, const wchar16* w, size_t n) { const size_t buflen = (n * MAX_UTF8_BYTES); // * 4 because the conversion functions can convert unicode character into maximum 4 bytes of UTF8 - TTempBuf buffer(buflen + 1); - char* const data = buffer.Data(); - size_t written = 0; - WideToUTF8(w, n, data, written); - data[written] = 0; - o.Write(data, written); -} - + TTempBuf buffer(buflen + 1); + char* const data = buffer.Data(); + size_t written = 0; + WideToUTF8(w, n, data, written); + data[written] = 0; + o.Write(data, written); +} + static void WriteString(IOutputStream& o, const wchar32* w, size_t n) { const size_t buflen = (n * MAX_UTF8_BYTES); // * 4 because the conversion functions can convert unicode character into maximum 4 bytes of UTF8 TTempBuf buffer(buflen + 1); @@ -100,11 +100,11 @@ void Out<std::string>(IOutputStream& o, const std::string& p) { } template <> -void Out<std::string_view>(IOutputStream& o, const std::string_view& p) { - o.Write(p.data(), p.length()); -} - -template <> +void Out<std::string_view>(IOutputStream& o, const std::string_view& p) { + o.Write(p.data(), p.length()); +} + +template <> void Out<std::u16string_view>(IOutputStream& o, const std::u16string_view& p) { WriteString(o, p.data(), p.length()); } @@ -131,14 +131,14 @@ void Out<TUtf32StringBuf>(IOutputStream& o, const TUtf32StringBuf& p) { template <> void Out<const wchar16*>(IOutputStream& o, const wchar16* w) { - if (w) { + if (w) { WriteString(o, w, std::char_traits<wchar16>::length(w)); - } else { - o.Write("(null)"); - } + } else { + o.Write("(null)"); + } } -template <> +template <> void Out<const wchar32*>(IOutputStream& o, const wchar32* w) { if (w) { WriteString(o, w, std::char_traits<wchar32>::length(w)); @@ -149,9 +149,9 @@ void Out<const wchar32*>(IOutputStream& o, const wchar32* w) { template <> void Out<TUtf16String>(IOutputStream& o, const TUtf16String& w) { - WriteString(o, w.c_str(), w.size()); -} - + WriteString(o, w.c_str(), w.size()); +} + template <> void Out<TUtf32String>(IOutputStream& o, const TUtf32String& w) { WriteString(o, w.c_str(), w.size()); @@ -217,7 +217,7 @@ void Out<TBasicCharRef<TString>>(IOutputStream& o, const TBasicCharRef<TString>& template <> void Out<TBasicCharRef<TUtf16String>>(IOutputStream& o, const TBasicCharRef<TUtf16String>& c) { - o << static_cast<wchar16>(c); + o << static_cast<wchar16>(c); } template <> diff --git a/util/stream/output.h b/util/stream/output.h index 00eef50b95..14b82509e6 100644 --- a/util/stream/output.h +++ b/util/stream/output.h @@ -10,19 +10,19 @@ #include <type_traits> -/** - * @addtogroup Streams_Base - * @{ - */ - -/** - * Abstract output stream. - */ +/** + * @addtogroup Streams_Base + * @{ + */ + +/** + * Abstract output stream. + */ class IOutputStream: public TNonCopyable { public: - /** - * Data block for output. - */ + /** + * Data block for output. + */ struct TPart { inline TPart(const void* Buf, size_t Len) noexcept : buf(Buf) @@ -62,34 +62,34 @@ public: return *this; }; - /** - * Writes into this stream. - * - * @param buf Data to write. - * @param len Number of bytes to write. - */ + /** + * Writes into this stream. + * + * @param buf Data to write. + * @param len Number of bytes to write. + */ inline void Write(const void* buf, size_t len) { if (len) { DoWrite(buf, len); } } - /** - * Writes a string into this stream. - * - * @param st String to write. - */ + /** + * Writes a string into this stream. + * + * @param st String to write. + */ inline void Write(const TStringBuf st) { Write(st.data(), st.size()); } - /** - * Writes several data blocks into this stream. - * - * @param parts Pointer to the start of the data blocks - * array. - * @param count Number of data blocks to write. - */ + /** + * Writes several data blocks into this stream. + * + * @param parts Pointer to the start of the data blocks + * array. + * @param count Number of data blocks to write. + */ inline void Write(const TPart* parts, size_t count) { if (count > 1) { DoWriteV(parts, count); @@ -98,56 +98,56 @@ public: } } - /** - * Writes a single character into this stream. - * - * @param ch Character to write. - */ + /** + * Writes a single character into this stream. + * + * @param ch Character to write. + */ inline void Write(char ch) { DoWriteC(ch); } - /** - * Flushes this stream's buffer, if any. - * - * Note that this can also be done with a `Flush` manipulator: - * @code - * stream << "some string" << Flush; - * @endcode - */ + /** + * Flushes this stream's buffer, if any. + * + * Note that this can also be done with a `Flush` manipulator: + * @code + * stream << "some string" << Flush; + * @endcode + */ inline void Flush() { DoFlush(); } - /** - * Flushes and closes this stream. No more data can be written into a stream - * once it's closed. - */ + /** + * Flushes and closes this stream. No more data can be written into a stream + * once it's closed. + */ inline void Finish() { DoFinish(); } protected: - /** - * Writes into this stream. - * - * @param buf Data to write. - * @param len Number of bytes to write. - * @throws yexception If IO error occurs. - */ + /** + * Writes into this stream. + * + * @param buf Data to write. + * @param len Number of bytes to write. + * @throws yexception If IO error occurs. + */ virtual void DoWrite(const void* buf, size_t len) = 0; - - /** - * Writes several data blocks into this stream. - * - * @param parts Pointer to the start of the data blocks - * array. - * @param count Number of data blocks to write. - * @throws yexception If IO error occurs. - */ + + /** + * Writes several data blocks into this stream. + * + * @param parts Pointer to the start of the data blocks + * array. + * @param count Number of data blocks to write. + * @throws yexception If IO error occurs. + */ virtual void DoWriteV(const TPart* parts, size_t count); - - /** + + /** * Writes a single character into this stream. Can be overridden with a faster implementation. * * @param ch Character to write. @@ -155,36 +155,36 @@ protected: virtual void DoWriteC(char ch); /** - * Flushes this stream's buffer, if any. - * - * @throws yexception If IO error occurs. - */ + * Flushes this stream's buffer, if any. + * + * @throws yexception If IO error occurs. + */ virtual void DoFlush(); - - /** - * Flushes and closes this stream. No more data can be written into a stream - * once it's closed. - * - * @throws yexception If IO error occurs. - */ + + /** + * Flushes and closes this stream. No more data can be written into a stream + * once it's closed. + * + * @throws yexception If IO error occurs. + */ virtual void DoFinish(); }; -/** +/** * `operator<<` for `IOutputStream` by default delegates to this function. - * - * Note that while `operator<<` uses overloading (and thus argument-dependent - * lookup), `Out` uses template specializations. This makes it possible to - * have a single `Out` declaration, and then just provide specializations in - * cpp files, letting the linker figure everything else out. This approach - * reduces compilation times. - * - * However, if the flexibility of overload resolution is needed, then one should - * just overload `operator<<`. - * - * @param out Output stream to write into. - * @param value Value to write. - */ + * + * Note that while `operator<<` uses overloading (and thus argument-dependent + * lookup), `Out` uses template specializations. This makes it possible to + * have a single `Out` declaration, and then just provide specializations in + * cpp files, letting the linker figure everything else out. This approach + * reduces compilation times. + * + * However, if the flexibility of overload resolution is needed, then one should + * just overload `operator<<`. + * + * @param out Output stream to write into. + * @param value Value to write. + */ template <class T> void Out(IOutputStream& out, typename TTypeTraits<T>::TFuncParam value); @@ -259,46 +259,46 @@ static inline IOutputStream& operator<<(IOutputStream& o, wchar32* t) { return o; } -namespace NPrivate { +namespace NPrivate { IOutputStream& StdOutStream() noexcept; IOutputStream& StdErrStream() noexcept; -} - -/** - * Standard output stream. - */ -#define Cout (::NPrivate::StdOutStream()) - -/** - * Standard error stream. - */ -#define Cerr (::NPrivate::StdErrStream()) - -/** - * Standard log stream. - */ +} + +/** + * Standard output stream. + */ +#define Cout (::NPrivate::StdOutStream()) + +/** + * Standard error stream. + */ +#define Cerr (::NPrivate::StdErrStream()) + +/** + * Standard log stream. + */ #define Clog Cerr -/** - * End-of-line output manipulator, basically the same as `std::endl`. +/** + * End-of-line output manipulator, basically the same as `std::endl`. */ static inline void Endl(IOutputStream& o) { (o << '\n').Flush(); } -/** - * Flushing stream manipulator, basically the same as `std::flush`. - */ +/** + * Flushing stream manipulator, basically the same as `std::flush`. + */ static inline void Flush(IOutputStream& o) { o.Flush(); } /* - * Also see format.h for additional manipulators. - */ + * Also see format.h for additional manipulators. + */ #include "debug.h" void RedirectStdioToAndroidLog(bool redirect); -/** @} */ +/** @} */ diff --git a/util/stream/pipe.cpp b/util/stream/pipe.cpp index 51be1934a7..f3081acf46 100644 --- a/util/stream/pipe.cpp +++ b/util/stream/pipe.cpp @@ -17,7 +17,7 @@ public: #endif Pipe_ = ::popen(command.data(), mode); if (Pipe_ == nullptr) { - ythrow TSystemError() << "failed to open pipe: " << command.Quote(); + ythrow TSystemError() << "failed to open pipe: " << command.Quote(); } } diff --git a/util/stream/pipe.h b/util/stream/pipe.h index 18525b9517..15ad8fd910 100644 --- a/util/stream/pipe.h +++ b/util/stream/pipe.h @@ -1,29 +1,29 @@ #pragma once -#include "input.h" -#include "output.h" +#include "input.h" +#include "output.h" #include <util/system/pipe.h> #include <util/generic/ptr.h> #include <util/generic/string.h> -/** - * @addtogroup Streams_Pipes - * @{ - */ - -/** - * Base class for starting a process and communicating with it via pipes. - */ +/** + * @addtogroup Streams_Pipes + * @{ + */ + +/** + * Base class for starting a process and communicating with it via pipes. + */ class TPipeBase { protected: - /** - * Starts a new process and opens a pipe. - * - * @param command Command line to start a process with. - * @param mode Data transfer mode for the pipe. Use - * "r" for reading and "w" for writing. - */ + /** + * Starts a new process and opens a pipe. + * + * @param command Command line to start a process with. + * @param mode Data transfer mode for the pipe. Use + * "r" for reading and "w" for writing. + */ TPipeBase(const TString& command, const char* mode); virtual ~TPipeBase(); @@ -32,44 +32,44 @@ protected: THolder<TImpl> Impl_; }; -/** - * Input stream that binds to a standard output stream of a newly started process. - * - * Note that if the process ends with non-zero exit status, `Read` function will - * throw an exception. - */ +/** + * Input stream that binds to a standard output stream of a newly started process. + * + * Note that if the process ends with non-zero exit status, `Read` function will + * throw an exception. + */ class TPipeInput: protected TPipeBase, public IInputStream { public: - /** - * Starts a new process and opens a pipe. - * - * @param command Command line to start a process with. - */ + /** + * Starts a new process and opens a pipe. + * + * @param command Command line to start a process with. + */ TPipeInput(const TString& command); private: size_t DoRead(void* buf, size_t len) override; }; -/** - * Output stream that binds to a standard input stream of a newly started process. - * - * Note that if the process ends with non-zero exit status, `Close` function will - * throw an exception. - */ +/** + * Output stream that binds to a standard input stream of a newly started process. + * + * Note that if the process ends with non-zero exit status, `Close` function will + * throw an exception. + */ class TPipeOutput: protected TPipeBase, public IOutputStream { public: - /** - * Starts a new process and opens a pipe. - * - * @param command Command line to start a process with. - */ + /** + * Starts a new process and opens a pipe. + * + * @param command Command line to start a process with. + */ TPipeOutput(const TString& command); - - /** - * Waits for the process to terminate and throws an exception if it ended - * with a non-zero exit status. - */ + + /** + * Waits for the process to terminate and throws an exception if it ended + * with a non-zero exit status. + */ void Close(); private: @@ -85,9 +85,9 @@ protected: TPipeHandle Handle_; }; -/** - * Input stream that binds to a standard output stream of an existing process. - */ +/** + * Input stream that binds to a standard output stream of an existing process. + */ class TPipedInput: public TPipedBase, public IInputStream { public: TPipedInput(PIPEHANDLE fd); @@ -97,9 +97,9 @@ private: size_t DoRead(void* buf, size_t len) override; }; -/** - * Output stream that binds to a standard input stream of an existing process. - */ +/** + * Output stream that binds to a standard input stream of an existing process. + */ class TPipedOutput: public TPipedBase, public IOutputStream { public: TPipedOutput(PIPEHANDLE fd); @@ -109,4 +109,4 @@ private: void DoWrite(const void* buf, size_t len) override; }; -/** @} */ +/** @} */ diff --git a/util/stream/printf.h b/util/stream/printf.h index 1c7ddc0664..180dc3a93c 100644 --- a/util/stream/printf.h +++ b/util/stream/printf.h @@ -4,22 +4,22 @@ class IOutputStream; -/** - * Stream-based `printf` function. Prints formatted data into the provided stream. - * Works the same way as a standard C `printf`. - * - * @param out Stream to write into. - * @param fmt Format string. - * @param ... Additional arguments. - */ +/** + * Stream-based `printf` function. Prints formatted data into the provided stream. + * Works the same way as a standard C `printf`. + * + * @param out Stream to write into. + * @param fmt Format string. + * @param ... Additional arguments. + */ size_t Y_PRINTF_FORMAT(2, 3) Printf(IOutputStream& out, const char* fmt, ...); - -/** - * Stream-based `vprintf` function. Prints formatted data from variable argument - * list into the provided stream. Works the same way as a standard C `vprintf`. - * - * @param out Stream to write into. - * @param fmt Format string. - * @param params Additional arguments as a variable argument list. - */ + +/** + * Stream-based `vprintf` function. Prints formatted data from variable argument + * list into the provided stream. Works the same way as a standard C `vprintf`. + * + * @param out Stream to write into. + * @param fmt Format string. + * @param params Additional arguments as a variable argument list. + */ size_t Y_PRINTF_FORMAT(2, 0) Printf(IOutputStream& out, const char* fmt, va_list params); diff --git a/util/stream/str.cpp b/util/stream/str.cpp index 13f0e8ef28..36891ea1ed 100644 --- a/util/stream/str.cpp +++ b/util/stream/str.cpp @@ -4,13 +4,13 @@ static constexpr size_t MIN_BUFFER_GROW_SIZE = 16; TStringInput::~TStringInput() = default; -size_t TStringInput::DoNext(const void** ptr, size_t len) { +size_t TStringInput::DoNext(const void** ptr, size_t len) { len = Min(len, S_->size() - Pos_); *ptr = S_->data() + Pos_; - Pos_ += len; - return len; -} - + Pos_ += len; + return len; +} + void TStringInput::DoUndo(size_t len) { Y_VERIFY(len <= Pos_); Pos_ -= len; diff --git a/util/stream/str.h b/util/stream/str.h index 028bd572c0..f00a3bcba7 100644 --- a/util/stream/str.h +++ b/util/stream/str.h @@ -1,34 +1,34 @@ #pragma once -#include "zerocopy.h" +#include "zerocopy.h" #include "zerocopy_output.h" #include <util/generic/string.h> #include <util/generic/noncopyable.h> -#include <util/generic/store_policy.h> +#include <util/generic/store_policy.h> /** - * @addtogroup Streams_Strings - * @{ - */ - -/** - * Input stream for reading data from a string. - */ + * @addtogroup Streams_Strings + * @{ + */ + +/** + * Input stream for reading data from a string. + */ class TStringInput: public IZeroCopyInputFastReadTo { public: - /** - * Constructs a string input stream that reads character data from the - * provided string. + /** + * Constructs a string input stream that reads character data from the + * provided string. + * + * Note that this stream keeps a reference to the provided string, so it's + * up to the user to make sure that the string doesn't get destroyed while + * this stream is in use. + * + * For reading data from `TStringBuf`s, see `TMemoryInput` (`util/stream/mem.h`). * - * Note that this stream keeps a reference to the provided string, so it's - * up to the user to make sure that the string doesn't get destroyed while - * this stream is in use. - * - * For reading data from `TStringBuf`s, see `TMemoryInput` (`util/stream/mem.h`). - * - * @param s String to read from. - */ + * @param s String to read from. + */ inline TStringInput(const TString& s) noexcept : S_(&s) , Pos_(0) @@ -36,7 +36,7 @@ public: } TStringInput(const TString&&) = delete; - + ~TStringInput() override; TStringInput(TStringInput&&) noexcept = default; @@ -47,7 +47,7 @@ public: DoSwap(Pos_, s.Pos_); } -protected: +protected: size_t DoNext(const void** ptr, size_t len) override; void DoUndo(size_t len) override; @@ -59,20 +59,20 @@ private: }; /** - * Stream for writing data into a string. - */ + * Stream for writing data into a string. + */ class TStringOutput: public IZeroCopyOutput { public: - /** + /** * Constructs a string output stream that appends character data to the - * provided string. - * - * Note that this stream keeps a reference to the provided string, so it's - * up to the user to make sure that the string doesn't get destroyed while - * this stream is in use. - * - * @param s String to append to. - */ + * provided string. + * + * Note that this stream keeps a reference to the provided string, so it's + * up to the user to make sure that the string doesn't get destroyed while + * this stream is in use. + * + * @param s String to append to. + */ inline TStringOutput(TString& s) noexcept : S_(&s) { @@ -82,14 +82,14 @@ public: ~TStringOutput() override; - /** - * @param size Number of additional characters to - * reserve in output string. - */ - inline void Reserve(size_t size) { + /** + * @param size Number of additional characters to + * reserve in output string. + */ + inline void Reserve(size_t size) { S_->reserve(S_->size() + size); - } - + } + inline void Swap(TStringOutput& s) noexcept { DoSwap(S_, s.S_); } @@ -104,9 +104,9 @@ private: TString* S_; }; -/** - * String input/output stream, similar to `std::stringstream`. - */ +/** + * String input/output stream, similar to `std::stringstream`. + */ class TStringStream: private TEmbedPolicy<TString>, public TStringInput, public TStringOutput { using TEmbeddedString = TEmbedPolicy<TString>; @@ -124,7 +124,7 @@ public: , TStringOutput(*TEmbeddedString::Ptr()) { } - + inline TStringStream(const TStringStream& other) : TEmbeddedString(other.Str()) , TStringInput(*TEmbeddedString::Ptr()) @@ -134,7 +134,7 @@ public: inline TStringStream& operator=(const TStringStream& other) { // All references remain alive, we need to change position only - Str() = other.Str(); + Str() = other.Str(); Pos_ = other.Pos_; return *this; @@ -142,7 +142,7 @@ public: ~TStringStream() override; - /** + /** * @returns Whether @c this contains any data */ explicit operator bool() const noexcept { @@ -150,66 +150,66 @@ public: } /** - * @returns String that this stream is writing into. + * @returns String that this stream is writing into. */ inline TString& Str() noexcept { - return *Ptr(); + return *Ptr(); } - /** - * @returns String that this stream is writing into. - */ + /** + * @returns String that this stream is writing into. + */ inline const TString& Str() const noexcept { - return *Ptr(); - } - - /** - * @returns Pointer to the character data contained - * in this stream. The data is guaranteed - * to be null-terminated. - */ + return *Ptr(); + } + + /** + * @returns Pointer to the character data contained + * in this stream. The data is guaranteed + * to be null-terminated. + */ inline const char* Data() const noexcept { return Ptr()->data(); - } - - /** - * @returns Total number of characters in this - * stream. Note that this is not the same - * as the total number of characters - * available for reading. - */ + } + + /** + * @returns Total number of characters in this + * stream. Note that this is not the same + * as the total number of characters + * available for reading. + */ inline size_t Size() const noexcept { return Ptr()->size(); - } - - /** - * @returns Whether the string that this stream - * operates on is empty. - */ + } + + /** + * @returns Whether the string that this stream + * operates on is empty. + */ Y_PURE_FUNCTION inline bool Empty() const noexcept { - return Str().empty(); - } - - using TStringOutput::Reserve; - - /** - * Clears the string that this stream operates on and resets the - * read/write pointers. - */ - inline void Clear() { - Str().clear(); - Pos_ = 0; - } - - // TODO: compatibility with existing code, remove - + return Str().empty(); + } + + using TStringOutput::Reserve; + + /** + * Clears the string that this stream operates on and resets the + * read/write pointers. + */ + inline void Clear() { + Str().clear(); + Pos_ = 0; + } + + // TODO: compatibility with existing code, remove + Y_PURE_FUNCTION bool empty() const { - return Empty(); - } - - void clear() { - Clear(); - } + return Empty(); + } + + void clear() { + Clear(); + } }; -/** @} */ +/** @} */ diff --git a/util/stream/str_ut.cpp b/util/stream/str_ut.cpp index fc6b46c31a..c688ee1e5c 100644 --- a/util/stream/str_ut.cpp +++ b/util/stream/str_ut.cpp @@ -13,7 +13,7 @@ Y_UNIT_TEST_SUITE(TStringInputOutputTest) { TString result = input.ReadAll(); - UNIT_ASSERT_VALUES_EQUAL(result, str); + UNIT_ASSERT_VALUES_EQUAL(result, str); } Y_UNIT_TEST(ConstRef) { @@ -23,7 +23,7 @@ Y_UNIT_TEST_SUITE(TStringInputOutputTest) { TString result = input.ReadAll(); - UNIT_ASSERT_VALUES_EQUAL(result, str); + UNIT_ASSERT_VALUES_EQUAL(result, str); } Y_UNIT_TEST(NonConstRef) { @@ -33,40 +33,40 @@ Y_UNIT_TEST_SUITE(TStringInputOutputTest) { TString result = input.ReadAll(); - UNIT_ASSERT_VALUES_EQUAL(result, str); + UNIT_ASSERT_VALUES_EQUAL(result, str); } Y_UNIT_TEST(Transfer) { TString inputString = "some_string"; - TStringInput input(inputString); - + TStringInput input(inputString); + TString outputString; - TStringOutput output(outputString); - - TransferData(&input, &output); - - UNIT_ASSERT_VALUES_EQUAL(inputString, outputString); - } - + TStringOutput output(outputString); + + TransferData(&input, &output); + + UNIT_ASSERT_VALUES_EQUAL(inputString, outputString); + } + Y_UNIT_TEST(SkipReadAll) { TString string0 = "All animals are equal, but some animals are more equal than others."; - + TString string1; for (size_t i = 1; i <= string0.size(); i++) { - string1 += string0.substr(0, i); + string1 += string0.substr(0, i); } - - TStringInput input0(string1); - - size_t left = 5; + + TStringInput input0(string1); + + size_t left = 5; while (left > 0) { - left -= input0.Skip(left); + left -= input0.Skip(left); } - + TString string2 = input0.ReadAll(); - - UNIT_ASSERT_VALUES_EQUAL(string2, string1.substr(5)); - } + + UNIT_ASSERT_VALUES_EQUAL(string2, string1.substr(5)); + } Y_UNIT_TEST(OperatorBool) { TStringStream str; @@ -76,17 +76,17 @@ Y_UNIT_TEST_SUITE(TStringInputOutputTest) { str.Clear(); UNIT_ASSERT(!str); } - + Y_UNIT_TEST(TestReadTo) { TString s("0123456789abc"); TString t; - - TStringInput in0(s); - UNIT_ASSERT_VALUES_EQUAL(in0.ReadTo(t, '7'), 8); - UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); - UNIT_ASSERT_VALUES_EQUAL(in0.ReadTo(t, 'z'), 5); - UNIT_ASSERT_VALUES_EQUAL(t, "89abc"); - } + + TStringInput in0(s); + UNIT_ASSERT_VALUES_EQUAL(in0.ReadTo(t, '7'), 8); + UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); + UNIT_ASSERT_VALUES_EQUAL(in0.ReadTo(t, 'z'), 5); + UNIT_ASSERT_VALUES_EQUAL(t, "89abc"); + } Y_UNIT_TEST(WriteViaNextAndUndo) { TString str1; diff --git a/util/stream/tee.h b/util/stream/tee.h index c69e232fb9..52fe3d3fe4 100644 --- a/util/stream/tee.h +++ b/util/stream/tee.h @@ -2,14 +2,14 @@ #include "output.h" -/** - * @addtogroup Streams_Multi - * @{ - */ - -/** - * A proxy output stream that writes into two slave streams simultaneously. - */ +/** + * @addtogroup Streams_Multi + * @{ + */ + +/** + * A proxy output stream that writes into two slave streams simultaneously. + */ class TTeeOutput: public IOutputStream { public: TTeeOutput(IOutputStream* l, IOutputStream* r) noexcept; @@ -25,4 +25,4 @@ private: IOutputStream* R_; }; -/** @} */ +/** @} */ diff --git a/util/stream/tokenizer.h b/util/stream/tokenizer.h index b2398efdd1..fc0f62d84d 100644 --- a/util/stream/tokenizer.h +++ b/util/stream/tokenizer.h @@ -8,21 +8,21 @@ #include <util/system/compiler.h> #include <util/system/yassert.h> -/** - * @addtogroup Streams - * @{ - */ - -/** - * Simple stream tokenizer. Splits the stream into tokens that are available - * via iterator interface. - * - * @tparam TEndOfToken Predicate for token delimiter characters. - * @see TEol +/** + * @addtogroup Streams + * @{ */ + +/** + * Simple stream tokenizer. Splits the stream into tokens that are available + * via iterator interface. + * + * @tparam TEndOfToken Predicate for token delimiter characters. + * @see TEol + */ template <typename TEndOfToken> class TStreamTokenizer { -public: +public: class TIterator { public: inline TIterator(TStreamTokenizer* const parent) @@ -143,15 +143,15 @@ public: Y_ASSERT(blen == Buf_.Capacity()); /* - * do reallocate - */ + * do reallocate + */ Buf_.Reserve(Buf_.Capacity() * 4); CheckBuf(); } else { /* - * do move - */ + * do move + */ MemMove(BufBegin(), Cur_, blen); } @@ -202,8 +202,8 @@ private: TEndOfToken Eot_; }; -/** - * Predicate for `TStreamTokenizer` that uses '\\n' as a delimiter. +/** + * Predicate for `TStreamTokenizer` that uses '\\n' as a delimiter. */ struct TEol { inline bool operator()(char ch) const noexcept { @@ -211,4 +211,4 @@ struct TEol { } }; -/** @} */ +/** @} */ diff --git a/util/stream/trace.h b/util/stream/trace.h index e74b6ecf3e..03cab37a8f 100644 --- a/util/stream/trace.h +++ b/util/stream/trace.h @@ -2,9 +2,9 @@ #include "debug.h" -/** - * Debug level, as set via `DBGOUT` environment variable. - */ +/** + * Debug level, as set via `DBGOUT` environment variable. + */ enum ETraceLevel: ui8 { TRACE_ERR = 1, TRACE_WARN = 2, @@ -22,24 +22,24 @@ enum ETraceLevel: ui8 { #ifdef Y_ENABLE_TRACE /** - * Writes the given data into standard debug stream if current debug level set - * via `DBGOUT` environment variable permits it. - * + * Writes the given data into standard debug stream if current debug level set + * via `DBGOUT` environment variable permits it. + * * Does nothing in release builds unless `Y_ENABLE_TRACE` is defined. - * - * Example usage: - * @code + * + * Example usage: + * @code * Y_DBGTRACE(DEBUG, "Advance from " << node1 << " to " << node2); - * @endcode - * - * @param elevel Debug level of this trace command, e.g. - * `WARN` or `DEBUG`. Basically a suffix of - * one of the values of `ETraceLevel` enum. - * @param args Argument chain to be written out into - * standard debug stream, joined with `<<` - * operator. - * @see ETraceLevel - */ + * @endcode + * + * @param elevel Debug level of this trace command, e.g. + * `WARN` or `DEBUG`. Basically a suffix of + * one of the values of `ETraceLevel` enum. + * @param args Argument chain to be written out into + * standard debug stream, joined with `<<` + * operator. + * @see ETraceLevel + */ #define Y_DBGTRACE(elevel, args) Y_DBGTRACE0(int(TRACE_##elevel), args) #define Y_DBGTRACE0(level, args) \ do \ diff --git a/util/stream/walk.cpp b/util/stream/walk.cpp index 57dc9ab036..8a6404699f 100644 --- a/util/stream/walk.cpp +++ b/util/stream/walk.cpp @@ -5,8 +5,8 @@ void IWalkInput::DoUndo(size_t len) { Len_ += len; Buf_ = static_cast<const char*>(Buf_) - len; -} - +} + size_t IWalkInput::DoNext(const void** ptr, size_t len) { if (!Len_) { Len_ = DoUnboundedNext(&Buf_); diff --git a/util/stream/walk.h b/util/stream/walk.h index 7e62cb44dc..202bada18b 100644 --- a/util/stream/walk.h +++ b/util/stream/walk.h @@ -2,34 +2,34 @@ #include "zerocopy.h" -/** - * Zero-copy stream that simplifies implementation of derived classes. - * - * Derived classes must implement `DoUnboundedNext` method. - */ +/** + * Zero-copy stream that simplifies implementation of derived classes. + * + * Derived classes must implement `DoUnboundedNext` method. + */ class IWalkInput: public IZeroCopyInputFastReadTo { public: IWalkInput() : Buf_(nullptr) - , Len_(0) + , Len_(0) { } -protected: +protected: void DoUndo(size_t len) override; size_t DoNext(const void** ptr, size_t len) override; - - /** - * Returns the next data chunk from this input stream. There are no - * restrictions on the size of the data chunk. - * - * @param ptr[out] Pointer to the start of the data chunk. - * @returns Size of the returned data chunk, in bytes. - * Return value of zero signals end of stream. - */ - virtual size_t DoUnboundedNext(const void** ptr) = 0; - + + /** + * Returns the next data chunk from this input stream. There are no + * restrictions on the size of the data chunk. + * + * @param ptr[out] Pointer to the start of the data chunk. + * @returns Size of the returned data chunk, in bytes. + * Return value of zero signals end of stream. + */ + virtual size_t DoUnboundedNext(const void** ptr) = 0; + private: - const void* Buf_; - size_t Len_; + const void* Buf_; + size_t Len_; }; diff --git a/util/stream/walk_ut.cpp b/util/stream/walk_ut.cpp index e0a783799f..115e249607 100644 --- a/util/stream/walk_ut.cpp +++ b/util/stream/walk_ut.cpp @@ -1,55 +1,55 @@ -#include "walk.h" - +#include "walk.h" + #include <library/cpp/testing/unittest/registar.h> - + class TStringListInput: public IWalkInput { -public: +public: TStringListInput(const TVector<TString>& data) : Data_(data) , Index_(0) { } - -protected: + +protected: size_t DoUnboundedNext(const void** ptr) override { - if (Index_ >= Data_.size()) { - return 0; - } - + if (Index_ >= Data_.size()) { + return 0; + } + const TString& string = Data_[Index_++]; - - *ptr = string.data(); - return string.size(); - } - -private: + + *ptr = string.data(); + return string.size(); + } + +private: const TVector<TString>& Data_; - size_t Index_; -}; - + size_t Index_; +}; + Y_UNIT_TEST_SUITE(TWalkTest) { Y_UNIT_TEST(ReadTo) { TVector<TString> data; - data.push_back("111a"); - data.push_back("222b"); - data.push_back("333c"); - data.push_back("444d"); - data.push_back("555e"); - data.push_back("666f"); - - TStringListInput input(data); - + data.push_back("111a"); + data.push_back("222b"); + data.push_back("333c"); + data.push_back("444d"); + data.push_back("555e"); + data.push_back("666f"); + + TStringListInput input(data); + TString tmp1 = input.ReadTo('c'); - UNIT_ASSERT_VALUES_EQUAL(tmp1, "111a222b333"); - - char tmp2; - input.Read(&tmp2, 1); - UNIT_ASSERT_VALUES_EQUAL(tmp2, '4'); - + UNIT_ASSERT_VALUES_EQUAL(tmp1, "111a222b333"); + + char tmp2; + input.Read(&tmp2, 1); + UNIT_ASSERT_VALUES_EQUAL(tmp2, '4'); + TString tmp3 = input.ReadTo('6'); - UNIT_ASSERT_VALUES_EQUAL(tmp3, "44d555e"); - + UNIT_ASSERT_VALUES_EQUAL(tmp3, "44d555e"); + TString tmp4 = input.ReadAll(); - UNIT_ASSERT_VALUES_EQUAL(tmp4, "66f"); - } -} + UNIT_ASSERT_VALUES_EQUAL(tmp4, "66f"); + } +} diff --git a/util/stream/zerocopy.cpp b/util/stream/zerocopy.cpp index dc2982ad55..4cf08b2aad 100644 --- a/util/stream/zerocopy.cpp +++ b/util/stream/zerocopy.cpp @@ -1,36 +1,36 @@ -#include "zerocopy.h" +#include "zerocopy.h" #include "output.h" IZeroCopyInput::~IZeroCopyInput() = default; size_t IZeroCopyInput::DoRead(void* buf, size_t len) { - const void* ptr; - size_t result = DoNext(&ptr, len); + const void* ptr; + size_t result = DoNext(&ptr, len); if (result) { memcpy(buf, ptr, result); } - return result; -} - + return result; +} + ui64 IZeroCopyInput::DoReadAll(IOutputStream& out) { - ui64 result = 0; + ui64 result = 0; const void* ptr; - - while (size_t len = Next(&ptr)) { - out.Write(ptr, len); - result += len; - } - - return result; -} - + + while (size_t len = Next(&ptr)) { + out.Write(ptr, len); + result += len; + } + + return result; +} + size_t IZeroCopyInput::DoSkip(size_t len) { - const void* ptr; + const void* ptr; - return DoNext(&ptr, len); -} + return DoNext(&ptr, len); +} IZeroCopyInputFastReadTo::~IZeroCopyInputFastReadTo() = default; diff --git a/util/stream/zerocopy.h b/util/stream/zerocopy.h index 3315aa3a51..f55c0e10df 100644 --- a/util/stream/zerocopy.h +++ b/util/stream/zerocopy.h @@ -2,22 +2,22 @@ #include <util/system/yassert.h> #include <util/system/defaults.h> -#include <util/generic/ylimits.h> - -#include "input.h" +#include <util/generic/ylimits.h> +#include "input.h" + class IOutputStream; -/** - * @addtogroup Streams - * @{ - */ - -/** - * Input stream with direct access to the input buffer. - * - * Derived classes must implement `DoNext` method. - */ +/** + * @addtogroup Streams + * @{ + */ + +/** + * Input stream with direct access to the input buffer. + * + * Derived classes must implement `DoNext` method. + */ class IZeroCopyInput: public IInputStream { public: IZeroCopyInput() noexcept = default; @@ -26,22 +26,22 @@ public: IZeroCopyInput(IZeroCopyInput&&) noexcept = default; IZeroCopyInput& operator=(IZeroCopyInput&&) noexcept = default; - /** - * Returns the next data chunk from this input stream. - * - * Note that this function is not guaranteed to return the requested number - * of bytes, even if they are in fact available in the stream. - * - * @param ptr[out] Pointer to the start of the data chunk. - * @param len[in] Maximal size of the data chunk to be returned, in bytes. - * @returns Size of the returned data chunk, in bytes. - * Return value of zero signals end of stream. - */ + /** + * Returns the next data chunk from this input stream. + * + * Note that this function is not guaranteed to return the requested number + * of bytes, even if they are in fact available in the stream. + * + * @param ptr[out] Pointer to the start of the data chunk. + * @param len[in] Maximal size of the data chunk to be returned, in bytes. + * @returns Size of the returned data chunk, in bytes. + * Return value of zero signals end of stream. + */ template <class T> inline size_t Next(T** ptr, size_t len) { Y_ASSERT(ptr); - return DoNext((const void**)ptr, len); + return DoNext((const void**)ptr, len); } template <class T> @@ -49,11 +49,11 @@ public: return Next(ptr, Max<size_t>()); } -protected: +protected: size_t DoRead(void* buf, size_t len) override; size_t DoSkip(size_t len) override; ui64 DoReadAll(IOutputStream& out) override; - virtual size_t DoNext(const void** ptr, size_t len) = 0; + virtual size_t DoNext(const void** ptr, size_t len) = 0; }; /** @@ -88,4 +88,4 @@ private: virtual void DoUndo(size_t len) = 0; }; -/** @} */ +/** @} */ diff --git a/util/stream/zlib.cpp b/util/stream/zlib.cpp index 60f4e9439f..3ccfa625ab 100644 --- a/util/stream/zlib.cpp +++ b/util/stream/zlib.cpp @@ -55,8 +55,8 @@ namespace { template <class P, class T> inline bool Next(P** buf, T* len) { if (!Len) { - Len = In->Next(&Buf); - if (!Len) { + Len = In->Next(&Buf); + if (!Len) { return false; } } @@ -177,7 +177,7 @@ namespace { void* buf = AdditionalData(); *ptr = buf; - return Stream_->Read(buf, Min(len, AdditionalDataLength())); + return Stream_->Read(buf, Min(len, AdditionalDataLength())); } private: @@ -327,7 +327,7 @@ TZLibDecompress::TZLibDecompress(IInputStream* input, ZLib::StreamType type, siz : Impl_(new (buflen) TDecompressStream(input, type, dict)) { } - + void TZLibDecompress::SetAllowMultipleStreams(bool allowMultipleStreams) { Impl_->SetAllowMultipleStreams(allowMultipleStreams); } @@ -338,9 +338,9 @@ size_t TZLibDecompress::DoRead(void* buf, size_t size) { return Impl_->Read(buf, MaxPortion(size)); } -void TZLibCompress::Init(const TParams& params) { +void TZLibCompress::Init(const TParams& params) { Y_ENSURE(params.BufLen >= 16, "ZLib buffer too small"); - Impl_.Reset(new (params.BufLen) TImpl(params)); + Impl_.Reset(new (params.BufLen) TImpl(params)); } void TZLibCompress::TDestruct::Destroy(TImpl* impl) { diff --git a/util/stream/zlib.h b/util/stream/zlib.h index e7de7c81b7..084a99a41b 100644 --- a/util/stream/zlib.h +++ b/util/stream/zlib.h @@ -1,19 +1,19 @@ #pragma once #include "fwd.h" -#include "input.h" -#include "output.h" -#include "buffered.h" +#include "input.h" +#include "output.h" +#include "buffered.h" #include <util/system/defaults.h> #include <util/generic/ptr.h> #include <util/generic/yexception.h> -/** - * @addtogroup Streams_Archs - * @{ - */ - +/** + * @addtogroup Streams_Archs + * @{ + */ + struct TZLibError: public yexception { }; @@ -31,20 +31,20 @@ namespace ZLib { Raw = 3, Invalid = 4 }; - - enum { - ZLIB_BUF_LEN = 8 * 1024 - }; + + enum { + ZLIB_BUF_LEN = 8 * 1024 + }; } /** - * Non-buffered ZLib decompressing stream. + * Non-buffered ZLib decompressing stream. * - * Please don't use `TZLibDecompress` if you read text data from stream using - * `ReadLine`, it is VERY slow (approx 10 times slower, according to synthetic - * benchmark). For fast buffered ZLib stream reading use `TBufferedZLibDecompress` - * aka `TZDecompress`. - */ + * Please don't use `TZLibDecompress` if you read text data from stream using + * `ReadLine`, it is VERY slow (approx 10 times slower, according to synthetic + * benchmark). For fast buffered ZLib stream reading use `TBufferedZLibDecompress` + * aka `TZDecompress`. + */ class TZLibDecompress: public IInputStream { public: TZLibDecompress(IZeroCopyInput* input, ZLib::StreamType type = ZLib::Auto, TStringBuf dict = {}); @@ -63,18 +63,18 @@ public: void SetAllowMultipleStreams(bool allowMultipleStreams); ~TZLibDecompress() override; - -protected: + +protected: size_t DoRead(void* buf, size_t size) override; - + public: class TImpl; THolder<TImpl> Impl_; }; -/** - * Non-buffered ZLib compressing stream. - */ +/** + * Non-buffered ZLib compressing stream. + */ class TZLibCompress: public IOutputStream { public: struct TParams { @@ -82,7 +82,7 @@ public: : Out(out) , Type(ZLib::ZLib) , CompressionLevel(6) - , BufLen(ZLib::ZLIB_BUF_LEN) + , BufLen(ZLib::ZLIB_BUF_LEN) { } @@ -145,7 +145,7 @@ private: public: class TImpl; - /** To allow inline constructors. */ + /** To allow inline constructors. */ struct TDestruct { static void Destroy(TImpl* impl); }; @@ -153,12 +153,12 @@ public: THolder<TImpl, TDestruct> Impl_; }; -/** - * Buffered ZLib decompressing stream. - * - * Supports efficient `ReadLine` calls and similar "reading in small pieces" - * usage patterns. - */ +/** + * Buffered ZLib decompressing stream. + * + * Supports efficient `ReadLine` calls and similar "reading in small pieces" + * usage patterns. + */ class TBufferedZLibDecompress: public TBuffered<TZLibDecompress> { public: template <class T> @@ -170,4 +170,4 @@ public: ~TBufferedZLibDecompress() override; }; -/** @} */ +/** @} */ |