Browse Source

Util: Add zero padding specifier to putU64()

master
Riyyi 2 years ago
parent
commit
25287bc370
  1. 33
      src/util/format/builder.cpp
  2. 4
      src/util/format/builder.h
  3. 6
      src/util/format/formatter.h

33
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<uint64_t>(value), fill, align, sign, width, isNegative);
putU64(static_cast<uint64_t>(value), fill, align, sign, zeroPadding, width, isNegative);
}
void Builder::putF64(double number, uint8_t precision) const

4
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;

6
src/util/format/formatter.h

@ -93,11 +93,13 @@ struct Formatter<T> {
{
if (std::is_signed_v<T>) {
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<T>) {
builder.putU64(
value, specifier.fill, specifier.align, specifier.sign, specifier.width);
value, specifier.fill, specifier.align, specifier.sign,
specifier.zeroPadding, specifier.width);
}
}
};

Loading…
Cancel
Save