9#include "../microfmt.hpp"
10#include <boost/dynamic_bitset.hpp>
11#include <boost/logic/tribool.hpp>
12#include <boost/rational.hpp>
19template <
typename Block,
typename Allocator>
struct formatter<boost::dynamic_bitset<Block, Allocator>> {
21 bool uppercase{
false};
25 for (
const char character : ctx.spec()) {
26 if (character ==
'x' || character ==
'X') {
28 uppercase = character ==
'X';
29 }
else if (character ==
'b') {
31 }
else if (character ==
'#') {
37 void format(
const boost::dynamic_bitset<Block, Allocator> &
bits,
const sink &out)
const noexcept {
39 out.write(base == 16 ? (uppercase ?
"0X" :
"0x") :
"0b");
47 for (std::size_t index =
bits.size(); index != 0; --index) {
48 out.put(
bits.test(index - 1) ?
'1' :
'0');
53 const auto &digits = uppercase ? detail::hex_digits_upper : detail::hex_digits_lower;
54 const std::size_t nibble_count = (
bits.size() + 3) / 4;
55 for (std::size_t nibble = nibble_count; nibble != 0; --nibble) {
57 for (
unsigned bit = 0; bit < 4; ++bit) {
58 const std::size_t index = (nibble - 1) * 4 + bit;
59 if (index <
bits.size() &&
bits.test(index)) {
63 out.put(digits[value]);
68template <
typename IntType>
struct formatter<boost::rational<IntType>> {
69 microfmt::string_view forwarded_spec{};
73 void format(
const boost::rational<IntType> &value,
const sink &out)
const noexcept {
76 integer_formatter.parse(integer_context);
77 integer_formatter.format(value.numerator(), out);
79 integer_formatter.format(value.denominator(), out);
83template <>
struct formatter<boost::logic::tribool> {
86 void format(boost::logic::tribool value,
const sink &out)
const noexcept {
87 if (boost::logic::indeterminate(value)) {
88 out.write(
"indeterminate");
90 out.write(value ?
"true" :
"false");
Definition microfmt.hpp:881
reloco::sink sink
Type-erased, zero-allocation output sink.
Definition microfmt.hpp:46
constexpr bitfield_view bits(uint32_t raw_val, span< const bit_field > fields, bool show_raw_hex=true, microfmt::string_view sep=" | ") noexcept
Definition bitfield.hpp:36