Browse Source

Util: Use putU64 when formatting pointer types

master
Riyyi 2 years ago
parent
commit
175251496f
  1. 1
      src/util/format/builder.h
  2. 7
      src/util/format/formatter.cpp
  3. 12
      src/util/format/formatter.h

1
src/util/format/builder.h

@ -60,7 +60,6 @@ public:
void putF64(double number, uint8_t precision = 6) const;
void putCharacter(char character) const { m_builder.write(&character, 1); }
void putString(std::string_view string, char fill = ' ', Align align = Align::Left, size_t width = 0) const;
void putPointer(const void* pointer) const { m_builder << pointer; }
const std::stringstream& builder() const { return m_builder; }
std::stringstream& builder() { return m_builder; }

7
src/util/format/formatter.cpp

@ -58,9 +58,14 @@ void Formatter<const char*>::format(Builder& builder, const char* value) const
// Pointer
void Formatter<std::nullptr_t>::parse(Parser& parser)
{
parser.parseSpecifier(specifier, Parser::ParameterType::Pointer);
}
void Formatter<std::nullptr_t>::format(Builder& builder, std::nullptr_t) const
{
Formatter<const void*>::format(builder, 0);
Formatter<std::string_view>::format(builder, "nullptr");
}
} // namespace Util::Format

12
src/util/format/formatter.h

@ -8,7 +8,7 @@
#include <cassert>
#include <cstddef> // size_t
#include <cstdint> // int8_t, int32_t, int64_t, uint8_t, uint32_t
#include <cstdint> // int8_t, int32_t, int64_t, uint8_t, uint32_t, uintptr_t
#include <map>
#include <string>
#include <string_view>
@ -193,7 +193,7 @@ struct Formatter<char[N]> : Formatter<const char*> {
// Pointer
template<typename T>
struct Formatter<T*> {
struct Formatter<T*> : Formatter<uintptr_t> {
Specifier specifier;
constexpr void parse(Parser& parser)
@ -203,14 +203,14 @@ struct Formatter<T*> {
void format(Builder& builder, T* value) const
{
value == nullptr
? builder.putString("nullptr")
: builder.putPointer(static_cast<const void*>(value));
builder.putU64(reinterpret_cast<uintptr_t>(value), 16, false, specifier.fill, specifier.align,
Builder::Sign::None, true, false, specifier.width);
}
};
template<>
struct Formatter<std::nullptr_t> : Formatter<const void*> {
struct Formatter<std::nullptr_t> : Formatter<std::string_view> {
void parse(Parser& parser);
void format(Builder& builder, std::nullptr_t) const;
};

Loading…
Cancel
Save