Browse Source

Util: Add format specifier logic to container types

master
Riyyi 2 years ago
parent
commit
14e57acfd8
  1. 54
      src/util/format/formatter.h

54
src/util/format/formatter.h

@ -8,7 +8,7 @@
#include <cassert> #include <cassert>
#include <cstddef> // size_t #include <cstddef> // size_t
#include <cstdint> // int8_t, int32_t, int64_t, uint8_t, uint32_t, uintptr_t #include <cstdint> // int8_t, uint8_t, uintptr_t
#include <map> #include <map>
#include <string> #include <string>
#include <string_view> #include <string_view>
@ -66,7 +66,7 @@ template<typename T>
struct Formatter { struct Formatter {
Specifier specifier; Specifier specifier;
constexpr void parse(Parser& parser) void parse(Parser& parser)
{ {
if constexpr (std::is_same_v<T, char>) { if constexpr (std::is_same_v<T, char>) {
parser.parseSpecifier(specifier, Parser::ParameterType::Char); parser.parseSpecifier(specifier, Parser::ParameterType::Char);
@ -197,7 +197,7 @@ 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> {
constexpr void parse(Parser& parser) void parse(Parser& parser)
{ {
parser.parseSpecifier(specifier, Parser::ParameterType::Pointer); parser.parseSpecifier(specifier, Parser::ParameterType::Pointer);
specifier.alternativeForm = true; specifier.alternativeForm = true;
@ -219,19 +219,38 @@ struct Formatter<std::nullptr_t> : Formatter<const void*> {
template<typename T> template<typename T>
struct Formatter<std::vector<T>> : Formatter<T> { struct Formatter<std::vector<T>> : Formatter<T> {
Specifier specifier;
void parse(Parser& parser)
{
parser.parseSpecifier(specifier, Parser::ParameterType::Container);
}
void format(Builder& builder, const std::vector<T>& value) const void format(Builder& builder, const std::vector<T>& value) const
{ {
builder.putString("{\n"); std::string indent = std::string(specifier.width, specifier.fill);
builder.putCharacter('{');
if (specifier.alternativeForm) {
builder.putCharacter('\n');
}
for (auto it = value.cbegin(); it != value.cend(); ++it) { for (auto it = value.cbegin(); it != value.cend(); ++it) {
builder.putString(" "); builder.putString(indent);
Formatter<T>::format(builder, *it); Formatter<T>::format(builder, *it);
// Add comma, except after the last element // Add comma, except after the last element
if (it != std::prev(value.end(), 1)) { if (it != std::prev(value.end(), 1)) {
builder.putCharacter(','); builder.putCharacter(',');
} }
else if (!specifier.alternativeForm) {
builder.putString(indent);
}
if (specifier.alternativeForm) {
builder.putCharacter('\n'); builder.putCharacter('\n');
} }
}
builder.putCharacter('}'); builder.putCharacter('}');
} }
}; };
@ -241,23 +260,42 @@ struct Formatter<std::vector<T>> : Formatter<T> {
struct Formatter<type<K, V>> \ struct Formatter<type<K, V>> \
: Formatter<K> \ : Formatter<K> \
, Formatter<V> { \ , Formatter<V> { \
Specifier specifier; \
\
void parse(Parser& parser) \
{ \
parser.parseSpecifier(specifier, Parser::ParameterType::Container); \
} \
\
void format(Builder& builder, const type<K, V>& value) const \ void format(Builder& builder, const type<K, V>& value) const \
{ \ { \
builder.putString("{\n"); \ std::string indent = std::string(specifier.width, specifier.fill); \
\
builder.putCharacter('{'); \
if (specifier.alternativeForm) { \
builder.putCharacter('\n'); \
} \
auto last = value.end(); \ auto last = value.end(); \
for (auto it = value.begin(); it != last; ++it) { \ for (auto it = value.begin(); it != last; ++it) { \
builder.putString(R"( ")"); \ builder.putString(indent); \
builder.putCharacter('"'); \
Formatter<K>::format(builder, it->first); \ Formatter<K>::format(builder, it->first); \
builder.putString(R"(": )"); \ builder.putCharacter('"'); \
builder.putString((specifier.width > 0) ? ": " : ":"); \
Formatter<V>::format(builder, it->second); \ Formatter<V>::format(builder, it->second); \
\ \
/* Add comma, except after the last element */ \ /* Add comma, except after the last element */ \
if (std::next(it) != last) { \ if (std::next(it) != last) { \
builder.putCharacter(','); \ builder.putCharacter(','); \
} \
else if (!specifier.alternativeForm) { \
builder.putString(indent); \
} \ } \
\ \
if (specifier.alternativeForm) { \
builder.putCharacter('\n'); \ builder.putCharacter('\n'); \
} \ } \
} \
builder.putCharacter('}'); \ builder.putCharacter('}'); \
} \ } \
} }

Loading…
Cancel
Save