8template <std::
unsigned_
integral UInt, u
int8_t Base>
9consteval size_t Writer::UnsignedDigitCapacity()
11 static_assert(Base == 2 || Base == 8 || Base == 10 || Base == 16,
12 "LibXR::Print::Writer only supports base 2, 8, 10, and 16");
14 UInt value = std::numeric_limits<UInt>::max();
16 while (value >=
static_cast<UInt
>(Base))
18 value /=
static_cast<UInt
>(Base);
36template <u
int8_t Base,
bool UpperCase,
size_t N, std::
unsigned_
integral UInt>
37size_t Writer::AppendUnsigned(
char (&out)[N], UInt value)
39 constexpr char lower_digits[] =
"0123456789abcdef";
40 constexpr char upper_digits[] =
"0123456789ABCDEF";
42 N >= UnsignedDigitCapacity<UInt, Base>(),
43 "LibXR::Print::Writer digit buffer is too small for the selected integer type");
45 const char* digits = UpperCase ? upper_digits : lower_digits;
46 char reverse[UnsignedDigitCapacity<UInt, Base>()];
57 reverse[count++] = digits[
static_cast<size_t>(value %
static_cast<UInt
>(Base))];
58 value /=
static_cast<UInt
>(Base);
61 for (
size_t i = 0; i < count; ++i)
63 out[i] = reverse[count - i - 1];
76inline size_t Writer::AppendSmallUnsigned(
char (&out)[N], uint8_t value)
78 return AppendUnsigned<10>(out, value);
88constexpr size_t Writer::FieldPadding(uint8_t width,
size_t payload_size)
90 return (width > payload_size) ?
static_cast<size_t>(width) - payload_size : 0;
100constexpr size_t Writer::IntegerPrecisionZeros(
const Spec& spec,
size_t digit_count)
102 return (spec.HasPrecision() && spec.precision > digit_count)
103 ?
static_cast<size_t>(spec.precision) - digit_count
116template <std::
unsigned_
integral UInt>
117constexpr std::string_view Writer::IntegerPrefix(FormatType type,
const Spec& spec,
120 if (!spec.Alternate() || value == 0)
124 if (type == FormatType::HexLower32 || type == FormatType::HexLower64)
128 if (type == FormatType::HexUpper32 || type == FormatType::HexUpper64)
132 if (type == FormatType::Binary32 || type == FormatType::Binary64)
134 return spec.UpperCase() ?
"0B" :
"0b";
149template <std::
unsigned_
integral UInt>
150size_t Writer::ApplyAlternateOctal(
char* digits,
size_t digit_count,
const Spec& spec,
153 if (!spec.Alternate())
155 return (value == 0 && spec.precision == 0) ? 0 : digit_count;
158 if (value == 0 && spec.precision == 0)
168 if (spec.HasPrecision() && spec.precision > digit_count)
173 for (
size_t i = digit_count; i > 0; --i)
175 digits[i] = digits[i - 1];