diff options
author | Ilnur Khuziev <ilnur.khuziev@yandex.ru> | 2022-02-10 16:46:14 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:14 +0300 |
commit | 60040c91ffe701a84689b2c6310ff845e65cff42 (patch) | |
tree | 1a2c5ffcf89eb53ecd79dbc9bc0a195c27404d0c /util/stream/format.cpp | |
parent | 736dcd8ca259457a136f2f9f9168c44643914323 (diff) | |
download | ydb-60040c91ffe701a84689b2c6310ff845e65cff42.tar.gz |
Restoring authorship annotation for Ilnur Khuziev <ilnur.khuziev@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'util/stream/format.cpp')
-rw-r--r-- | util/stream/format.cpp | 144 |
1 files changed, 72 insertions, 72 deletions
diff --git a/util/stream/format.cpp b/util/stream/format.cpp index eafdd20f81..3996130df5 100644 --- a/util/stream/format.cpp +++ b/util/stream/format.cpp @@ -4,13 +4,13 @@ #include <util/generic/ymath.h> #include <util/string/cast.h> -namespace NFormatPrivate { - static inline i64 Round(double value) { - double res1 = floor(value); - double res2 = ceil(value); +namespace NFormatPrivate { + static inline i64 Round(double value) { + double res1 = floor(value); + double res2 = ceil(value); return (value - res1 < res2 - value) ? (i64)res1 : (i64)res2; - } - + } + static inline IOutputStream& PrintDoubleShortly(IOutputStream& os, const double& d) { // General case: request 3 significant digits // Side-effect: allows exponential representation @@ -33,95 +33,95 @@ namespace NFormatPrivate { } return os << Prec(d, mode, ndigits); - } + } } - + 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; - - double v = value.Value; - if (v < 0) { - stream << "-"; - v = -v; - } - - if (v < base) { - NFormatPrivate::PrintDoubleShortly(stream, v); + + double v = value.Value; + if (v < 0) { + stream << "-"; + v = -v; + } + + if (v < base) { + NFormatPrivate::PrintDoubleShortly(stream, v); } else if (v < base2) { - NFormatPrivate::PrintDoubleShortly(stream, v / (double)base) << 'K'; + NFormatPrivate::PrintDoubleShortly(stream, v / (double)base) << 'K'; } else if (v < base3) { - NFormatPrivate::PrintDoubleShortly(stream, v / (double)base2) << 'M'; + NFormatPrivate::PrintDoubleShortly(stream, v / (double)base2) << 'M'; } else if (v < base4) { - NFormatPrivate::PrintDoubleShortly(stream, v / (double)base3) << 'G'; - } else { - NFormatPrivate::PrintDoubleShortly(stream, v / (double)base4) << 'T'; - } - + NFormatPrivate::PrintDoubleShortly(stream, v / (double)base3) << 'G'; + } else { + NFormatPrivate::PrintDoubleShortly(stream, v / (double)base4) << 'T'; + } + if (value.Format == SF_BYTES) { - if (v < base) { + if (v < base) { stream << "B"; } else { stream << "iB"; } } -} - +} + template <> void Out<NFormatPrivate::THumanReadableDuration>(IOutputStream& os, const NFormatPrivate::THumanReadableDuration& hr) { - TTempBuf buf; - TMemoryOutput ss(buf.Data(), buf.Size()); - - do { - ui64 microSeconds = hr.Value.MicroSeconds(); - if (microSeconds < 1000) { - ss << microSeconds << "us"; - break; - } - if (microSeconds < 1000 * 1000) { + TTempBuf buf; + TMemoryOutput ss(buf.Data(), buf.Size()); + + do { + ui64 microSeconds = hr.Value.MicroSeconds(); + if (microSeconds < 1000) { + ss << microSeconds << "us"; + break; + } + if (microSeconds < 1000 * 1000) { NFormatPrivate::PrintDoubleShortly(ss, (double)microSeconds / 1000.0) << "ms"; - break; - } - + break; + } + double seconds = (double)(hr.Value.MilliSeconds()) / 1000.0; - if (seconds < 60) { - NFormatPrivate::PrintDoubleShortly(ss, seconds) << 's'; - break; - } - - ui64 s = NFormatPrivate::Round(seconds * 1000 + 0.5) / 1000; - - ui64 m = s / 60; - s = s % 60; - - ui64 h = m / 60; - m = m % 60; - - ui64 d = h / 24; - h = h % 24; - - ui64 times[] = {d, h, m, s}; - char names[] = {'d', 'h', 'm', 's'}; - bool first = true; - + if (seconds < 60) { + NFormatPrivate::PrintDoubleShortly(ss, seconds) << 's'; + break; + } + + ui64 s = NFormatPrivate::Round(seconds * 1000 + 0.5) / 1000; + + ui64 m = s / 60; + s = s % 60; + + ui64 h = m / 60; + m = m % 60; + + ui64 d = h / 24; + h = h % 24; + + ui64 times[] = {d, h, m, s}; + char names[] = {'d', 'h', 'm', 's'}; + bool first = true; + for (size_t i = 0; i < Y_ARRAY_SIZE(times); ++i) { - if (times[i] > 0) { + if (times[i] > 0) { if (!first) { - ss << ' '; + ss << ' '; } - ss << times[i] << names[i]; - first = false; - } - } - } while (false); - - size_t written = buf.Size() - ss.Avail(); - os.Write(buf.Data(), written); -} - + ss << times[i] << names[i]; + first = false; + } + } + } while (false); + + size_t written = buf.Size() - ss.Avail(); + os.Write(buf.Data(), written); +} + void Time(IOutputStream& l) { l << millisec(); } |