123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- enum ENumberFormatFlag {
- HF_FULL = 0x01,
- HF_ADDX = 0x02,
- };
- Y_DECLARE_FLAGS(ENumberFormat, ENumberFormatFlag);
- Y_DECLARE_OPERATORS_FOR_FLAGS(ENumberFormat);
- enum ESizeFormat {
- SF_QUANTITY,
- SF_BYTES,
- };
- 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> {};
- template <typename T>
- inline void StreamWrite(T& stream, const char* s, size_t size) {
- stream.write(s, size);
- }
- template <>
- inline void StreamWrite(IOutputStream& stream, const char* s, size_t size) {
- stream.Write(s, size);
- }
- template <>
- inline void StreamWrite(TStringStream& stream, const char* s, size_t size) {
- stream.Write(s, size);
- }
- template <typename T>
- static inline void WriteChars(T& os, char c, size_t count) {
- if (count == 0) {
- return;
- }
- TTempBuf buf(count);
- memset(buf.Data(), c, count);
- StreamWrite(os, buf.Data(), count);
- }
- template <typename T>
- struct TLeftPad {
- T Value;
- size_t Width;
- char Padc;
- inline TLeftPad(const T& value, size_t width, char padc)
- : Value(value)
- , Width(width)
- , Padc(padc)
- {
- }
- };
- template <typename T>
- IOutputStream& operator<<(IOutputStream& o Y_LIFETIME_BOUND, const TLeftPad<T>& lp) {
- TTempBuf buf;
- TMemoryOutput ss(buf.Data(), buf.Size());
- ss << lp.Value;
- size_t written = buf.Size() - ss.Avail();
- if (lp.Width > written) {
- WriteChars(o, lp.Padc, lp.Width - written);
- }
- o.Write(buf.Data(), written);
- return o;
- }
- template <typename T>
- struct TRightPad {
- T Value;
- size_t Width;
- char Padc;
- inline TRightPad(const T& value, size_t width, char padc)
- : Value(value)
- , Width(width)
- , Padc(padc)
- {
- }
- };
- template <typename T>
- IOutputStream& operator<<(IOutputStream& o Y_LIFETIME_BOUND, const TRightPad<T>& lp) {
- TTempBuf buf;
- TMemoryOutput ss(buf.Data(), buf.Size());
- ss << lp.Value;
- size_t written = buf.Size() - ss.Avail();
- o.Write(buf.Data(), written);
- if (lp.Width > written) {
- WriteChars(o, lp.Padc, lp.Width - written);
- }
- return o;
- }
- template <typename T, size_t Base>
- struct TBaseNumber {
- T Value;
- ENumberFormat Flags;
- template <typename OtherT>
- inline TBaseNumber(OtherT value, ENumberFormat flags)
- : Value(value)
- , Flags(flags)
- {
- }
- };
- template <typename T, size_t Base>
- using TUnsignedBaseNumber = TBaseNumber<std::make_unsigned_t<std::remove_cv_t<T>>, Base>;
- template <typename TStream, typename T, size_t Base>
- TStream& ToStreamImpl(TStream& stream Y_LIFETIME_BOUND, const TBaseNumber<T, Base>& value) {
- char buf[8 * sizeof(T) + 1];
- TStringBuf str(buf, IntToString<Base>(value.Value, buf, sizeof(buf)));
- if (str[0] == '-') {
- stream << '-';
- str.Skip(1);
- }
- if (value.Flags & HF_ADDX) {
- if (Base == 16) {
- stream << TStringBuf("0x");
- } 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());
- }
- stream << str;
- return stream;
- }
- template <typename T, size_t Base>
- IOutputStream& operator<<(IOutputStream& stream Y_LIFETIME_BOUND, const TBaseNumber<T, Base>& value) {
- return ToStreamImpl(stream, value);
- }
- template <typename T, size_t Base>
- std::ostream& operator<<(std::ostream& stream Y_LIFETIME_BOUND, const TBaseNumber<T, Base>& value) {
- return ToStreamImpl(stream, value);
- }
- template <typename Char, size_t Base>
- struct TBaseText {
- TBasicStringBuf<Char> Text;
- inline TBaseText(const TBasicStringBuf<Char> text)
- : Text(text)
- {
- }
- };
- template <typename Char, size_t Base>
- IOutputStream& operator<<(IOutputStream& os Y_LIFETIME_BOUND, 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);
- }
- return os;
- }
- template <typename T>
- 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;
- };
- template <typename T>
- IOutputStream& operator<<(IOutputStream& o Y_LIFETIME_BOUND, 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;
- }
- struct THumanReadableDuration {
- TDuration Value;
- constexpr THumanReadableDuration(const TDuration& value)
- : Value(value)
- {
- }
- };
- struct THumanReadableSize {
- double Value;
- ESizeFormat Format;
- };
- }
- 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);
- }
- 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);
- }
- 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);
- }
- 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);
- }
- template <typename T>
- static constexpr ::NFormatPrivate::TUnsignedBaseNumber<T, 16> Hex(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept {
- return {value, 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};
- }
- template <typename T>
- static constexpr ::NFormatPrivate::TUnsignedBaseNumber<T, 2> Bin(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept {
- return {value, 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};
- }
- template <typename TChar>
- static inline ::NFormatPrivate::TBaseText<TChar, 16> HexText(const TBasicStringBuf<TChar> value) {
- return ::NFormatPrivate::TBaseText<TChar, 16>(value);
- }
- template <typename TChar>
- static inline ::NFormatPrivate::TBaseText<TChar, 2> BinText(const TBasicStringBuf<TChar> value) {
- return ::NFormatPrivate::TBaseText<TChar, 2>(value);
- }
- static constexpr ::NFormatPrivate::THumanReadableDuration HumanReadable(const TDuration duration) noexcept {
- return ::NFormatPrivate::THumanReadableDuration(duration);
- }
- static constexpr ::NFormatPrivate::THumanReadableSize HumanReadableSize(const double size, ESizeFormat format) noexcept {
- return {size, format};
- }
- void Time(IOutputStream& l);
- void TimeHumanReadable(IOutputStream& l);
- template <typename T>
- static constexpr ::NFormatPrivate::TFloatPrecision<T> Prec(const T& value, const EFloatToStringMode mode, const int ndigits = 0) noexcept {
- return {value, mode, ndigits};
- }
- template <typename T>
- static constexpr ::NFormatPrivate::TFloatPrecision<T> Prec(const T& value, const int ndigits) noexcept {
- return {value, PREC_NDIGITS, ndigits};
- }
|