Browse Source

Util: Fix Builder::putU64() when printing unaligned zero-padded hexes

master
Riyyi 2 years ago
parent
commit
086da50a57
  1. 48
      src/util/format/builder.cpp

48
src/util/format/builder.cpp

@ -73,85 +73,87 @@ void Builder::putU64(size_t value,
size_t width, size_t width,
bool isNegative) const bool isNegative) const
{ {
std::string string = numberToString(value, base, uppercase); std::string number = numberToString(value, base, uppercase);
// Sign // Sign
std::string signCharacter = ""; std::string prefix = "";
switch (sign) { switch (sign) {
case Sign::None: case Sign::None:
case Sign::Negative: case Sign::Negative:
if (isNegative) { if (isNegative) {
signCharacter = '-'; prefix = '-';
} }
break; break;
case Sign::Both: case Sign::Both:
signCharacter = (isNegative) ? '-' : '+'; prefix = (isNegative) ? '-' : '+';
break; break;
case Sign::Space: case Sign::Space:
signCharacter = (isNegative) ? '-' : ' '; prefix = (isNegative) ? '-' : ' ';
break; break;
default: default:
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
}; };
if (align != Align::None || !zeroPadding) {
string.insert(0, signCharacter);
}
// Alternative form // Alternative form
if (alternativeForm) { if (alternativeForm) {
switch (base) { switch (base) {
case 2: case 2:
string.insert(0, (uppercase) ? "0B" : "0b"); prefix += (uppercase) ? "0B" : "0b";
break; break;
case 8: case 8:
string.insert(0, 1, '0'); prefix += '0';
break; break;
case 10: case 10:
break; break;
case 16: case 16:
string.insert(0, (uppercase) ? "0X" : "0x"); prefix += (uppercase) ? "0X" : "0x";
break; break;
default: default:
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
} }
// Zero padding if (!zeroPadding) {
if (zeroPadding) { number.insert(0, prefix);
}
else {
if (align != Builder::Align::None) {
number.insert(0, prefix);
}
fill = '0'; fill = '0';
} }
size_t length = string.length(); size_t length = number.length();
if (width < length) { if (width < length) {
m_builder.write(string.data(), length); m_builder.write(number.data(), length);
return; return;
} }
switch (align) { switch (align) {
case Align::Left: case Align::Left:
m_builder.write(string.data(), length); m_builder.write(number.data(), length);
m_builder << std::string(width - length, fill); m_builder << std::string(width - length, fill);
break; break;
case Align::Center: { case Align::Center: {
size_t half = (width - length) / 2; size_t half = (width - length) / 2;
m_builder << std::string(half, fill); m_builder << std::string(half, fill);
m_builder.write(string.data(), length); m_builder.write(number.data(), length);
m_builder << std::string(width - half - length, fill); m_builder << std::string(width - half - length, fill);
break; break;
} }
case Align::Right: case Align::Right:
m_builder << std::string(width - length, fill); m_builder << std::string(width - length, fill);
m_builder.write(string.data(), length); m_builder.write(number.data(), length);
break; break;
case Align::None: case Align::None:
if (zeroPadding) { if (zeroPadding) {
m_builder << signCharacter; m_builder << prefix;
if (signCharacter.empty()) { m_builder << std::string(width - length - prefix.length(), fill);
m_builder << '0';
} }
else {
m_builder << std::string(width - length, fill);
} }
m_builder << std::string(width - length - zeroPadding, fill); m_builder.write(number.data(), length);
m_builder.write(string.data(), length);
break; break;
default: default:
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();

Loading…
Cancel
Save