23 #ifndef WPIUTIL_WPI_FORMAT_H
24 #define WPIUTIL_WPI_FORMAT_H
26 #include "wpi/ArrayRef.h"
27 #include "wpi/StringRef.h"
47 virtual int snprint(
char *Buffer,
unsigned BufferSize)
const = 0;
55 unsigned print(
char *Buffer,
unsigned BufferSize)
const {
56 assert(BufferSize &&
"Invalid buffer size!");
59 int N =
snprint(Buffer, BufferSize);
63 return BufferSize * 2;
67 if (
unsigned(N) >= BufferSize)
82 template <
typename Arg,
typename... Args>
84 static_assert(std::is_scalar<Arg>::value,
85 "format can't be used with non fundamental / non pointer type");
90 template <
typename... Ts>
92 std::tuple<Ts...> Vals;
94 template <std::size_t... Is>
95 int snprint_tuple(
char *Buffer,
unsigned BufferSize,
96 std::index_sequence<Is...>)
const {
98 return _snprintf_s(Buffer, BufferSize, BufferSize, Fmt, std::get<Is>(Vals)...);
101 #pragma GCC diagnostic push
102 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
104 return snprintf(Buffer, BufferSize, Fmt, std::get<Is>(Vals)...);
106 #pragma GCC diagnostic pop
117 int snprint(
char *Buffer,
unsigned BufferSize)
const override {
118 return snprint_tuple(Buffer, BufferSize, std::index_sequence_for<Ts...>());
131 template <
typename... Ts>
139 enum Justification { JustifyNone, JustifyLeft, JustifyRight, JustifyCenter };
141 : Str(S), Width(W), Justify(J) {}
146 Justification Justify;
184 : HexValue(HV), DecValue(DV), Width(W), Hex(H), Upper(U),
195 bool Upper =
false) {
196 assert(Width <= 18 &&
"hex width must be <= 18");
208 bool Upper =
false) {
209 assert(Width <= 16 &&
"hex width must be <= 16");
227 std::optional<uint64_t> FirstByteOffset;
228 uint32_t IndentLevel;
230 uint8_t ByteGroupSize;
237 uint32_t NPL, uint8_t BGS,
bool U,
bool A)
238 : Bytes(B), FirstByteOffset(O), IndentLevel(IL), NumPerLine(NPL),
239 ByteGroupSize(BGS), Upper(U), ASCII(A) {
241 if (ByteGroupSize > NumPerLine)
242 ByteGroupSize = NumPerLine;
247 format_bytes(
ArrayRef<uint8_t> Bytes, std::optional<uint64_t> FirstByteOffset = std::nullopt,
248 uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4,
249 uint32_t IndentLevel = 0,
bool Upper =
false) {
250 return FormattedBytes(Bytes, IndentLevel, FirstByteOffset, NumPerLine,
251 ByteGroupSize, Upper,
false);
254 inline FormattedBytes
255 format_bytes_with_ascii(ArrayRef<uint8_t> Bytes,
256 std::optional<uint64_t> FirstByteOffset = std::nullopt,
257 uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4,
258 uint32_t IndentLevel = 0,
bool Upper =
false) {
259 return FormattedBytes(Bytes, IndentLevel, FirstByteOffset, NumPerLine,
260 ByteGroupSize, Upper,
true);