mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 23:37:36 +00:00
AK+Format: Keep type information for integers in TypeErasedParameter.
It's now save to pass a signed integer as parameter and then use it as replacement field (previously, this would just cast it to size_t which would be bad.)
This commit is contained in:
parent
6a2f5f4522
commit
56bfefabb6
3 changed files with 81 additions and 10 deletions
42
AK/Format.h
42
AK/Format.h
|
@ -39,7 +39,43 @@ template<typename T, typename = void>
|
|||
struct Formatter;
|
||||
|
||||
struct TypeErasedParameter {
|
||||
enum class Type {
|
||||
UInt8,
|
||||
UInt16,
|
||||
UInt32,
|
||||
UInt64,
|
||||
Int8,
|
||||
Int16,
|
||||
Int32,
|
||||
Int64,
|
||||
Custom
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
static Type get_type()
|
||||
{
|
||||
if (IsSame<T, u8>::value)
|
||||
return Type::UInt8;
|
||||
if (IsSame<T, u16>::value)
|
||||
return Type::UInt16;
|
||||
if (IsSame<T, u32>::value)
|
||||
return Type::UInt32;
|
||||
if (IsSame<T, u64>::value)
|
||||
return Type::UInt64;
|
||||
if (IsSame<T, i8>::value)
|
||||
return Type::Int8;
|
||||
if (IsSame<T, i16>::value)
|
||||
return Type::Int16;
|
||||
if (IsSame<T, i32>::value)
|
||||
return Type::Int32;
|
||||
if (IsSame<T, i64>::value)
|
||||
return Type::Int64;
|
||||
|
||||
return Type::Custom;
|
||||
}
|
||||
|
||||
const void* value;
|
||||
Type type;
|
||||
void (*formatter)(StringBuilder& builder, const void* value, StringView flags, Span<const TypeErasedParameter> parameters);
|
||||
};
|
||||
|
||||
|
@ -92,7 +128,8 @@ struct StandardFormatter {
|
|||
};
|
||||
|
||||
static constexpr size_t value_not_set = 0;
|
||||
static constexpr size_t value_from_arg = NumericLimits<size_t>::max() - max_format_arguments;
|
||||
static constexpr size_t value_from_next_arg = NumericLimits<size_t>::max();
|
||||
static constexpr size_t value_from_arg = NumericLimits<size_t>::max() - max_format_arguments - 1;
|
||||
|
||||
Align m_align = Align::Default;
|
||||
Sign m_sign = Sign::NegativeOnly;
|
||||
|
@ -132,7 +169,8 @@ template<typename... Parameters>
|
|||
Array<TypeErasedParameter, sizeof...(Parameters)> make_type_erased_parameters(const Parameters&... parameters)
|
||||
{
|
||||
static_assert(sizeof...(Parameters) <= max_format_arguments);
|
||||
return { TypeErasedParameter { ¶meters, Detail::Format::format_value<Parameters> }... };
|
||||
|
||||
return { TypeErasedParameter { ¶meters, TypeErasedParameter::get_type<Parameters>(), Detail::Format::format_value<Parameters> }... };
|
||||
}
|
||||
|
||||
void vformat(StringBuilder& builder, StringView fmtstr, Span<const TypeErasedParameter>);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue