mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 09:34:59 +00:00
AK/Format: Compute TypeErasedParameter type and size at compile-time
Problem: - Type and size information is known at compile-time, but computations are being performed using run-time parameters. Solution: - Move function arguments to be template arguments. - Convert to `consteval` where possible. - Decorate functions with `constexpr` which are used in both run-time and compile-time contexts.
This commit is contained in:
parent
6cbf88ad4c
commit
e117756d9f
2 changed files with 44 additions and 45 deletions
|
@ -46,16 +46,16 @@ namespace AK {
|
|||
|
||||
namespace {
|
||||
|
||||
constexpr size_t use_next_index = NumericLimits<size_t>::max();
|
||||
static constexpr size_t use_next_index = NumericLimits<size_t>::max();
|
||||
|
||||
// The worst case is that we have the largest 64-bit value formatted as binary number, this would take
|
||||
// 65 bytes. Choosing a larger power of two won't hurt and is a bit of mitigation against out-of-bounds accesses.
|
||||
inline size_t convert_unsigned_to_string(u64 value, Array<u8, 128>& buffer, u8 base, bool upper_case)
|
||||
static constexpr size_t convert_unsigned_to_string(u64 value, Array<u8, 128>& buffer, u8 base, bool upper_case)
|
||||
{
|
||||
VERIFY(base >= 2 && base <= 16);
|
||||
|
||||
static constexpr const char* lowercase_lookup = "0123456789abcdef";
|
||||
static constexpr const char* uppercase_lookup = "0123456789ABCDEF";
|
||||
constexpr const char* lowercase_lookup = "0123456789abcdef";
|
||||
constexpr const char* uppercase_lookup = "0123456789ABCDEF";
|
||||
|
||||
if (value == 0) {
|
||||
buffer[0] = '0';
|
||||
|
@ -102,34 +102,6 @@ void vformat_impl(TypeErasedFormatParams& params, FormatBuilder& builder, Format
|
|||
|
||||
} // namespace AK::{anonymous}
|
||||
|
||||
size_t TypeErasedParameter::to_size() const
|
||||
{
|
||||
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
|
||||
VERIFY_NOT_REACHED();
|
||||
|
||||
VERIFY(svalue >= 0);
|
||||
|
||||
return static_cast<size_t>(svalue);
|
||||
}
|
||||
|
||||
FormatParser::FormatParser(StringView input)
|
||||
: GenericLexer(input)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue