Browse Source

Util: Add alternative form specifier option to integral types

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

23
src/util/format/builder.cpp

@ -68,6 +68,7 @@ void Builder::putU64(size_t value,
char fill,
Align align,
Sign sign,
bool alternativeForm,
bool zeroPadding,
size_t width,
bool isNegative) const
@ -96,6 +97,25 @@ void Builder::putU64(size_t value,
string.insert(0, signCharacter);
}
// Alternative form
if (alternativeForm) {
switch (base) {
case 2:
string.insert(0, (uppercase) ? "0B" : "0b");
break;
case 8:
break;
string.insert(0, 1, '0');
case 10:
break;
case 16:
string.insert(0, (uppercase) ? "0X" : "0x");
break;
default:
VERIFY_NOT_REACHED();
}
}
// Zero padding
if (zeroPadding) {
fill = '0';
@ -144,12 +164,13 @@ void Builder::putI64(int64_t value,
char fill,
Align align,
Sign sign,
bool alternativeForm,
bool zeroPadding,
size_t width) const
{
bool isNegative = value < 0;
value = isNegative ? -value : value;
putU64(static_cast<uint64_t>(value), base, uppercase, fill, align, sign, zeroPadding, width, isNegative);
putU64(static_cast<uint64_t>(value), base, uppercase, fill, align, sign, alternativeForm, zeroPadding, width, isNegative);
}
void Builder::putF64(double number, uint8_t precision) const

2
src/util/format/builder.h

@ -42,6 +42,7 @@ public:
char fill = ' ',
Align align = Align::Right,
Sign sign = Sign::Negative,
bool alternativeForm = false,
bool zeroPadding = false,
size_t width = 0,
bool isNegative = false) const;
@ -52,6 +53,7 @@ public:
char fill = ' ',
Align align = Align::Right,
Sign sign = Sign::Negative,
bool alternativeForm = false,
bool zeroPadding = false,
size_t width = 0) const;

4
src/util/format/formatter.h

@ -127,12 +127,12 @@ struct Formatter<T> {
if constexpr (std::is_unsigned_v<T>) {
builder.putU64(
value, base, uppercase, specifier.fill, specifier.align, specifier.sign,
specifier.zeroPadding, specifier.width);
specifier.alternativeForm, specifier.zeroPadding, specifier.width);
}
else {
builder.putI64(
value, base, uppercase, specifier.fill, specifier.align, specifier.sign,
specifier.zeroPadding, specifier.width);
specifier.alternativeForm, specifier.zeroPadding, specifier.width);
}
}
};

Loading…
Cancel
Save