From 25287bc370a8d5001e923712c05f1817a6ecaee7 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Thu, 4 Aug 2022 19:31:30 +0200 Subject: [PATCH] Util: Add zero padding specifier to putU64() --- src/util/format/builder.cpp | 33 +++++++++++++++++++++++++-------- src/util/format/builder.h | 4 ++-- src/util/format/formatter.h | 6 ++++-- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/util/format/builder.cpp b/src/util/format/builder.cpp index 03216a8..5b31f7e 100644 --- a/src/util/format/builder.cpp +++ b/src/util/format/builder.cpp @@ -28,28 +28,36 @@ void Builder::putLiteral(std::string_view literal) } } -void Builder::putU64(size_t value, char fill, Align align, Sign sign, size_t width, bool isNegative) const +void Builder::putU64(size_t value, char fill, Align align, Sign sign, bool zeroPadding, size_t width, bool isNegative) const { std::string string = (std::stringstream {} << value).str(); - printf("sign {%c}\n", (char)sign); // Sign + std::string signCharacter = ""; switch (sign) { case Sign::None: case Sign::Negative: if (isNegative) { - string.insert(0, 1, '-'); + signCharacter = '-'; } break; case Sign::Both: - string.insert(0, 1, (isNegative) ? '-' : '+'); + signCharacter = (isNegative) ? '-' : '+'; break; case Sign::Space: - string.insert(0, 1, (isNegative) ? '-' : ' '); + signCharacter = (isNegative) ? '-' : ' '; break; default: VERIFY_NOT_REACHED(); }; + if (align != Align::None || !zeroPadding) { + string.insert(0, signCharacter); + } + + // Zero padding + if (zeroPadding) { + fill = '0'; + } size_t length = string.length(); if (width < length) { @@ -69,21 +77,30 @@ void Builder::putU64(size_t value, char fill, Align align, Sign sign, size_t wid m_builder << std::string(width - half - length, fill); break; } - case Align::None: case Align::Right: m_builder << std::string(width - length, fill); m_builder.write(string.data(), length); break; + case Align::None: + if (zeroPadding) { + m_builder << signCharacter; + if (signCharacter.empty()) { + m_builder << '0'; + } + } + m_builder << std::string(width - length - zeroPadding, fill); + m_builder.write(string.data(), length); + break; default: VERIFY_NOT_REACHED(); }; } -void Builder::putI64(int64_t value, char fill, Align align, Sign sign, size_t width) const +void Builder::putI64(int64_t value, char fill, Align align, Sign sign, bool zeroPadding, size_t width) const { bool isNegative = value < 0; value = isNegative ? -value : value; - putU64(static_cast(value), fill, align, sign, width, isNegative); + putU64(static_cast(value), fill, align, sign, zeroPadding, width, isNegative); } void Builder::putF64(double number, uint8_t precision) const diff --git a/src/util/format/builder.h b/src/util/format/builder.h index 460a0b2..5e3745b 100644 --- a/src/util/format/builder.h +++ b/src/util/format/builder.h @@ -36,8 +36,8 @@ public: void putLiteral(std::string_view literal); - void putU64(size_t value, char fill = ' ', Align align = Align::Right, Sign sign = Sign::Negative, size_t width = 0, bool isNegative = false) const; - void putI64(int64_t value, char fill = ' ', Align align = Align::Right, Sign sign = Sign::Negative, size_t width = 0) const; + void putU64(size_t value, char fill = ' ', Align align = Align::Right, Sign sign = Sign::Negative, bool zeroPadding = false, size_t width = 0, bool isNegative = false) const; + void putI64(int64_t value, char fill = ' ', Align align = Align::Right, Sign sign = Sign::Negative, bool zeroPadding = false, size_t width = 0) const; void putF64(double number, uint8_t precision = 6) const; void putCharacter(char character) const { m_builder.write(&character, 1); } void putString(std::string_view string, char fill = ' ', Align align = Align::Left, size_t width = 0) const; diff --git a/src/util/format/formatter.h b/src/util/format/formatter.h index b466955..6b8bc75 100644 --- a/src/util/format/formatter.h +++ b/src/util/format/formatter.h @@ -93,11 +93,13 @@ struct Formatter { { if (std::is_signed_v) { builder.putI64( - value, specifier.fill, specifier.align, specifier.sign, specifier.width); + value, specifier.fill, specifier.align, specifier.sign, + specifier.zeroPadding, specifier.width); } if (std::is_unsigned_v) { builder.putU64( - value, specifier.fill, specifier.align, specifier.sign, specifier.width); + value, specifier.fill, specifier.align, specifier.sign, + specifier.zeroPadding, specifier.width); } } };