diff options
author | Evgeny Grechnikov <diamondaz@yandex.ru> | 2022-02-10 16:46:20 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:20 +0300 |
commit | 6e38f52f898d7c077ddd319800b4014967a5ca76 (patch) | |
tree | f0b2473cfc98506158b8f1d3d387c4f478ade18e /util/stream | |
parent | bd085aee9b4f7a0bee302ce687964ffb7098f986 (diff) | |
download | ydb-6e38f52f898d7c077ddd319800b4014967a5ca76.tar.gz |
Restoring authorship annotation for Evgeny Grechnikov <diamondaz@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/stream')
-rw-r--r-- | util/stream/format.h | 68 | ||||
-rw-r--r-- | util/stream/format_ut.cpp | 54 |
2 files changed, 61 insertions, 61 deletions
diff --git a/util/stream/format.h b/util/stream/format.h index b033208a1b..057d5de71c 100644 --- a/util/stream/format.h +++ b/util/stream/format.h @@ -38,11 +38,11 @@ namespace NFormatPrivate { template <typename T> struct TLeftPad { - T Value; + T Value; size_t Width; char Padc; - inline TLeftPad(const T& value, size_t width, char padc) + inline TLeftPad(const T& value, size_t width, char padc) : Value(value) , Width(width) , Padc(padc) @@ -54,7 +54,7 @@ namespace NFormatPrivate { IOutputStream& operator<<(IOutputStream& o, const TLeftPad<T>& lp) { TTempBuf buf; TMemoryOutput ss(buf.Data(), buf.Size()); - ss << lp.Value; + ss << lp.Value; size_t written = buf.Size() - ss.Avail(); if (lp.Width > written) { WriteChars(o, lp.Padc, lp.Width - written); @@ -65,11 +65,11 @@ namespace NFormatPrivate { template <typename T> struct TRightPad { - T Value; + T Value; size_t Width; char Padc; - inline TRightPad(const T& value, size_t width, char padc) + inline TRightPad(const T& value, size_t width, char padc) : Value(value) , Width(width) , Padc(padc) @@ -81,7 +81,7 @@ namespace NFormatPrivate { IOutputStream& operator<<(IOutputStream& o, const TRightPad<T>& lp) { TTempBuf buf; TMemoryOutput ss(buf.Data(), buf.Size()); - ss << lp.Value; + ss << lp.Value; size_t written = buf.Size() - ss.Avail(); o.Write(buf.Data(), written); if (lp.Width > written) { @@ -152,24 +152,24 @@ namespace NFormatPrivate { } return os; } - + template <typename T> - struct TFloatPrecision { + struct TFloatPrecision { using TdVal = std::remove_cv_t<T>; static_assert(std::is_floating_point<TdVal>::value, "expect std::is_floating_point<TdVal>::value"); - - TdVal Value; - EFloatToStringMode Mode; - int NDigits; - }; - + + TdVal Value; + EFloatToStringMode Mode; + int NDigits; + }; + template <typename T> IOutputStream& operator<<(IOutputStream& o, const TFloatPrecision<T>& prec) { - char buf[512]; - size_t count = FloatToString(prec.Value, buf, sizeof(buf), prec.Mode, prec.NDigits); - o << TStringBuf(buf, count); - return o; - } + char buf[512]; + size_t count = FloatToString(prec.Value, buf, sizeof(buf), prec.Mode, prec.NDigits); + o << TStringBuf(buf, count); + return o; + } struct THumanReadableDuration { TDuration Value; @@ -205,14 +205,14 @@ namespace NFormatPrivate { */ 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); + return ::NFormatPrivate::TLeftPad<T>(value, width, padc); } template <typename T, int N> static constexpr ::NFormatPrivate::TLeftPad<const T*> LeftPad(const T (&value)[N], const size_t width, const char padc = ' ') noexcept { - return ::NFormatPrivate::TLeftPad<const T*>(value, width, padc); -} - + return ::NFormatPrivate::TLeftPad<const T*>(value, width, padc); +} + /** * Output manipulator similar to `std::setw` and `std::setfill`. * @@ -231,14 +231,14 @@ static constexpr ::NFormatPrivate::TLeftPad<const T*> LeftPad(const T (&value)[N */ 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); + return ::NFormatPrivate::TRightPad<T>(value, width, padc); } template <typename T, int N> static constexpr ::NFormatPrivate::TRightPad<const T*> RightPad(const T (&value)[N], const size_t width, const char padc = ' ') noexcept { - return ::NFormatPrivate::TRightPad<const T*>(value, width, padc); -} - + return ::NFormatPrivate::TRightPad<const T*>(value, width, padc); +} + /** * Output manipulator similar to `std::setbase(16)`. * @@ -402,7 +402,7 @@ static constexpr ::NFormatPrivate::THumanReadableSize HumanReadableSize(const do void Time(IOutputStream& l); void TimeHumanReadable(IOutputStream& l); - + /** * Output manipulator for adjusting precision of floating point values. * @@ -419,11 +419,11 @@ void TimeHumanReadable(IOutputStream& l); * @param ndigits Number of significant digits (in `PREC_NDIGITS` and `PREC_POINT_DIGITS` mode). * @see EFloatToStringMode */ -template <typename T> +template <typename T> static constexpr ::NFormatPrivate::TFloatPrecision<T> Prec(const T& value, const EFloatToStringMode mode, const int ndigits = 0) noexcept { - return {value, mode, ndigits}; -} - + return {value, mode, ndigits}; +} + /** * Output manipulator for adjusting precision of floating point values. * @@ -440,5 +440,5 @@ static constexpr ::NFormatPrivate::TFloatPrecision<T> Prec(const T& value, const */ template <typename T> static constexpr ::NFormatPrivate::TFloatPrecision<T> Prec(const T& value, const int ndigits) noexcept { - return {value, PREC_NDIGITS, ndigits}; -} + return {value, PREC_NDIGITS, ndigits}; +} diff --git a/util/stream/format_ut.cpp b/util/stream/format_ut.cpp index 43245aeb48..b58052e319 100644 --- a/util/stream/format_ut.cpp +++ b/util/stream/format_ut.cpp @@ -30,21 +30,21 @@ Y_UNIT_TEST_SUITE(TOutputStreamFormattingTest) { ss << "[" << Time << "] " << "qwqw" << TimeHumanReadable << Endl; } - + Y_UNIT_TEST(TestHexReference) { - /* - One possible implementation of Hex() stores a reference to the given object. - This can lead to wrong results if the given object is a temporary - which is valid only during constructor call. The following code tries to - demonstrate this. If the implementation stores a reference, - the test fails if compiled with g++44 in debug build - (without optimizations), but performs correctly in release build. - */ - THolder<TStringStream> ss(new TStringStream); - THolder<int> ii(new int(0x1234567)); - (*ss) << Hex(*ii); - UNIT_ASSERT_VALUES_EQUAL("0x01234567", ss->Str()); - } + /* + One possible implementation of Hex() stores a reference to the given object. + This can lead to wrong results if the given object is a temporary + which is valid only during constructor call. The following code tries to + demonstrate this. If the implementation stores a reference, + the test fails if compiled with g++44 in debug build + (without optimizations), but performs correctly in release build. + */ + THolder<TStringStream> ss(new TStringStream); + THolder<int> ii(new int(0x1234567)); + (*ss) << Hex(*ii); + UNIT_ASSERT_VALUES_EQUAL("0x01234567", ss->Str()); + } Y_UNIT_TEST(TestHexText) { { @@ -59,7 +59,7 @@ Y_UNIT_TEST_SUITE(TOutputStreamFormattingTest) { UNIT_ASSERT_VALUES_EQUAL("0061 0062 0063 0438", ss.Str()); } } - + 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"); @@ -82,18 +82,18 @@ Y_UNIT_TEST_SUITE(TOutputStreamFormattingTest) { } Y_UNIT_TEST(TestPrec) { - TStringStream ss; - ss << Prec(1.2345678901234567, PREC_AUTO); - UNIT_ASSERT_VALUES_EQUAL("1.2345678901234567", ss.Str()); - - ss.Clear(); - ss << Prec(1.2345678901234567, 3); - UNIT_ASSERT_VALUES_EQUAL("1.23", ss.Str()); - - ss.Clear(); - ss << Prec(1.2345678901234567, PREC_POINT_DIGITS, 3); - UNIT_ASSERT_VALUES_EQUAL("1.235", ss.Str()); - } + TStringStream ss; + ss << Prec(1.2345678901234567, PREC_AUTO); + UNIT_ASSERT_VALUES_EQUAL("1.2345678901234567", ss.Str()); + + ss.Clear(); + ss << Prec(1.2345678901234567, 3); + UNIT_ASSERT_VALUES_EQUAL("1.23", ss.Str()); + + ss.Clear(); + ss << Prec(1.2345678901234567, PREC_POINT_DIGITS, 3); + UNIT_ASSERT_VALUES_EQUAL("1.235", ss.Str()); + } Y_UNIT_TEST(TestHumanReadableSize1000) { UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(0, SF_QUANTITY)), "0"); |