7template <std::
unsigned_
integral UInt, u
int8_t Base>
8consteval size_t Writer::UnsignedDigitCapacity()
10 static_assert(Base == 2 || Base == 8 || Base == 10 || Base == 16,
11 "LibXR::Print::Writer only supports base 2, 8, 10, and 16");
13 UInt value = std::numeric_limits<UInt>::max();
15 while (value >=
static_cast<UInt
>(Base))
17 value /=
static_cast<UInt
>(Base);
33template <u
int8_t Base,
bool UpperCase,
size_t N, std::
unsigned_
integral UInt>
34size_t Writer::AppendUnsigned(
char (&out)[N], UInt value)
36 constexpr char lower_digits[] =
"0123456789abcdef";
37 constexpr char upper_digits[] =
"0123456789ABCDEF";
38 static_assert(N >= UnsignedDigitCapacity<UInt, Base>(),
39 "LibXR::Print::Writer digit buffer is too small for the selected integer type");
41 const char* digits = UpperCase ? upper_digits : lower_digits;
42 char reverse[UnsignedDigitCapacity<UInt, Base>()];
53 reverse[count++] = digits[
static_cast<size_t>(value %
static_cast<UInt
>(Base))];
54 value /=
static_cast<UInt
>(Base);
57 for (
size_t i = 0; i < count; ++i)
59 out[i] = reverse[count - i - 1];
72inline size_t Writer::AppendSmallUnsigned(
char (&out)[N], uint8_t value)
74 return AppendUnsigned<10>(out, value);
83constexpr size_t Writer::FieldPadding(uint8_t width,
size_t payload_size)
85 return (width > payload_size) ?
static_cast<size_t>(width) - payload_size : 0;
94constexpr size_t Writer::IntegerPrecisionZeros(
const Spec& spec,
size_t digit_count)
96 return (spec.HasPrecision() && spec.precision > digit_count)
97 ?
static_cast<size_t>(spec.precision) - digit_count
109template <std::
unsigned_
integral UInt>
110constexpr std::string_view Writer::IntegerPrefix(FormatType type,
const Spec& spec,
113 if (!spec.Alternate() || value == 0)
117 if (type == FormatType::HexLower32 || type == FormatType::HexLower64)
121 if (type == FormatType::HexUpper32 || type == FormatType::HexUpper64)
125 if (type == FormatType::Binary32 || type == FormatType::Binary64)
127 return spec.UpperCase() ?
"0B" :
"0b";
141template <std::
unsigned_
integral UInt>
142size_t Writer::ApplyAlternateOctal(
char* digits,
size_t digit_count,
const Spec& spec,
145 if (!spec.Alternate())
147 return (value == 0 && spec.precision == 0) ? 0 : digit_count;
150 if (value == 0 && spec.precision == 0)
160 if (spec.HasPrecision() && spec.precision > digit_count)
165 digits[digit_count++] =
'0';
166 for (
size_t i = digit_count - 1; i > 0; --i)
168 digits[i] = digits[i - 1];