diff options
author | asynts <asynts@gmail.com> | 2020-12-30 12:14:15 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-12-30 20:33:53 +0100 |
commit | 7e62ffbc6ea3d6ee9300c2c7c7361cb528db1808 (patch) | |
tree | 65bf0ae501594a2482921a95514fe4cec1f098f2 /AK | |
parent | 865f5ed4f6bb6f6a756946f6b3ef3f021ac9554c (diff) | |
download | serenity-7e62ffbc6ea3d6ee9300c2c7c7361cb528db1808.zip |
AK+Format: Remove TypeErasedFormatParams& from format function.
Diffstat (limited to 'AK')
-rw-r--r-- | AK/Format.cpp | 110 | ||||
-rw-r--r-- | AK/Format.h | 47 | ||||
-rw-r--r-- | AK/JsonValue.h | 4 | ||||
-rw-r--r-- | AK/LexicalPath.h | 4 | ||||
-rw-r--r-- | AK/NonnullOwnPtr.h | 4 | ||||
-rw-r--r-- | AK/NonnullRefPtr.h | 4 | ||||
-rw-r--r-- | AK/StringImpl.h | 4 | ||||
-rw-r--r-- | AK/Tests/TestFormat.cpp | 4 | ||||
-rw-r--r-- | AK/URL.h | 4 | ||||
-rw-r--r-- | AK/WeakPtr.h | 6 |
10 files changed, 89 insertions, 102 deletions
diff --git a/AK/Format.cpp b/AK/Format.cpp index 7fa10d3268..14ed0459dd 100644 --- a/AK/Format.cpp +++ b/AK/Format.cpp @@ -98,44 +98,32 @@ void vformat_impl(TypeErasedFormatParams& params, FormatBuilder& builder, Format } // namespace AK::{anonymous} -size_t TypeErasedFormatParams::decode(size_t value, size_t default_value) +size_t TypeErasedParameter::to_size() const { - if (value == StandardFormatter::value_not_set) - return default_value; - - if (value == StandardFormatter::value_from_next_arg) - value = StandardFormatter::value_from_arg + take_next_index(); - - if (value >= StandardFormatter::value_from_arg) { - const auto parameter = parameters().at(value - StandardFormatter::value_from_arg); - - Optional<i64> svalue; - if (parameter.type == TypeErasedParameter::Type::UInt8) - value = *reinterpret_cast<const u8*>(parameter.value); - else if (parameter.type == TypeErasedParameter::Type::UInt16) - value = *reinterpret_cast<const u16*>(parameter.value); - else if (parameter.type == TypeErasedParameter::Type::UInt32) - value = *reinterpret_cast<const u32*>(parameter.value); - else if (parameter.type == TypeErasedParameter::Type::UInt64) - value = *reinterpret_cast<const u64*>(parameter.value); - else if (parameter.type == TypeErasedParameter::Type::Int8) - svalue = *reinterpret_cast<const i8*>(parameter.value); - else if (parameter.type == TypeErasedParameter::Type::Int16) - svalue = *reinterpret_cast<const i16*>(parameter.value); - else if (parameter.type == TypeErasedParameter::Type::Int32) - svalue = *reinterpret_cast<const i32*>(parameter.value); - else if (parameter.type == TypeErasedParameter::Type::Int64) - svalue = *reinterpret_cast<const i64*>(parameter.value); - else - ASSERT_NOT_REACHED(); + i64 svalue; + + if (type == TypeErasedParameter::Type::UInt8) + svalue = *reinterpret_cast<const u8*>(value); + else if (type == TypeErasedParameter::Type::UInt16) + svalue = *reinterpret_cast<const u16*>(value); + else if (type == TypeErasedParameter::Type::UInt32) + svalue = *reinterpret_cast<const u32*>(value); + else if (type == TypeErasedParameter::Type::UInt64) + svalue = *reinterpret_cast<const u64*>(value); + else if (type == TypeErasedParameter::Type::Int8) + svalue = *reinterpret_cast<const i8*>(value); + else if (type == TypeErasedParameter::Type::Int16) + svalue = *reinterpret_cast<const i16*>(value); + else if (type == TypeErasedParameter::Type::Int32) + svalue = *reinterpret_cast<const i32*>(value); + else if (type == TypeErasedParameter::Type::Int64) + svalue = *reinterpret_cast<const i64*>(value); + else + ASSERT_NOT_REACHED(); - if (svalue.has_value()) { - ASSERT(svalue.value() >= 0); - value = static_cast<size_t>(svalue.value()); - } - } + ASSERT(svalue >= 0); - return value; + return static_cast<size_t>(svalue); } FormatParser::FormatParser(StringView input) @@ -467,7 +455,7 @@ void StandardFormatter::parse(TypeErasedFormatParams& params, FormatParser& pars if (index == use_next_index) index = params.take_next_index(); - m_width = value_from_arg + index; + m_width = params.parameters().at(index).to_size(); } else if (size_t width = 0; parser.consume_number(width)) { m_width = width; } @@ -477,7 +465,7 @@ void StandardFormatter::parse(TypeErasedFormatParams& params, FormatParser& pars if (index == use_next_index) index = params.take_next_index(); - m_precision = value_from_arg + index; + m_precision = params.parameters().at(index).to_size(); } else if (size_t precision = 0; parser.consume_number(precision)) { m_precision = precision; } @@ -514,7 +502,7 @@ void StandardFormatter::parse(TypeErasedFormatParams& params, FormatParser& pars ASSERT(parser.is_eof()); } -void Formatter<StringView>::format(TypeErasedFormatParams& params, FormatBuilder& builder, StringView value) +void Formatter<StringView>::format(FormatBuilder& builder, StringView value) { if (m_sign_mode != FormatBuilder::SignMode::Default) ASSERT_NOT_REACHED(); @@ -524,17 +512,17 @@ void Formatter<StringView>::format(TypeErasedFormatParams& params, FormatBuilder ASSERT_NOT_REACHED(); if (m_mode != Mode::Default && m_mode != Mode::String && m_mode != Mode::Character) ASSERT_NOT_REACHED(); - if (m_width != value_not_set && m_precision != value_not_set) + if (m_width.has_value() && m_precision.has_value()) ASSERT_NOT_REACHED(); - const auto width = params.decode(m_width); - const auto precision = params.decode(m_precision, NumericLimits<size_t>::max()); + m_width = m_width.value_or(0); + m_precision = m_precision.value_or(NumericLimits<size_t>::max()); - builder.put_string(value, m_align, width, precision, m_fill); + builder.put_string(value, m_align, m_width.value(), m_precision.value(), m_fill); } template<typename T> -void Formatter<T, typename EnableIf<IsIntegral<T>::value>::Type>::format(TypeErasedFormatParams& params, FormatBuilder& builder, T value) +void Formatter<T, typename EnableIf<IsIntegral<T>::value>::Type>::format(FormatBuilder& builder, T value) { if (m_mode == Mode::Character) { // FIXME: We just support ASCII for now, in the future maybe unicode? @@ -543,10 +531,10 @@ void Formatter<T, typename EnableIf<IsIntegral<T>::value>::Type>::format(TypeEra m_mode = Mode::String; Formatter<StringView> formatter { *this }; - return formatter.format(params, builder, StringView { reinterpret_cast<const char*>(&value), 1 }); + return formatter.format(builder, StringView { reinterpret_cast<const char*>(&value), 1 }); } - if (m_precision != NumericLimits<size_t>::max()) + if (m_precision.has_value()) ASSERT_NOT_REACHED(); if (m_mode == Mode::Pointer) { @@ -556,7 +544,7 @@ void Formatter<T, typename EnableIf<IsIntegral<T>::value>::Type>::format(TypeEra ASSERT_NOT_REACHED(); if (m_alternative_form) ASSERT_NOT_REACHED(); - if (m_width != value_not_set) + if (m_width.has_value()) ASSERT_NOT_REACHED(); m_mode = Mode::Hexadecimal; @@ -585,37 +573,37 @@ void Formatter<T, typename EnableIf<IsIntegral<T>::value>::Type>::format(TypeEra ASSERT_NOT_REACHED(); } - const auto width = params.decode(m_width); + m_width = m_width.value_or(0); if (IsSame<typename MakeUnsigned<T>::Type, T>::value) - builder.put_u64(value, base, m_alternative_form, upper_case, m_zero_pad, m_align, width, m_fill, m_sign_mode); + builder.put_u64(value, base, m_alternative_form, upper_case, m_zero_pad, m_align, m_width.value(), m_fill, m_sign_mode); else - builder.put_i64(value, base, m_alternative_form, upper_case, m_zero_pad, m_align, width, m_fill, m_sign_mode); + builder.put_i64(value, base, m_alternative_form, upper_case, m_zero_pad, m_align, m_width.value(), m_fill, m_sign_mode); } -void Formatter<char>::format(TypeErasedFormatParams& params, FormatBuilder& builder, char value) +void Formatter<char>::format(FormatBuilder& builder, char value) { if (m_mode == Mode::Binary || m_mode == Mode::BinaryUppercase || m_mode == Mode::Decimal || m_mode == Mode::Octal || m_mode == Mode::Hexadecimal || m_mode == Mode::HexadecimalUppercase) { // Trick: signed char != char. (Sometimes weird features are actually helpful.) Formatter<signed char> formatter { *this }; - return formatter.format(params, builder, static_cast<signed char>(value)); + return formatter.format(builder, static_cast<signed char>(value)); } else { Formatter<StringView> formatter { *this }; - return formatter.format(params, builder, { &value, 1 }); + return formatter.format(builder, { &value, 1 }); } } -void Formatter<bool>::format(TypeErasedFormatParams& params, FormatBuilder& builder, bool value) +void Formatter<bool>::format(FormatBuilder& builder, bool value) { if (m_mode == Mode::Binary || m_mode == Mode::BinaryUppercase || m_mode == Mode::Decimal || m_mode == Mode::Octal || m_mode == Mode::Hexadecimal || m_mode == Mode::HexadecimalUppercase) { Formatter<u8> formatter { *this }; - return formatter.format(params, builder, static_cast<u8>(value)); + return formatter.format(builder, static_cast<u8>(value)); } else { Formatter<StringView> formatter { *this }; - return formatter.format(params, builder, value ? "true" : "false"); + return formatter.format(builder, value ? "true" : "false"); } } #ifndef KERNEL -void Formatter<double>::format(TypeErasedFormatParams& params, FormatBuilder& builder, double value) +void Formatter<double>::format(FormatBuilder& builder, double value) { u8 base; bool upper_case; @@ -632,15 +620,15 @@ void Formatter<double>::format(TypeErasedFormatParams& params, FormatBuilder& bu ASSERT_NOT_REACHED(); } - const auto width = params.decode(m_width); - const auto precision = params.decode(m_precision, 6); + m_width = m_width.value_or(0); + m_precision = m_precision.value_or(6); - builder.put_f64(value, base, upper_case, m_align, width, precision, m_fill, m_sign_mode); + builder.put_f64(value, base, upper_case, m_align, m_width.value(), m_precision.value(), m_fill, m_sign_mode); } -void Formatter<float>::format(TypeErasedFormatParams& params, FormatBuilder& builder, float value) +void Formatter<float>::format(FormatBuilder& builder, float value) { Formatter<double> formatter { *this }; - formatter.format(params, builder, value); + formatter.format(builder, value); } #endif diff --git a/AK/Format.h b/AK/Format.h index b269d70ac6..ea2bd6edf7 100644 --- a/AK/Format.h +++ b/AK/Format.h @@ -28,6 +28,7 @@ #include <AK/Array.h> #include <AK/GenericLexer.h> +#include <AK/Optional.h> #include <AK/StringView.h> #ifndef KERNEL @@ -94,6 +95,10 @@ struct TypeErasedParameter { return Type::Custom; } + size_t to_size() const; + + // FIXME: Getters and setters. + const void* value; Type type; void (*formatter)(TypeErasedFormatParams&, FormatBuilder&, FormatParser&, const void* value); @@ -197,8 +202,6 @@ public: void set_parameters(Span<const TypeErasedParameter> parameters) { m_parameters = parameters; } size_t take_next_index() { return m_next_index++; } - size_t decode(size_t value, size_t default_value = 0); - private: Span<const TypeErasedParameter> m_parameters; size_t m_next_index { 0 }; @@ -210,7 +213,7 @@ void __format_value(TypeErasedFormatParams& params, FormatBuilder& builder, Form Formatter<T> formatter; formatter.parse(params, parser); - formatter.format(params, builder, *static_cast<const T*>(value)); + formatter.format(builder, *static_cast<const T*>(value)); } template<typename... Parameters> @@ -249,18 +252,14 @@ struct StandardFormatter { HexfloatUppercase, }; - static constexpr size_t value_not_set = NumericLimits<size_t>::max(); - static constexpr size_t value_from_next_arg = NumericLimits<size_t>::max() - 1; - static constexpr size_t value_from_arg = NumericLimits<size_t>::max() - max_format_arguments - 2; - FormatBuilder::Align m_align = FormatBuilder::Align::Default; FormatBuilder::SignMode m_sign_mode = FormatBuilder::SignMode::OnlyIfNeeded; Mode m_mode = Mode::Default; bool m_alternative_form = false; char m_fill = ' '; bool m_zero_pad = false; - size_t m_width = value_not_set; - size_t m_precision = value_not_set; + Optional<size_t> m_width; + Optional<size_t> m_precision; void parse(TypeErasedFormatParams&, FormatParser&); }; @@ -273,7 +272,7 @@ struct Formatter<T, typename EnableIf<IsIntegral<T>::value>::Type> : StandardFor { } - void format(TypeErasedFormatParams&, FormatBuilder&, T value); + void format(FormatBuilder&, T value); }; template<> @@ -284,17 +283,17 @@ struct Formatter<StringView> : StandardFormatter { { } - void format(TypeErasedFormatParams&, FormatBuilder&, StringView value); + void format(FormatBuilder&, StringView value); }; template<> struct Formatter<const char*> : Formatter<StringView> { - void format(TypeErasedFormatParams& params, FormatBuilder& builder, const char* value) + void format(FormatBuilder& builder, const char* value) { if (m_mode == Mode::Pointer) { Formatter<FlatPtr> formatter { *this }; - formatter.format(params, builder, reinterpret_cast<FlatPtr>(value)); + formatter.format(builder, reinterpret_cast<FlatPtr>(value)); } else { - Formatter<StringView>::format(params, builder, value); + Formatter<StringView>::format(builder, value); } } }; @@ -313,29 +312,29 @@ struct Formatter<FlyString> : Formatter<StringView> { template<typename T> struct Formatter<T*> : StandardFormatter { - void format(TypeErasedFormatParams& params, FormatBuilder& builder, T* value) + void format(FormatBuilder& builder, T* value) { if (m_mode == Mode::Default) m_mode = Mode::Pointer; Formatter<FlatPtr> formatter { *this }; - formatter.format(params, builder, reinterpret_cast<FlatPtr>(value)); + formatter.format(builder, reinterpret_cast<FlatPtr>(value)); } }; template<> struct Formatter<char> : StandardFormatter { - void format(TypeErasedFormatParams&, FormatBuilder&, char value); + void format(FormatBuilder&, char value); }; template<> struct Formatter<bool> : StandardFormatter { - void format(TypeErasedFormatParams&, FormatBuilder&, bool value); + void format(FormatBuilder&, bool value); }; #ifndef KERNEL template<> struct Formatter<float> : StandardFormatter { - void format(TypeErasedFormatParams&, FormatBuilder&, float value); + void format(FormatBuilder&, float value); }; template<> struct Formatter<double> : StandardFormatter { @@ -345,7 +344,7 @@ struct Formatter<double> : StandardFormatter { { } - void format(TypeErasedFormatParams&, FormatBuilder&, double value); + void format(FormatBuilder&, double value); }; #endif @@ -409,16 +408,16 @@ private: }; template<typename T, bool Supported = false> struct __FormatIfSupported : Formatter<StringView> { - void format(TypeErasedFormatParams& params, FormatBuilder& builder, const FormatIfSupported<T>&) + void format(FormatBuilder& builder, const FormatIfSupported<T>&) { - Formatter<StringView>::format(params, builder, "?"); + Formatter<StringView>::format(builder, "?"); } }; template<typename T> struct __FormatIfSupported<T, true> : Formatter<T> { - void format(TypeErasedFormatParams& params, FormatBuilder& builder, const FormatIfSupported<T>& value) + void format(FormatBuilder& builder, const FormatIfSupported<T>& value) { - Formatter<T>::format(params, builder, value.value()); + Formatter<T>::format(builder, value.value()); } }; template<typename T> diff --git a/AK/JsonValue.h b/AK/JsonValue.h index 4cf90ff888..d3bf23cb2c 100644 --- a/AK/JsonValue.h +++ b/AK/JsonValue.h @@ -263,9 +263,9 @@ private: template<> struct Formatter<JsonValue> : Formatter<StringView> { - void format(TypeErasedFormatParams& params, FormatBuilder& builder, const JsonValue& value) + void format(FormatBuilder& builder, const JsonValue& value) { - Formatter<StringView>::format(params, builder, value.to_string()); + Formatter<StringView>::format(builder, value.to_string()); } }; diff --git a/AK/LexicalPath.h b/AK/LexicalPath.h index f198f8c2fb..2777c17b26 100644 --- a/AK/LexicalPath.h +++ b/AK/LexicalPath.h @@ -66,9 +66,9 @@ private: template<> struct Formatter<LexicalPath> : Formatter<StringView> { - void format(TypeErasedFormatParams& params, FormatBuilder& builder, const LexicalPath& value) + void format(FormatBuilder& builder, const LexicalPath& value) { - Formatter<StringView>::format(params, builder, value.string()); + Formatter<StringView>::format(builder, value.string()); } }; diff --git a/AK/NonnullOwnPtr.h b/AK/NonnullOwnPtr.h index d06895f642..321ea67b7d 100644 --- a/AK/NonnullOwnPtr.h +++ b/AK/NonnullOwnPtr.h @@ -197,9 +197,9 @@ inline void swap(NonnullOwnPtr<T>& a, NonnullOwnPtr<U>& b) template<typename T> struct Formatter<NonnullOwnPtr<T>> : Formatter<const T*> { - void format(TypeErasedFormatParams& params, FormatBuilder& builder, const NonnullOwnPtr<T>& value) + void format(FormatBuilder& builder, const NonnullOwnPtr<T>& value) { - Formatter<const T*>::format(params, builder, value.ptr()); + Formatter<const T*>::format(builder, value.ptr()); } }; diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h index a3b8309139..734c822c73 100644 --- a/AK/NonnullRefPtr.h +++ b/AK/NonnullRefPtr.h @@ -348,9 +348,9 @@ inline const LogStream& operator<<(const LogStream& stream, const NonnullRefPtr< template<typename T> struct Formatter<NonnullRefPtr<T>> : Formatter<const T*> { - void format(TypeErasedFormatParams& params, FormatBuilder& builder, const NonnullRefPtr<T>& value) + void format(FormatBuilder& builder, const NonnullRefPtr<T>& value) { - Formatter<const T*>::format(params, builder, value.ptr()); + Formatter<const T*>::format(builder, value.ptr()); } }; diff --git a/AK/StringImpl.h b/AK/StringImpl.h index 24278ce99e..506775ef8a 100644 --- a/AK/StringImpl.h +++ b/AK/StringImpl.h @@ -132,9 +132,9 @@ constexpr u32 string_hash(const char* characters, size_t length) template<> struct Formatter<StringImpl> : Formatter<StringView> { - void format(TypeErasedFormatParams& params, FormatBuilder& builder, const StringImpl& value) + void format(FormatBuilder& builder, const StringImpl& value) { - Formatter<StringView>::format(params, builder, { value.characters(), value.length() }); + Formatter<StringView>::format(builder, { value.characters(), value.length() }); } }; diff --git a/AK/Tests/TestFormat.cpp b/AK/Tests/TestFormat.cpp index c88a006964..bcf9b91889 100644 --- a/AK/Tests/TestFormat.cpp +++ b/AK/Tests/TestFormat.cpp @@ -213,9 +213,9 @@ struct B { }; template<> struct AK::Formatter<B> : Formatter<StringView> { - void format(TypeErasedFormatParams& params, FormatBuilder& builder, B) + void format(FormatBuilder& builder, B) { - Formatter<StringView>::format(params, builder, "B"); + Formatter<StringView>::format(builder, "B"); } }; @@ -106,9 +106,9 @@ inline const LogStream& operator<<(const LogStream& stream, const URL& value) template<> struct Formatter<URL> : Formatter<StringView> { - void format(TypeErasedFormatParams& params, FormatBuilder& builder, const URL& value) + void format(FormatBuilder& builder, const URL& value) { - Formatter<StringView>::format(params, builder, value.to_string()); + Formatter<StringView>::format(builder, value.to_string()); } }; diff --git a/AK/WeakPtr.h b/AK/WeakPtr.h index bb460e3215..72d3b8af07 100644 --- a/AK/WeakPtr.h +++ b/AK/WeakPtr.h @@ -226,13 +226,13 @@ inline const LogStream& operator<<(const LogStream& stream, const WeakPtr<T>& va template<typename T> struct Formatter<WeakPtr<T>> : Formatter<const T*> { - void format(TypeErasedFormatParams& params, FormatBuilder& builder, const WeakPtr<T>& value) + void format(FormatBuilder& builder, const WeakPtr<T>& value) { #ifdef KERNEL auto ref = value.strong_ref(); - Formatter<const T*>::format(params, builder, ref.ptr()); + Formatter<const T*>::format(builder, ref.ptr()); #else - Formatter<const T*>::format(params, builder, value.ptr()); + Formatter<const T*>::format(builder, value.ptr()); #endif } }; |