Browse Source

Util: Call integral formatter from pointer type

master
Riyyi 2 years ago
parent
commit
dc8d2a49cd
  1. 14
      src/util/format/formatter.cpp
  2. 10
      src/util/format/formatter.h

14
src/util/format/formatter.cpp

@ -46,9 +46,10 @@ void Formatter<const char*>::parse(Parser& parser)
void Formatter<const char*>::format(Builder& builder, const char* value) const void Formatter<const char*>::format(Builder& builder, const char* value) const
{ {
if (specifier.type == PresentationType::Pointer) { if (specifier.type == PresentationType::Pointer) {
Formatter<const void*> formatter { .specifier = specifier }; Formatter<uintptr_t> formatter { .specifier = specifier };
formatter.format(builder, static_cast<const void*>(value)); formatter.specifier.alternativeForm = true;
return; formatter.specifier.type = PresentationType::Hex;
return formatter.format(builder, reinterpret_cast<uintptr_t>(value));
} }
Formatter<std::string_view>::format( Formatter<std::string_view>::format(
@ -58,14 +59,9 @@ void Formatter<const char*>::format(Builder& builder, const char* value) const
// Pointer // 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 void Formatter<std::nullptr_t>::format(Builder& builder, std::nullptr_t) const
{ {
Formatter<std::string_view>::format(builder, "nullptr"); Formatter<const void*>::format(builder, 0);
} }
} // namespace Util::Format } // namespace Util::Format

10
src/util/format/formatter.h

@ -194,23 +194,21 @@ struct Formatter<char[N]> : Formatter<const char*> {
template<typename T> template<typename T>
struct Formatter<T*> : Formatter<uintptr_t> { struct Formatter<T*> : Formatter<uintptr_t> {
Specifier specifier;
constexpr void parse(Parser& parser) constexpr void parse(Parser& parser)
{ {
parser.parseSpecifier(specifier, Parser::ParameterType::Pointer); parser.parseSpecifier(specifier, Parser::ParameterType::Pointer);
specifier.alternativeForm = true;
specifier.type = PresentationType::Hex;
} }
void format(Builder& builder, T* value) const void format(Builder& builder, T* value) const
{ {
builder.putU64(reinterpret_cast<uintptr_t>(value), 16, false, specifier.fill, specifier.align, Formatter<uintptr_t>::format(builder, reinterpret_cast<uintptr_t>(value));
Builder::Sign::None, true, false, specifier.width);
} }
}; };
template<> template<>
struct Formatter<std::nullptr_t> : Formatter<std::string_view> { struct Formatter<std::nullptr_t> : Formatter<const void*> {
void parse(Parser& parser);
void format(Builder& builder, std::nullptr_t) const; void format(Builder& builder, std::nullptr_t) const;
}; };

Loading…
Cancel
Save