mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 16:37:47 +00:00
AK: Convert AK::Format formatting helpers to returning ErrorOr<void>
This isn't a complete conversion to ErrorOr<void>, but a good chunk. The end goal here is to propagate buffer allocation failures to the caller, and allow the use of TRY() with formatting functions.
This commit is contained in:
parent
008355c222
commit
216e21a1fa
87 changed files with 450 additions and 448 deletions
|
@ -271,7 +271,7 @@ private:
|
|||
|
||||
template<typename T, typename X, bool Incr, bool Cmp, bool Bool, bool Flags, bool Shift, bool Arith>
|
||||
struct Formatter<DistinctNumeric<T, X, Incr, Cmp, Bool, Flags, Shift, Arith>> : Formatter<T> {
|
||||
void format(FormatBuilder& builder, DistinctNumeric<T, X, Incr, Cmp, Bool, Flags, Shift, Arith> value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, DistinctNumeric<T, X, Incr, Cmp, Bool, Flags, Shift, Arith> value)
|
||||
{
|
||||
return Formatter<T>::format(builder, value.value());
|
||||
}
|
||||
|
|
11
AK/Error.h
11
AK/Error.h
|
@ -6,7 +6,6 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Format.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Try.h>
|
||||
|
@ -131,16 +130,6 @@ private:
|
|||
Optional<ErrorType> m_error;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Formatter<Error> : Formatter<FormatString> {
|
||||
void format(FormatBuilder& builder, Error const& error)
|
||||
{
|
||||
if (error.is_errno())
|
||||
return Formatter<FormatString>::format(builder, "Error(errno={})", error.code());
|
||||
return Formatter<FormatString>::format(builder, "Error({})", error.string_literal());
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
using AK::Error;
|
||||
|
|
|
@ -322,10 +322,10 @@ struct Formatter<FixedPoint<precision, Underlying>> : StandardFormatter {
|
|||
{
|
||||
}
|
||||
|
||||
void format(FormatBuilder& builder, FixedPoint<precision, Underlying> value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, FixedPoint<precision, Underlying> value)
|
||||
{
|
||||
Formatter<double> formatter { *this };
|
||||
formatter.format(builder, (double)value);
|
||||
return formatter.format(builder, (double)value);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
226
AK/Format.cpp
226
AK/Format.cpp
|
@ -75,15 +75,15 @@ static constexpr size_t convert_unsigned_to_string(u64 value, Array<u8, 128>& bu
|
|||
return used;
|
||||
}
|
||||
|
||||
void vformat_impl(TypeErasedFormatParams& params, FormatBuilder& builder, FormatParser& parser)
|
||||
ErrorOr<void> vformat_impl(TypeErasedFormatParams& params, FormatBuilder& builder, FormatParser& parser)
|
||||
{
|
||||
const auto literal = parser.consume_literal();
|
||||
builder.put_literal(literal);
|
||||
TRY(builder.put_literal(literal));
|
||||
|
||||
FormatParser::FormatSpecifier specifier;
|
||||
if (!parser.consume_specifier(specifier)) {
|
||||
VERIFY(parser.is_eof());
|
||||
return;
|
||||
return {};
|
||||
}
|
||||
|
||||
if (specifier.index == use_next_index)
|
||||
|
@ -92,9 +92,9 @@ void vformat_impl(TypeErasedFormatParams& params, FormatBuilder& builder, Format
|
|||
auto& parameter = params.parameters().at(specifier.index);
|
||||
|
||||
FormatParser argparser { specifier.flags };
|
||||
parameter.formatter(params, builder, argparser, parameter.value);
|
||||
|
||||
vformat_impl(params, builder, parser);
|
||||
TRY(parameter.formatter(params, builder, argparser, parameter.value));
|
||||
TRY(vformat_impl(params, builder, parser));
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace AK::{anonymous}
|
||||
|
@ -189,20 +189,23 @@ bool FormatParser::consume_replacement_field(size_t& index)
|
|||
return true;
|
||||
}
|
||||
|
||||
void FormatBuilder::put_padding(char fill, size_t amount)
|
||||
ErrorOr<void> FormatBuilder::put_padding(char fill, size_t amount)
|
||||
{
|
||||
for (size_t i = 0; i < amount; ++i)
|
||||
m_builder.append(fill);
|
||||
TRY(m_builder.try_append(fill));
|
||||
return {};
|
||||
}
|
||||
void FormatBuilder::put_literal(StringView value)
|
||||
ErrorOr<void> FormatBuilder::put_literal(StringView value)
|
||||
{
|
||||
for (size_t i = 0; i < value.length(); ++i) {
|
||||
m_builder.append(value[i]);
|
||||
TRY(m_builder.try_append(value[i]));
|
||||
if (value[i] == '{' || value[i] == '}')
|
||||
++i;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
void FormatBuilder::put_string(
|
||||
|
||||
ErrorOr<void> FormatBuilder::put_string(
|
||||
StringView value,
|
||||
Align align,
|
||||
size_t min_width,
|
||||
|
@ -216,21 +219,23 @@ void FormatBuilder::put_string(
|
|||
value = value.substring_view(0, used_by_string);
|
||||
|
||||
if (align == Align::Left || align == Align::Default) {
|
||||
m_builder.append(value);
|
||||
put_padding(fill, used_by_padding);
|
||||
TRY(m_builder.try_append(value));
|
||||
TRY(put_padding(fill, used_by_padding));
|
||||
} else if (align == Align::Center) {
|
||||
const auto used_by_left_padding = used_by_padding / 2;
|
||||
const auto used_by_right_padding = ceil_div<size_t, size_t>(used_by_padding, 2);
|
||||
|
||||
put_padding(fill, used_by_left_padding);
|
||||
m_builder.append(value);
|
||||
put_padding(fill, used_by_right_padding);
|
||||
TRY(put_padding(fill, used_by_left_padding));
|
||||
TRY(m_builder.try_append(value));
|
||||
TRY(put_padding(fill, used_by_right_padding));
|
||||
} else if (align == Align::Right) {
|
||||
put_padding(fill, used_by_padding);
|
||||
m_builder.append(value);
|
||||
TRY(put_padding(fill, used_by_padding));
|
||||
TRY(m_builder.try_append(value));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
void FormatBuilder::put_u64(
|
||||
|
||||
ErrorOr<void> FormatBuilder::put_u64(
|
||||
u64 value,
|
||||
u8 base,
|
||||
bool prefix,
|
||||
|
@ -271,64 +276,69 @@ void FormatBuilder::put_u64(
|
|||
const auto used_by_field = used_by_prefix + used_by_digits;
|
||||
const auto used_by_padding = max(used_by_field, min_width) - used_by_field;
|
||||
|
||||
const auto put_prefix = [&]() {
|
||||
const auto put_prefix = [&]() -> ErrorOr<void> {
|
||||
if (is_negative)
|
||||
m_builder.append('-');
|
||||
TRY(m_builder.try_append('-'));
|
||||
else if (sign_mode == SignMode::Always)
|
||||
m_builder.append('+');
|
||||
TRY(m_builder.try_append('+'));
|
||||
else if (sign_mode == SignMode::Reserved)
|
||||
m_builder.append(' ');
|
||||
TRY(m_builder.try_append(' '));
|
||||
|
||||
if (prefix) {
|
||||
if (base == 2) {
|
||||
if (upper_case)
|
||||
m_builder.append("0B");
|
||||
TRY(m_builder.try_append("0B"));
|
||||
else
|
||||
m_builder.append("0b");
|
||||
TRY(m_builder.try_append("0b"));
|
||||
} else if (base == 8) {
|
||||
m_builder.append("0");
|
||||
TRY(m_builder.try_append("0"));
|
||||
} else if (base == 16) {
|
||||
if (upper_case)
|
||||
m_builder.append("0X");
|
||||
TRY(m_builder.try_append("0X"));
|
||||
else
|
||||
m_builder.append("0x");
|
||||
TRY(m_builder.try_append("0x"));
|
||||
}
|
||||
}
|
||||
return {};
|
||||
};
|
||||
const auto put_digits = [&]() {
|
||||
|
||||
const auto put_digits = [&]() -> ErrorOr<void> {
|
||||
for (size_t i = 0; i < used_by_digits; ++i)
|
||||
m_builder.append(buffer[i]);
|
||||
TRY(m_builder.try_append(buffer[i]));
|
||||
return {};
|
||||
};
|
||||
|
||||
if (align == Align::Left) {
|
||||
const auto used_by_right_padding = used_by_padding;
|
||||
|
||||
put_prefix();
|
||||
put_digits();
|
||||
put_padding(fill, used_by_right_padding);
|
||||
TRY(put_prefix());
|
||||
TRY(put_digits());
|
||||
TRY(put_padding(fill, used_by_right_padding));
|
||||
} else if (align == Align::Center) {
|
||||
const auto used_by_left_padding = used_by_padding / 2;
|
||||
const auto used_by_right_padding = ceil_div<size_t, size_t>(used_by_padding, 2);
|
||||
|
||||
put_padding(fill, used_by_left_padding);
|
||||
put_prefix();
|
||||
put_digits();
|
||||
put_padding(fill, used_by_right_padding);
|
||||
TRY(put_padding(fill, used_by_left_padding));
|
||||
TRY(put_prefix());
|
||||
TRY(put_digits());
|
||||
TRY(put_padding(fill, used_by_right_padding));
|
||||
} else if (align == Align::Right) {
|
||||
const auto used_by_left_padding = used_by_padding;
|
||||
|
||||
if (zero_pad) {
|
||||
put_prefix();
|
||||
put_padding('0', used_by_left_padding);
|
||||
put_digits();
|
||||
TRY(put_prefix());
|
||||
TRY(put_padding('0', used_by_left_padding));
|
||||
TRY(put_digits());
|
||||
} else {
|
||||
put_padding(fill, used_by_left_padding);
|
||||
put_prefix();
|
||||
put_digits();
|
||||
TRY(put_padding(fill, used_by_left_padding));
|
||||
TRY(put_prefix());
|
||||
TRY(put_digits());
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
void FormatBuilder::put_i64(
|
||||
|
||||
ErrorOr<void> FormatBuilder::put_i64(
|
||||
i64 value,
|
||||
u8 base,
|
||||
bool prefix,
|
||||
|
@ -342,11 +352,12 @@ void FormatBuilder::put_i64(
|
|||
const auto is_negative = value < 0;
|
||||
value = is_negative ? -value : value;
|
||||
|
||||
put_u64(static_cast<u64>(value), base, prefix, upper_case, zero_pad, align, min_width, fill, sign_mode, is_negative);
|
||||
TRY(put_u64(static_cast<u64>(value), base, prefix, upper_case, zero_pad, align, min_width, fill, sign_mode, is_negative));
|
||||
return {};
|
||||
}
|
||||
|
||||
#ifndef KERNEL
|
||||
void FormatBuilder::put_f64(
|
||||
ErrorOr<void> FormatBuilder::put_f64(
|
||||
double value,
|
||||
u8 base,
|
||||
bool upper_case,
|
||||
|
@ -362,25 +373,25 @@ void FormatBuilder::put_f64(
|
|||
|
||||
if (isnan(value) || isinf(value)) [[unlikely]] {
|
||||
if (value < 0.0)
|
||||
string_builder.append('-');
|
||||
TRY(string_builder.try_append('-'));
|
||||
else if (sign_mode == SignMode::Always)
|
||||
string_builder.append('+');
|
||||
TRY(string_builder.try_append('+'));
|
||||
else if (sign_mode == SignMode::Reserved)
|
||||
string_builder.append(' ');
|
||||
TRY(string_builder.try_append(' '));
|
||||
|
||||
if (isnan(value))
|
||||
string_builder.append(upper_case ? "NAN"sv : "nan"sv);
|
||||
TRY(string_builder.try_append(upper_case ? "NAN"sv : "nan"sv));
|
||||
else
|
||||
string_builder.append(upper_case ? "INF"sv : "inf"sv);
|
||||
put_string(string_builder.string_view(), align, min_width, NumericLimits<size_t>::max(), fill);
|
||||
return;
|
||||
TRY(string_builder.try_append(upper_case ? "INF"sv : "inf"sv));
|
||||
TRY(put_string(string_builder.string_view(), align, min_width, NumericLimits<size_t>::max(), fill));
|
||||
return {};
|
||||
}
|
||||
|
||||
bool is_negative = value < 0.0;
|
||||
if (is_negative)
|
||||
value = -value;
|
||||
|
||||
format_builder.put_u64(static_cast<u64>(value), base, false, upper_case, false, Align::Right, 0, ' ', sign_mode, is_negative);
|
||||
TRY(format_builder.put_u64(static_cast<u64>(value), base, false, upper_case, false, Align::Right, 0, ' ', sign_mode, is_negative));
|
||||
|
||||
if (precision > 0) {
|
||||
// FIXME: This is a terrible approximation but doing it properly would be a lot of work. If someone is up for that, a good
|
||||
|
@ -401,19 +412,20 @@ void FormatBuilder::put_f64(
|
|||
}
|
||||
|
||||
if (zero_pad || visible_precision > 0)
|
||||
string_builder.append('.');
|
||||
TRY(string_builder.try_append('.'));
|
||||
|
||||
if (visible_precision > 0)
|
||||
format_builder.put_u64(static_cast<u64>(value), base, false, upper_case, true, Align::Right, visible_precision);
|
||||
TRY(format_builder.put_u64(static_cast<u64>(value), base, false, upper_case, true, Align::Right, visible_precision));
|
||||
|
||||
if (zero_pad && (precision - visible_precision) > 0)
|
||||
format_builder.put_u64(0, base, false, false, true, Align::Right, precision - visible_precision);
|
||||
TRY(format_builder.put_u64(0, base, false, false, true, Align::Right, precision - visible_precision));
|
||||
}
|
||||
|
||||
put_string(string_builder.string_view(), align, min_width, NumericLimits<size_t>::max(), fill);
|
||||
TRY(put_string(string_builder.string_view(), align, min_width, NumericLimits<size_t>::max(), fill));
|
||||
return {};
|
||||
}
|
||||
|
||||
void FormatBuilder::put_f80(
|
||||
ErrorOr<void> FormatBuilder::put_f80(
|
||||
long double value,
|
||||
u8 base,
|
||||
bool upper_case,
|
||||
|
@ -428,25 +440,25 @@ void FormatBuilder::put_f80(
|
|||
|
||||
if (isnan(value) || isinf(value)) [[unlikely]] {
|
||||
if (value < 0.0l)
|
||||
string_builder.append('-');
|
||||
TRY(string_builder.try_append('-'));
|
||||
else if (sign_mode == SignMode::Always)
|
||||
string_builder.append('+');
|
||||
TRY(string_builder.try_append('+'));
|
||||
else if (sign_mode == SignMode::Reserved)
|
||||
string_builder.append(' ');
|
||||
TRY(string_builder.try_append(' '));
|
||||
|
||||
if (isnan(value))
|
||||
string_builder.append(upper_case ? "NAN"sv : "nan"sv);
|
||||
TRY(string_builder.try_append(upper_case ? "NAN"sv : "nan"sv));
|
||||
else
|
||||
string_builder.append(upper_case ? "INF"sv : "inf"sv);
|
||||
put_string(string_builder.string_view(), align, min_width, NumericLimits<size_t>::max(), fill);
|
||||
return;
|
||||
TRY(string_builder.try_append(upper_case ? "INF"sv : "inf"sv));
|
||||
TRY(put_string(string_builder.string_view(), align, min_width, NumericLimits<size_t>::max(), fill));
|
||||
return {};
|
||||
}
|
||||
|
||||
bool is_negative = value < 0.0l;
|
||||
if (is_negative)
|
||||
value = -value;
|
||||
|
||||
format_builder.put_u64(static_cast<u64>(value), base, false, upper_case, false, Align::Right, 0, ' ', sign_mode, is_negative);
|
||||
TRY(format_builder.put_u64(static_cast<u64>(value), base, false, upper_case, false, Align::Right, 0, ' ', sign_mode, is_negative));
|
||||
|
||||
if (precision > 0) {
|
||||
// FIXME: This is a terrible approximation but doing it properly would be a lot of work. If someone is up for that, a good
|
||||
|
@ -468,44 +480,50 @@ void FormatBuilder::put_f80(
|
|||
|
||||
if (visible_precision > 0) {
|
||||
string_builder.append('.');
|
||||
format_builder.put_u64(static_cast<u64>(value), base, false, upper_case, true, Align::Right, visible_precision);
|
||||
TRY(format_builder.put_u64(static_cast<u64>(value), base, false, upper_case, true, Align::Right, visible_precision));
|
||||
}
|
||||
}
|
||||
|
||||
put_string(string_builder.string_view(), align, min_width, NumericLimits<size_t>::max(), fill);
|
||||
TRY(put_string(string_builder.string_view(), align, min_width, NumericLimits<size_t>::max(), fill));
|
||||
return {};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void FormatBuilder::put_hexdump(ReadonlyBytes bytes, size_t width, char fill)
|
||||
ErrorOr<void> FormatBuilder::put_hexdump(ReadonlyBytes bytes, size_t width, char fill)
|
||||
{
|
||||
auto put_char_view = [&](auto i) {
|
||||
put_padding(fill, 4);
|
||||
auto put_char_view = [&](auto i) -> ErrorOr<void> {
|
||||
TRY(put_padding(fill, 4));
|
||||
for (size_t j = i - width; j < i; ++j) {
|
||||
auto ch = bytes[j];
|
||||
m_builder.append(ch >= 32 && ch <= 127 ? ch : '.'); // silly hack
|
||||
TRY(m_builder.try_append(ch >= 32 && ch <= 127 ? ch : '.')); // silly hack
|
||||
}
|
||||
return {};
|
||||
};
|
||||
|
||||
for (size_t i = 0; i < bytes.size(); ++i) {
|
||||
if (width > 0) {
|
||||
if (i % width == 0 && i) {
|
||||
put_char_view(i);
|
||||
put_literal("\n"sv);
|
||||
TRY(put_char_view(i));
|
||||
TRY(put_literal("\n"sv));
|
||||
}
|
||||
}
|
||||
put_u64(bytes[i], 16, false, false, true, Align::Right, 2);
|
||||
TRY(put_u64(bytes[i], 16, false, false, true, Align::Right, 2));
|
||||
}
|
||||
|
||||
if (width > 0 && bytes.size() && bytes.size() % width == 0)
|
||||
put_char_view(bytes.size());
|
||||
TRY(put_char_view(bytes.size()));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void vformat(StringBuilder& builder, StringView fmtstr, TypeErasedFormatParams& params)
|
||||
ErrorOr<void> vformat(StringBuilder& builder, StringView fmtstr, TypeErasedFormatParams& params)
|
||||
{
|
||||
FormatBuilder fmtbuilder { builder };
|
||||
FormatParser parser { fmtstr };
|
||||
|
||||
vformat_impl(params, fmtbuilder, parser);
|
||||
TRY(vformat_impl(params, fmtbuilder, parser));
|
||||
return {};
|
||||
}
|
||||
|
||||
void StandardFormatter::parse(TypeErasedFormatParams& params, FormatParser& parser)
|
||||
|
@ -588,7 +606,7 @@ void StandardFormatter::parse(TypeErasedFormatParams& params, FormatParser& pars
|
|||
VERIFY(parser.is_eof());
|
||||
}
|
||||
|
||||
void Formatter<StringView>::format(FormatBuilder& builder, StringView value)
|
||||
ErrorOr<void> Formatter<StringView>::format(FormatBuilder& builder, StringView value)
|
||||
{
|
||||
if (m_sign_mode != FormatBuilder::SignMode::Default)
|
||||
VERIFY_NOT_REACHED();
|
||||
|
@ -603,20 +621,20 @@ void Formatter<StringView>::format(FormatBuilder& builder, StringView value)
|
|||
m_precision = m_precision.value_or(NumericLimits<size_t>::max());
|
||||
|
||||
if (m_mode == Mode::HexDump)
|
||||
builder.put_hexdump(value.bytes(), m_width.value(), m_fill);
|
||||
else
|
||||
builder.put_string(value, m_align, m_width.value(), m_precision.value(), m_fill);
|
||||
return builder.put_hexdump(value.bytes(), m_width.value(), m_fill);
|
||||
return builder.put_string(value, m_align, m_width.value(), m_precision.value(), m_fill);
|
||||
}
|
||||
|
||||
void Formatter<FormatString>::vformat(FormatBuilder& builder, StringView fmtstr, TypeErasedFormatParams& params)
|
||||
ErrorOr<void> Formatter<FormatString>::vformat(FormatBuilder& builder, StringView fmtstr, TypeErasedFormatParams& params)
|
||||
{
|
||||
StringBuilder string_builder;
|
||||
AK::vformat(string_builder, fmtstr, params);
|
||||
return Formatter<StringView>::format(builder, string_builder.string_view());
|
||||
TRY(AK::vformat(string_builder, fmtstr, params));
|
||||
TRY(Formatter<StringView>::format(builder, string_builder.string_view()));
|
||||
return {};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Formatter<T, typename EnableIf<IsIntegral<T>>::Type>::format(FormatBuilder& builder, T value)
|
||||
ErrorOr<void> Formatter<T, typename EnableIf<IsIntegral<T>>::Type>::format(FormatBuilder& builder, T value)
|
||||
{
|
||||
if (m_mode == Mode::Character) {
|
||||
// FIXME: We just support ASCII for now, in the future maybe unicode?
|
||||
|
@ -665,8 +683,7 @@ void Formatter<T, typename EnableIf<IsIntegral<T>>::Type>::format(FormatBuilder&
|
|||
upper_case = true;
|
||||
} else if (m_mode == Mode::HexDump) {
|
||||
m_width = m_width.value_or(32);
|
||||
builder.put_hexdump({ &value, sizeof(value) }, m_width.value(), m_fill);
|
||||
return;
|
||||
return builder.put_hexdump({ &value, sizeof(value) }, m_width.value(), m_fill);
|
||||
} else {
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
@ -674,12 +691,12 @@ void Formatter<T, typename EnableIf<IsIntegral<T>>::Type>::format(FormatBuilder&
|
|||
m_width = m_width.value_or(0);
|
||||
|
||||
if constexpr (IsSame<MakeUnsigned<T>, T>)
|
||||
builder.put_u64(value, base, m_alternative_form, upper_case, m_zero_pad, m_align, m_width.value(), m_fill, m_sign_mode);
|
||||
return 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, m_width.value(), m_fill, m_sign_mode);
|
||||
return 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(FormatBuilder& builder, char value)
|
||||
ErrorOr<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.)
|
||||
|
@ -690,7 +707,7 @@ void Formatter<char>::format(FormatBuilder& builder, char value)
|
|||
return formatter.format(builder, { &value, 1 });
|
||||
}
|
||||
}
|
||||
void Formatter<wchar_t>::format(FormatBuilder& builder, wchar_t value)
|
||||
ErrorOr<void> Formatter<wchar_t>::format(FormatBuilder& builder, wchar_t 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<u32> formatter { *this };
|
||||
|
@ -703,7 +720,7 @@ void Formatter<wchar_t>::format(FormatBuilder& builder, wchar_t value)
|
|||
return formatter.format(builder, codepoint.to_string());
|
||||
}
|
||||
}
|
||||
void Formatter<bool>::format(FormatBuilder& builder, bool value)
|
||||
ErrorOr<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 };
|
||||
|
@ -716,7 +733,7 @@ void Formatter<bool>::format(FormatBuilder& builder, bool value)
|
|||
}
|
||||
}
|
||||
#ifndef KERNEL
|
||||
void Formatter<long double>::format(FormatBuilder& builder, long double value)
|
||||
ErrorOr<void> Formatter<long double>::format(FormatBuilder& builder, long double value)
|
||||
{
|
||||
u8 base;
|
||||
bool upper_case;
|
||||
|
@ -736,10 +753,10 @@ void Formatter<long double>::format(FormatBuilder& builder, long double value)
|
|||
m_width = m_width.value_or(0);
|
||||
m_precision = m_precision.value_or(6);
|
||||
|
||||
builder.put_f80(value, base, upper_case, m_align, m_width.value(), m_precision.value(), m_fill, m_sign_mode);
|
||||
return builder.put_f80(value, base, upper_case, m_align, m_width.value(), m_precision.value(), m_fill, m_sign_mode);
|
||||
}
|
||||
|
||||
void Formatter<double>::format(FormatBuilder& builder, double value)
|
||||
ErrorOr<void> Formatter<double>::format(FormatBuilder& builder, double value)
|
||||
{
|
||||
u8 base;
|
||||
bool upper_case;
|
||||
|
@ -759,12 +776,13 @@ void Formatter<double>::format(FormatBuilder& builder, double value)
|
|||
m_width = m_width.value_or(0);
|
||||
m_precision = m_precision.value_or(6);
|
||||
|
||||
builder.put_f64(value, base, upper_case, m_zero_pad, m_align, m_width.value(), m_precision.value(), m_fill, m_sign_mode);
|
||||
return builder.put_f64(value, base, upper_case, m_zero_pad, m_align, m_width.value(), m_precision.value(), m_fill, m_sign_mode);
|
||||
}
|
||||
void Formatter<float>::format(FormatBuilder& builder, float value)
|
||||
|
||||
ErrorOr<void> Formatter<float>::format(FormatBuilder& builder, float value)
|
||||
{
|
||||
Formatter<double> formatter { *this };
|
||||
formatter.format(builder, value);
|
||||
return formatter.format(builder, value);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -772,7 +790,7 @@ void Formatter<float>::format(FormatBuilder& builder, float value)
|
|||
void vout(FILE* file, StringView fmtstr, TypeErasedFormatParams& params, bool newline)
|
||||
{
|
||||
StringBuilder builder;
|
||||
vformat(builder, fmtstr, params);
|
||||
MUST(vformat(builder, fmtstr, params));
|
||||
|
||||
if (newline)
|
||||
builder.append('\n');
|
||||
|
@ -832,7 +850,7 @@ void vdbgln(StringView fmtstr, TypeErasedFormatParams& params)
|
|||
# endif
|
||||
#endif
|
||||
|
||||
vformat(builder, fmtstr, params);
|
||||
MUST(vformat(builder, fmtstr, params));
|
||||
builder.append('\n');
|
||||
|
||||
const auto string = builder.string_view();
|
||||
|
@ -866,7 +884,7 @@ void vdmesgln(StringView fmtstr, TypeErasedFormatParams& params)
|
|||
}
|
||||
# endif
|
||||
|
||||
vformat(builder, fmtstr, params);
|
||||
MUST(vformat(builder, fmtstr, params));
|
||||
builder.append('\n');
|
||||
|
||||
const auto string = builder.string_view();
|
||||
|
@ -888,7 +906,7 @@ void v_critical_dmesgln(StringView fmtstr, TypeErasedFormatParams& params)
|
|||
}
|
||||
# endif
|
||||
|
||||
vformat(builder, fmtstr, params);
|
||||
MUST(vformat(builder, fmtstr, params));
|
||||
builder.append('\n');
|
||||
|
||||
const auto string = builder.string_view();
|
||||
|
|
115
AK/Format.h
115
AK/Format.h
|
@ -11,6 +11,8 @@
|
|||
#include <AK/AllOf.h>
|
||||
#include <AK/AnyOf.h>
|
||||
#include <AK/Array.h>
|
||||
#include <AK/Error.h>
|
||||
#include <AK/Forward.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/StringView.h>
|
||||
|
||||
|
@ -125,7 +127,7 @@ struct TypeErasedParameter {
|
|||
|
||||
const void* value;
|
||||
Type type;
|
||||
void (*formatter)(TypeErasedFormatParams&, FormatBuilder&, FormatParser&, const void* value);
|
||||
ErrorOr<void> (*formatter)(TypeErasedFormatParams&, FormatBuilder&, FormatParser&, void const* value);
|
||||
};
|
||||
|
||||
class FormatBuilder {
|
||||
|
@ -148,18 +150,18 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
void put_padding(char fill, size_t amount);
|
||||
ErrorOr<void> put_padding(char fill, size_t amount);
|
||||
|
||||
void put_literal(StringView value);
|
||||
ErrorOr<void> put_literal(StringView value);
|
||||
|
||||
void put_string(
|
||||
ErrorOr<void> put_string(
|
||||
StringView value,
|
||||
Align align = Align::Left,
|
||||
size_t min_width = 0,
|
||||
size_t max_width = NumericLimits<size_t>::max(),
|
||||
char fill = ' ');
|
||||
|
||||
void put_u64(
|
||||
ErrorOr<void> put_u64(
|
||||
u64 value,
|
||||
u8 base = 10,
|
||||
bool prefix = false,
|
||||
|
@ -171,7 +173,7 @@ public:
|
|||
SignMode sign_mode = SignMode::OnlyIfNeeded,
|
||||
bool is_negative = false);
|
||||
|
||||
void put_i64(
|
||||
ErrorOr<void> put_i64(
|
||||
i64 value,
|
||||
u8 base = 10,
|
||||
bool prefix = false,
|
||||
|
@ -183,7 +185,7 @@ public:
|
|||
SignMode sign_mode = SignMode::OnlyIfNeeded);
|
||||
|
||||
#ifndef KERNEL
|
||||
void put_f80(
|
||||
ErrorOr<void> put_f80(
|
||||
long double value,
|
||||
u8 base = 10,
|
||||
bool upper_case = false,
|
||||
|
@ -193,7 +195,7 @@ public:
|
|||
char fill = ' ',
|
||||
SignMode sign_mode = SignMode::OnlyIfNeeded);
|
||||
|
||||
void put_f64(
|
||||
ErrorOr<void> put_f64(
|
||||
double value,
|
||||
u8 base = 10,
|
||||
bool upper_case = false,
|
||||
|
@ -205,7 +207,7 @@ public:
|
|||
SignMode sign_mode = SignMode::OnlyIfNeeded);
|
||||
#endif
|
||||
|
||||
void put_hexdump(
|
||||
ErrorOr<void> put_hexdump(
|
||||
ReadonlyBytes,
|
||||
size_t width,
|
||||
char fill = ' ');
|
||||
|
@ -233,12 +235,12 @@ private:
|
|||
};
|
||||
|
||||
template<typename T>
|
||||
void __format_value(TypeErasedFormatParams& params, FormatBuilder& builder, FormatParser& parser, const void* value)
|
||||
ErrorOr<void> __format_value(TypeErasedFormatParams& params, FormatBuilder& builder, FormatParser& parser, const void* value)
|
||||
{
|
||||
Formatter<T> formatter;
|
||||
|
||||
formatter.parse(params, parser);
|
||||
formatter.format(builder, *static_cast<const T*>(value));
|
||||
return formatter.format(builder, *static_cast<const T*>(value));
|
||||
}
|
||||
|
||||
template<typename... Parameters>
|
||||
|
@ -298,7 +300,7 @@ struct Formatter<T, typename EnableIf<IsIntegral<T>>::Type> : StandardFormatter
|
|||
{
|
||||
}
|
||||
|
||||
void format(FormatBuilder&, T value);
|
||||
ErrorOr<void> format(FormatBuilder&, T);
|
||||
};
|
||||
|
||||
template<>
|
||||
|
@ -309,7 +311,7 @@ struct Formatter<StringView> : StandardFormatter {
|
|||
{
|
||||
}
|
||||
|
||||
void format(FormatBuilder&, StringView value);
|
||||
ErrorOr<void> format(FormatBuilder&, StringView);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
|
@ -320,12 +322,12 @@ requires(HasFormatter<T>) struct Formatter<Vector<T>> : StandardFormatter {
|
|||
: StandardFormatter(move(formatter))
|
||||
{
|
||||
}
|
||||
void format(FormatBuilder& builder, Vector<T> value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, Vector<T> value)
|
||||
{
|
||||
if (m_mode == Mode::Pointer) {
|
||||
Formatter<FlatPtr> formatter { *this };
|
||||
formatter.format(builder, reinterpret_cast<FlatPtr>(value.data()));
|
||||
return;
|
||||
TRY(formatter.format(builder, reinterpret_cast<FlatPtr>(value.data())));
|
||||
return {};
|
||||
}
|
||||
|
||||
if (m_sign_mode != FormatBuilder::SignMode::Default)
|
||||
|
@ -343,33 +345,34 @@ requires(HasFormatter<T>) struct Formatter<Vector<T>> : StandardFormatter {
|
|||
m_precision = m_precision.value_or(NumericLimits<size_t>::max());
|
||||
|
||||
Formatter<T> content_fmt;
|
||||
builder.put_literal("[ "sv);
|
||||
TRY(builder.put_literal("[ "sv));
|
||||
bool first = true;
|
||||
for (auto& content : value) {
|
||||
if (!first) {
|
||||
builder.put_literal(", "sv);
|
||||
TRY(builder.put_literal(", "sv));
|
||||
content_fmt = Formatter<T> {};
|
||||
}
|
||||
first = false;
|
||||
content_fmt.format(builder, content);
|
||||
TRY(content_fmt.format(builder, content));
|
||||
}
|
||||
builder.put_literal(" ]"sv);
|
||||
TRY(builder.put_literal(" ]"sv));
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Formatter<ReadonlyBytes> : Formatter<StringView> {
|
||||
void format(FormatBuilder& builder, ReadonlyBytes value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, ReadonlyBytes value)
|
||||
{
|
||||
if (m_mode == Mode::Pointer) {
|
||||
Formatter<FlatPtr> formatter { *this };
|
||||
formatter.format(builder, reinterpret_cast<FlatPtr>(value.data()));
|
||||
} else if (m_mode == Mode::Default || m_mode == Mode::HexDump) {
|
||||
m_mode = Mode::HexDump;
|
||||
Formatter<StringView>::format(builder, value);
|
||||
} else {
|
||||
Formatter<StringView>::format(builder, value);
|
||||
return formatter.format(builder, reinterpret_cast<FlatPtr>(value.data()));
|
||||
}
|
||||
if (m_mode == Mode::Default || m_mode == Mode::HexDump) {
|
||||
m_mode = Mode::HexDump;
|
||||
return Formatter<StringView>::format(builder, value);
|
||||
}
|
||||
return Formatter<StringView>::format(builder, value);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -379,14 +382,13 @@ struct Formatter<Bytes> : Formatter<ReadonlyBytes> {
|
|||
|
||||
template<>
|
||||
struct Formatter<const char*> : Formatter<StringView> {
|
||||
void format(FormatBuilder& builder, const char* value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, const char* value)
|
||||
{
|
||||
if (m_mode == Mode::Pointer) {
|
||||
Formatter<FlatPtr> formatter { *this };
|
||||
formatter.format(builder, reinterpret_cast<FlatPtr>(value));
|
||||
} else {
|
||||
Formatter<StringView>::format(builder, value);
|
||||
return formatter.format(builder, reinterpret_cast<FlatPtr>(value));
|
||||
}
|
||||
return Formatter<StringView>::format(builder, value);
|
||||
}
|
||||
};
|
||||
template<>
|
||||
|
@ -397,14 +399,13 @@ struct Formatter<char[Size]> : Formatter<const char*> {
|
|||
};
|
||||
template<size_t Size>
|
||||
struct Formatter<unsigned char[Size]> : Formatter<StringView> {
|
||||
void format(FormatBuilder& builder, const unsigned char* value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, const unsigned char* value)
|
||||
{
|
||||
if (m_mode == Mode::Pointer) {
|
||||
Formatter<FlatPtr> formatter { *this };
|
||||
formatter.format(builder, reinterpret_cast<FlatPtr>(value));
|
||||
} else {
|
||||
Formatter<StringView>::format(builder, { value, Size });
|
||||
return formatter.format(builder, reinterpret_cast<FlatPtr>(value));
|
||||
}
|
||||
Formatter<StringView>::format(builder, { value, Size });
|
||||
}
|
||||
};
|
||||
template<>
|
||||
|
@ -416,33 +417,33 @@ struct Formatter<FlyString> : Formatter<StringView> {
|
|||
|
||||
template<typename T>
|
||||
struct Formatter<T*> : StandardFormatter {
|
||||
void format(FormatBuilder& builder, T* value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, T* value)
|
||||
{
|
||||
if (m_mode == Mode::Default)
|
||||
m_mode = Mode::Pointer;
|
||||
|
||||
Formatter<FlatPtr> formatter { *this };
|
||||
formatter.format(builder, reinterpret_cast<FlatPtr>(value));
|
||||
return formatter.format(builder, reinterpret_cast<FlatPtr>(value));
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Formatter<char> : StandardFormatter {
|
||||
void format(FormatBuilder&, char value);
|
||||
ErrorOr<void> format(FormatBuilder&, char);
|
||||
};
|
||||
template<>
|
||||
struct Formatter<wchar_t> : StandardFormatter {
|
||||
void format(FormatBuilder& builder, wchar_t value);
|
||||
ErrorOr<void> format(FormatBuilder& builder, wchar_t);
|
||||
};
|
||||
template<>
|
||||
struct Formatter<bool> : StandardFormatter {
|
||||
void format(FormatBuilder&, bool value);
|
||||
ErrorOr<void> format(FormatBuilder&, bool);
|
||||
};
|
||||
|
||||
#ifndef KERNEL
|
||||
template<>
|
||||
struct Formatter<float> : StandardFormatter {
|
||||
void format(FormatBuilder&, float value);
|
||||
ErrorOr<void> format(FormatBuilder&, float value);
|
||||
};
|
||||
template<>
|
||||
struct Formatter<double> : StandardFormatter {
|
||||
|
@ -452,7 +453,7 @@ struct Formatter<double> : StandardFormatter {
|
|||
{
|
||||
}
|
||||
|
||||
void format(FormatBuilder&, double value);
|
||||
ErrorOr<void> format(FormatBuilder&, double);
|
||||
};
|
||||
|
||||
template<>
|
||||
|
@ -463,13 +464,13 @@ struct Formatter<long double> : StandardFormatter {
|
|||
{
|
||||
}
|
||||
|
||||
void format(FormatBuilder&, long double value);
|
||||
ErrorOr<void> format(FormatBuilder&, long double value);
|
||||
};
|
||||
#endif
|
||||
|
||||
template<>
|
||||
struct Formatter<std::nullptr_t> : Formatter<FlatPtr> {
|
||||
void format(FormatBuilder& builder, std::nullptr_t)
|
||||
ErrorOr<void> format(FormatBuilder& builder, std::nullptr_t)
|
||||
{
|
||||
if (m_mode == Mode::Default)
|
||||
m_mode = Mode::Pointer;
|
||||
|
@ -478,7 +479,7 @@ struct Formatter<std::nullptr_t> : Formatter<FlatPtr> {
|
|||
}
|
||||
};
|
||||
|
||||
void vformat(StringBuilder&, StringView fmtstr, TypeErasedFormatParams&);
|
||||
ErrorOr<void> vformat(StringBuilder&, StringView fmtstr, TypeErasedFormatParams&);
|
||||
|
||||
#ifndef KERNEL
|
||||
void vout(FILE*, StringView fmtstr, TypeErasedFormatParams&, bool newline = false);
|
||||
|
@ -582,16 +583,16 @@ private:
|
|||
};
|
||||
template<typename T, bool Supported = false>
|
||||
struct __FormatIfSupported : Formatter<StringView> {
|
||||
void format(FormatBuilder& builder, const FormatIfSupported<T>&)
|
||||
ErrorOr<void> format(FormatBuilder& builder, FormatIfSupported<T> const&)
|
||||
{
|
||||
Formatter<StringView>::format(builder, "?");
|
||||
return Formatter<StringView>::format(builder, "?");
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
struct __FormatIfSupported<T, true> : Formatter<T> {
|
||||
void format(FormatBuilder& builder, const FormatIfSupported<T>& value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, FormatIfSupported<T> const& value)
|
||||
{
|
||||
Formatter<T>::format(builder, value.value());
|
||||
return Formatter<T>::format(builder, value.value());
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
|
@ -605,12 +606,22 @@ struct FormatString {
|
|||
template<>
|
||||
struct Formatter<FormatString> : Formatter<StringView> {
|
||||
template<typename... Parameters>
|
||||
void format(FormatBuilder& builder, StringView fmtstr, const Parameters&... parameters)
|
||||
ErrorOr<void> format(FormatBuilder& builder, StringView fmtstr, const Parameters&... parameters)
|
||||
{
|
||||
VariadicFormatParams variadic_format_params { parameters... };
|
||||
vformat(builder, fmtstr, variadic_format_params);
|
||||
return vformat(builder, fmtstr, variadic_format_params);
|
||||
}
|
||||
ErrorOr<void> vformat(FormatBuilder& builder, StringView fmtstr, TypeErasedFormatParams& params);
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Formatter<Error> : Formatter<FormatString> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, Error const& error)
|
||||
{
|
||||
if (error.is_errno())
|
||||
return Formatter<FormatString>::format(builder, "Error(errno={})", error.code());
|
||||
return Formatter<FormatString>::format(builder, "Error({})", error.string_literal());
|
||||
}
|
||||
void vformat(FormatBuilder& builder, StringView fmtstr, TypeErasedFormatParams& params);
|
||||
};
|
||||
|
||||
} // namespace AK
|
||||
|
|
|
@ -133,7 +133,7 @@ struct Traits<IPv4Address> : public GenericTraits<IPv4Address> {
|
|||
|
||||
template<>
|
||||
struct Formatter<IPv4Address> : Formatter<String> {
|
||||
void format(FormatBuilder& builder, IPv4Address value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, IPv4Address value)
|
||||
{
|
||||
return Formatter<String>::format(builder, value.to_string());
|
||||
}
|
||||
|
|
|
@ -245,9 +245,9 @@ private:
|
|||
|
||||
template<>
|
||||
struct Formatter<JsonValue> : Formatter<StringView> {
|
||||
void format(FormatBuilder& builder, const JsonValue& value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, JsonValue const& value)
|
||||
{
|
||||
Formatter<StringView>::format(builder, value.to_string());
|
||||
return Formatter<StringView>::format(builder, value.to_string());
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -81,9 +81,9 @@ private:
|
|||
|
||||
template<>
|
||||
struct Formatter<LexicalPath> : Formatter<StringView> {
|
||||
void format(FormatBuilder& builder, LexicalPath const& value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, LexicalPath const& value)
|
||||
{
|
||||
Formatter<StringView>::format(builder, value.string());
|
||||
return Formatter<StringView>::format(builder, value.string());
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -190,9 +190,9 @@ inline void swap(NonnullOwnPtr<T>& a, NonnullOwnPtr<U>& b)
|
|||
|
||||
template<typename T>
|
||||
struct Formatter<NonnullOwnPtr<T>> : Formatter<const T*> {
|
||||
void format(FormatBuilder& builder, const NonnullOwnPtr<T>& value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, NonnullOwnPtr<T> const& value)
|
||||
{
|
||||
Formatter<const T*>::format(builder, value.ptr());
|
||||
return Formatter<const T*>::format(builder, value.ptr());
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -237,9 +237,9 @@ inline NonnullRefPtr<T> adopt_ref(T& object)
|
|||
|
||||
template<typename T>
|
||||
struct Formatter<NonnullRefPtr<T>> : Formatter<const T*> {
|
||||
void format(FormatBuilder& builder, const NonnullRefPtr<T>& value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, NonnullRefPtr<T> const& value)
|
||||
{
|
||||
Formatter<const T*>::format(builder, value.ptr());
|
||||
return Formatter<const T*>::format(builder, value.ptr());
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -295,9 +295,9 @@ private:
|
|||
|
||||
template<typename T>
|
||||
struct Formatter<RefPtr<T>> : Formatter<const T*> {
|
||||
void format(FormatBuilder& builder, const RefPtr<T>& value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, RefPtr<T> const& value)
|
||||
{
|
||||
Formatter<const T*>::format(builder, value.ptr());
|
||||
return Formatter<const T*>::format(builder, value.ptr());
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -43,7 +43,7 @@ private:
|
|||
|
||||
template<>
|
||||
struct AK::Formatter<AK::SourceLocation> : AK::Formatter<FormatString> {
|
||||
void format(FormatBuilder& builder, AK::SourceLocation location)
|
||||
ErrorOr<void> format(FormatBuilder& builder, AK::SourceLocation location)
|
||||
{
|
||||
return AK::Formatter<FormatString>::format(builder, "[{} @ {}:{}]", location.function_name(), location.filename(), location.line_number());
|
||||
}
|
||||
|
|
|
@ -475,7 +475,7 @@ InputStream& operator>>(InputStream& stream, String& string)
|
|||
String String::vformatted(StringView fmtstr, TypeErasedFormatParams& params)
|
||||
{
|
||||
StringBuilder builder;
|
||||
vformat(builder, fmtstr, params);
|
||||
MUST(vformat(builder, fmtstr, params));
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
|
|
|
@ -49,7 +49,7 @@ public:
|
|||
void appendff(CheckedFormatString<Parameters...>&& fmtstr, Parameters const&... parameters)
|
||||
{
|
||||
VariadicFormatParams variadic_format_params { parameters... };
|
||||
vformat(*this, fmtstr.view(), variadic_format_params);
|
||||
MUST(vformat(*this, fmtstr.view(), variadic_format_params));
|
||||
}
|
||||
|
||||
[[nodiscard]] String build() const;
|
||||
|
|
|
@ -109,9 +109,9 @@ inline size_t allocation_size_for_stringimpl(size_t length)
|
|||
|
||||
template<>
|
||||
struct Formatter<StringImpl> : Formatter<StringView> {
|
||||
void format(FormatBuilder& builder, const StringImpl& value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, StringImpl const& value)
|
||||
{
|
||||
Formatter<StringView>::format(builder, { value.characters(), value.length() });
|
||||
return Formatter<StringView>::format(builder, { value.characters(), value.length() });
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -714,7 +714,7 @@ struct Formatter<UFixedBigInt<T>> : StandardFormatter {
|
|||
{
|
||||
}
|
||||
|
||||
void format(FormatBuilder& builder, UFixedBigInt<T> value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, UFixedBigInt<T> value)
|
||||
{
|
||||
if (m_precision.has_value())
|
||||
VERIFY_NOT_REACHED();
|
||||
|
@ -751,12 +751,13 @@ struct Formatter<UFixedBigInt<T>> : StandardFormatter {
|
|||
ssize_t lower_length = ceil_div(sizeof(T) * 8, (ssize_t)base);
|
||||
Formatter<T> formatter { *this };
|
||||
formatter.m_width = max(width - lower_length, (ssize_t)0);
|
||||
formatter.format(builder, value.high());
|
||||
builder.put_literal("'"sv);
|
||||
TRY(formatter.format(builder, value.high()));
|
||||
TRY(builder.put_literal("'"sv));
|
||||
formatter.m_zero_pad = true;
|
||||
formatter.m_alternative_form = false;
|
||||
formatter.m_width = lower_length;
|
||||
formatter.format(builder, value.low());
|
||||
TRY(formatter.format(builder, value.low()));
|
||||
return {};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
4
AK/URL.h
4
AK/URL.h
|
@ -147,9 +147,9 @@ private:
|
|||
|
||||
template<>
|
||||
struct Formatter<URL> : Formatter<StringView> {
|
||||
void format(FormatBuilder& builder, URL const& value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, URL const& value)
|
||||
{
|
||||
Formatter<StringView>::format(builder, value.serialize());
|
||||
return Formatter<StringView>::format(builder, value.serialize());
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -123,9 +123,9 @@ private:
|
|||
|
||||
template<>
|
||||
struct AK::Formatter<AK::Utf16View> : Formatter<FormatString> {
|
||||
void format(FormatBuilder& builder, AK::Utf16View const& value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, AK::Utf16View const& value)
|
||||
{
|
||||
return builder.builder().append(value);
|
||||
return builder.builder().try_append(value);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -191,9 +191,9 @@ inline WeakPtr<U> Weakable<T>::make_weak_ptr() const
|
|||
|
||||
template<typename T>
|
||||
struct Formatter<WeakPtr<T>> : Formatter<const T*> {
|
||||
void format(FormatBuilder& builder, const WeakPtr<T>& value)
|
||||
ErrorOr<void> format(FormatBuilder& builder, WeakPtr<T> const& value)
|
||||
{
|
||||
Formatter<const T*>::format(builder, value.ptr());
|
||||
return Formatter<const T*>::format(builder, value.ptr());
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue