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:
Andreas Kling 2021-11-16 01:15:21 +01:00
parent 008355c222
commit 216e21a1fa
Notes: sideshowbarker 2024-07-18 01:03:43 +09:00
87 changed files with 450 additions and 448 deletions

View File

@ -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());
}

View File

@ -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;

View File

@ -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);
}
};

View File

@ -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();

View File

@ -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

View File

@ -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());
}

View File

@ -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());
}
};

View File

@ -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());
}
};

View File

@ -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());
}
};

View File

@ -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());
}
};

View File

@ -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());
}
};

View File

@ -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());
}

View File

@ -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();
}

View File

@ -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;

View File

@ -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() });
}
};

View File

@ -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 {};
}
};
}

View File

@ -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());
}
};

View File

@ -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);
}
};

View File

@ -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());
}
};

View File

@ -143,7 +143,7 @@ private:
template<>
struct AK::Formatter<IOAddress> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, IOAddress value)
ErrorOr<void> format(FormatBuilder& builder, IOAddress value)
{
return Formatter<FormatString>::format(builder, "IO {:x}", value.get());
}

View File

@ -298,7 +298,7 @@ class Device;
template<>
struct AK::Formatter<Kernel::PCI::Address> : Formatter<FormatString> {
void format(FormatBuilder& builder, Kernel::PCI::Address value)
ErrorOr<void> format(FormatBuilder& builder, Kernel::PCI::Address value)
{
return Formatter<FormatString>::format(
builder,
@ -308,7 +308,7 @@ struct AK::Formatter<Kernel::PCI::Address> : Formatter<FormatString> {
template<>
struct AK::Formatter<Kernel::PCI::HardwareID> : Formatter<FormatString> {
void format(FormatBuilder& builder, Kernel::PCI::HardwareID value)
ErrorOr<void> format(FormatBuilder& builder, Kernel::PCI::HardwareID value)
{
return Formatter<FormatString>::format(
builder,

View File

@ -53,7 +53,7 @@ private:
template<>
struct AK::Formatter<Kernel::InodeIdentifier> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, Kernel::InodeIdentifier value)
ErrorOr<void> format(FormatBuilder& builder, Kernel::InodeIdentifier value)
{
return AK::Formatter<FormatString>::format(builder, "{}:{}", value.fsid(), value.index());
}

View File

@ -37,7 +37,7 @@ public:
// FIXME: This really not ideal, but vformat expects StringBuilder.
StringBuilder builder;
AK::VariadicFormatParams variadic_format_params { parameters... };
vformat(builder, fmtstr.view(), variadic_format_params);
TRY(vformat(builder, fmtstr.view(), variadic_format_params));
return append_bytes(builder.string_view().bytes());
}

View File

@ -46,28 +46,27 @@ namespace AK {
template<>
struct Formatter<Kernel::KString> : Formatter<StringView> {
void format(FormatBuilder& builder, Kernel::KString const& value)
ErrorOr<void> format(FormatBuilder& builder, Kernel::KString const& value)
{
Formatter<StringView>::format(builder, value.view());
return Formatter<StringView>::format(builder, value.view());
}
};
template<>
struct Formatter<OwnPtr<Kernel::KString>> : Formatter<StringView> {
void format(FormatBuilder& builder, OwnPtr<Kernel::KString> const& value)
ErrorOr<void> format(FormatBuilder& builder, OwnPtr<Kernel::KString> const& value)
{
if (value)
Formatter<StringView>::format(builder, value->view());
else
Formatter<StringView>::format(builder, "[out of memory]"sv);
return Formatter<StringView>::format(builder, value->view());
return Formatter<StringView>::format(builder, "[out of memory]"sv);
}
};
template<>
struct Formatter<NonnullOwnPtr<Kernel::KString>> : Formatter<StringView> {
void format(FormatBuilder& builder, NonnullOwnPtr<Kernel::KString> const& value)
ErrorOr<void> format(FormatBuilder& builder, NonnullOwnPtr<Kernel::KString> const& value)
{
Formatter<StringView>::format(builder, value->view());
return Formatter<StringView>::format(builder, value->view());
}
};

View File

@ -322,9 +322,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, const NonnullRefPtr<T>& value)
{
Formatter<const T*>::format(builder, value.ptr());
return Formatter<const T*>::format(builder, value.ptr());
}
};

View File

@ -445,9 +445,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());
}
};

View File

@ -217,13 +217,13 @@ 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)
{
#ifdef KERNEL
auto ref = value.strong_ref();
Formatter<const T*>::format(builder, ref.ptr());
return Formatter<const T*>::format(builder, ref.ptr());
#else
Formatter<const T*>::format(builder, value.ptr());
return Formatter<const T*>::format(builder, value.ptr());
#endif
}
};

View File

@ -62,7 +62,7 @@ private:
template<>
struct AK::Formatter<Kernel::Memory::VirtualRange> : Formatter<FormatString> {
void format(FormatBuilder& builder, Kernel::Memory::VirtualRange value)
ErrorOr<void> format(FormatBuilder& builder, Kernel::Memory::VirtualRange value)
{
return Formatter<FormatString>::format(builder, "{} - {} (size {:p})", value.base().as_ptr(), value.base().offset(value.size() - 1).as_ptr(), value.size());
}

View File

@ -58,7 +58,7 @@ private:
template<>
struct AK::Formatter<PhysicalAddress> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, PhysicalAddress value)
ErrorOr<void> format(FormatBuilder& builder, PhysicalAddress value)
{
if constexpr (sizeof(PhysicalPtr) == sizeof(u64))
return AK::Formatter<FormatString>::format(builder, "P{:016x}", value.get());

View File

@ -980,7 +980,7 @@ inline static ErrorOr<NonnullOwnPtr<KString>> try_copy_kstring_from_user(const K
template<>
struct AK::Formatter<Kernel::Process> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, const Kernel::Process& value)
ErrorOr<void> format(FormatBuilder& builder, Kernel::Process const& value)
{
return AK::Formatter<FormatString>::format(builder, "{}({})", value.name(), value.pid().value());
}

View File

@ -1279,7 +1279,7 @@ void Thread::track_lock_release(LockRank rank)
}
void AK::Formatter<Kernel::Thread>::format(FormatBuilder& builder, const Kernel::Thread& value)
ErrorOr<void> AK::Formatter<Kernel::Thread>::format(FormatBuilder& builder, Kernel::Thread const& value)
{
return AK::Formatter<FormatString>::format(
builder,

View File

@ -1432,5 +1432,5 @@ inline IterationDecision Thread::for_each_in_state(State state, Callback callbac
template<>
struct AK::Formatter<Kernel::Thread> : AK::Formatter<FormatString> {
void format(FormatBuilder&, const Kernel::Thread&);
ErrorOr<void> format(FormatBuilder&, Kernel::Thread const&);
};

View File

@ -54,7 +54,7 @@ inline VirtualAddress operator-(const VirtualAddress& a, const VirtualAddress& b
template<>
struct AK::Formatter<VirtualAddress> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, const VirtualAddress& value)
ErrorOr<void> format(FormatBuilder& builder, const VirtualAddress& value)
{
return AK::Formatter<FormatString>::format(builder, "V{}", value.as_ptr());
}

View File

@ -201,9 +201,9 @@ struct B {
};
template<>
struct AK::Formatter<B> : Formatter<StringView> {
void format(FormatBuilder& builder, B)
ErrorOr<void> format(FormatBuilder& builder, B)
{
Formatter<StringView>::format(builder, "B");
return Formatter<StringView>::format(builder, "B");
}
};
@ -283,7 +283,7 @@ struct C {
};
template<>
struct AK::Formatter<C> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, C c)
ErrorOr<void> format(FormatBuilder& builder, C c)
{
return AK::Formatter<FormatString>::format(builder, "C(i={})", c.i);
}
@ -300,7 +300,7 @@ TEST_CASE(long_long_regression)
StringBuilder builder;
AK::FormatBuilder fmtbuilder { builder };
fmtbuilder.put_i64(0x0123456789abcdefLL);
MUST(fmtbuilder.put_i64(0x0123456789abcdefLL));
EXPECT_EQ(builder.string_view(), "81985529216486895");
}

View File

@ -128,15 +128,16 @@ union Extractor {
namespace AK {
template<>
struct Formatter<Extractor> : StandardFormatter {
void format(FormatBuilder& builder, const Extractor& value)
ErrorOr<void> format(FormatBuilder& builder, Extractor const& value)
{
builder.put_literal("{");
builder.put_u64(value.sign);
builder.put_literal(", ");
builder.put_u64(value.exponent, 16, true);
builder.put_literal(", ");
builder.put_u64(value.mantissa, 16, true);
builder.put_literal("}");
TRY(builder.put_literal("{"));
TRY(builder.put_u64(value.sign));
TRY(builder.put_literal(", "));
TRY(builder.put_u64(value.exponent, 16, true));
TRY(builder.put_literal(", "));
TRY(builder.put_u64(value.mantissa, 16, true));
TRY(builder.put_literal("}"));
return {};
}
};
}

View File

@ -172,7 +172,7 @@ inline void ValueAndShadowReference<T>::operator=(const ValueWithShadow<T>& othe
template<typename T>
struct AK::Formatter<UserspaceEmulator::ValueWithShadow<T>> : AK::Formatter<T> {
void format(FormatBuilder& builder, UserspaceEmulator::ValueWithShadow<T> value)
ErrorOr<void> format(FormatBuilder& builder, UserspaceEmulator::ValueWithShadow<T> value)
{
return Formatter<T>::format(builder, value.value());
}

View File

@ -63,8 +63,8 @@ public:
template<>
struct AK::Formatter<Hearts::Player> : Formatter<FormatString> {
void format(FormatBuilder& builder, Hearts::Player const& player)
ErrorOr<void> format(FormatBuilder& builder, Hearts::Player const& player)
{
builder.put_string(player.name);
return builder.put_string(player.name);
}
};

View File

@ -82,7 +82,7 @@ private:
template<>
struct AK::Formatter<Cards::Card> : Formatter<FormatString> {
void format(FormatBuilder& builder, const Cards::Card& card)
ErrorOr<void> format(FormatBuilder& builder, Cards::Card const& card)
{
StringView type;
@ -103,6 +103,6 @@ struct AK::Formatter<Cards::Card> : Formatter<FormatString> {
VERIFY_NOT_REACHED();
}
Formatter<FormatString>::format(builder, "{:>2}{}", Cards::Card::labels[card.value()], type);
return Formatter<FormatString>::format(builder, "{:>2}{}", Cards::Card::labels[card.value()], type);
}
};

View File

@ -98,7 +98,7 @@ private:
template<>
struct AK::Formatter<Cards::CardStack> : Formatter<FormatString> {
void format(FormatBuilder& builder, const Cards::CardStack& stack)
ErrorOr<void> format(FormatBuilder& builder, Cards::CardStack const& stack)
{
StringView type;
@ -130,6 +130,6 @@ struct AK::Formatter<Cards::CardStack> : Formatter<FormatString> {
first_card = false;
}
Formatter<FormatString>::format(builder, "{:<10} {:>16}: {}", type, stack.bounding_box(), cards.build());
return Formatter<FormatString>::format(builder, "{:<10} {:>16}: {}", type, stack.bounding_box(), cards.build());
}
};

View File

@ -85,15 +85,15 @@ namespace AK {
template<>
struct Formatter<Core::FileWatcherEvent> : Formatter<FormatString> {
void format(FormatBuilder& builder, const Core::FileWatcherEvent& value)
ErrorOr<void> format(FormatBuilder& builder, Core::FileWatcherEvent const& value)
{
Formatter<FormatString>::format(builder, "FileWatcherEvent(\"{}\", {})", value.event_path, value.type);
return Formatter<FormatString>::format(builder, "FileWatcherEvent(\"{}\", {})", value.event_path, value.type);
}
};
template<>
struct Formatter<Core::FileWatcherEvent::Type> : Formatter<FormatString> {
void format(FormatBuilder& builder, const Core::FileWatcherEvent::Type& value)
ErrorOr<void> format(FormatBuilder& builder, Core::FileWatcherEvent::Type const& value)
{
char const* type;
switch (value) {
@ -116,7 +116,7 @@ struct Formatter<Core::FileWatcherEvent::Type> : Formatter<FormatString> {
VERIFY_NOT_REACHED();
}
builder.put_string(type);
return builder.put_string(type);
}
};

View File

@ -199,7 +199,7 @@ private:
template<>
struct AK::Formatter<Core::Object> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, const Core::Object& value)
ErrorOr<void> format(FormatBuilder& builder, const Core::Object& value)
{
return AK::Formatter<FormatString>::format(builder, "{}({})", value.class_name(), &value);
}

View File

@ -94,7 +94,7 @@ private:
template<>
struct AK::Formatter<Core::SocketAddress> : Formatter<String> {
void format(FormatBuilder& builder, const Core::SocketAddress& value)
ErrorOr<void> format(FormatBuilder& builder, Core::SocketAddress const& value)
{
return Formatter<String>::format(builder, value.to_string());
}

View File

@ -395,7 +395,7 @@ void pretty_print(Decoder& decoder, OutputStream& stream, int indent)
}
void AK::Formatter<Crypto::ASN1::DecodeError>::format(FormatBuilder& fmtbuilder, Crypto::ASN1::DecodeError error)
ErrorOr<void> AK::Formatter<Crypto::ASN1::DecodeError>::format(FormatBuilder& fmtbuilder, Crypto::ASN1::DecodeError error)
{
using Crypto::ASN1::DecodeError;

View File

@ -206,5 +206,5 @@ void pretty_print(Decoder&, OutputStream&, int indent = 0);
template<>
struct AK::Formatter<Crypto::ASN1::DecodeError> : Formatter<StringView> {
void format(FormatBuilder&, Crypto::ASN1::DecodeError);
ErrorOr<void> format(FormatBuilder&, Crypto::ASN1::DecodeError);
};

View File

@ -350,14 +350,14 @@ bool UnsignedBigInteger::operator>=(UnsignedBigInteger const& other) const
}
void AK::Formatter<Crypto::UnsignedBigInteger>::format(FormatBuilder& fmtbuilder, const Crypto::UnsignedBigInteger& value)
ErrorOr<void> AK::Formatter<Crypto::UnsignedBigInteger>::format(FormatBuilder& fmtbuilder, const Crypto::UnsignedBigInteger& value)
{
if (value.is_invalid())
return Formatter<StringView>::format(fmtbuilder, "invalid");
StringBuilder builder;
for (int i = value.length() - 1; i >= 0; --i)
builder.appendff("{}|", value.words()[i]);
TRY(builder.try_appendff("{}|", value.words()[i]));
return Formatter<StringView>::format(fmtbuilder, builder.string_view());
}

View File

@ -125,7 +125,7 @@ struct UnsignedDivisionResult {
template<>
struct AK::Formatter<Crypto::UnsignedBigInteger> : Formatter<StringView> {
void format(FormatBuilder&, const Crypto::UnsignedBigInteger&);
ErrorOr<void> format(FormatBuilder&, Crypto::UnsignedBigInteger const&);
};
inline Crypto::UnsignedBigInteger

View File

@ -127,12 +127,11 @@ struct AK::Formatter<LibDSP::ProcessorRangeParameter> : AK::StandardFormatter {
: StandardFormatter(formatter)
{
}
void format(FormatBuilder& builder, LibDSP::ProcessorRangeParameter value)
ErrorOr<void> format(FormatBuilder& builder, LibDSP::ProcessorRangeParameter value)
{
if (m_mode == Mode::Pointer) {
Formatter<FlatPtr> formatter { *this };
formatter.format(builder, reinterpret_cast<FlatPtr>(&value));
return;
return formatter.format(builder, reinterpret_cast<FlatPtr>(&value));
}
if (m_sign_mode != FormatBuilder::SignMode::Default)
@ -149,6 +148,7 @@ struct AK::Formatter<LibDSP::ProcessorRangeParameter> : AK::StandardFormatter {
m_width = m_width.value_or(0);
m_precision = m_precision.value_or(NumericLimits<size_t>::max());
builder.put_literal(String::formatted("[{} - {}]: {}", value.min_value(), value.max_value(), value.value()));
TRY(builder.put_literal(String::formatted("[{} - {}]: {}", value.min_value(), value.max_value(), value.value())));
return {};
}
};

View File

@ -66,12 +66,11 @@ namespace AK {
template<>
struct Formatter<GUI::ModelIndex> : Formatter<FormatString> {
void format(FormatBuilder& builder, const GUI::ModelIndex& value)
ErrorOr<void> format(FormatBuilder& builder, GUI::ModelIndex const& value)
{
if (value.internal_data())
return Formatter<FormatString>::format(builder, "ModelIndex({},{},{})", value.row(), value.column(), value.internal_data());
else
return Formatter<FormatString>::format(builder, "ModelIndex({},{})", value.row(), value.column());
return Formatter<FormatString>::format(builder, "ModelIndex({},{})", value.row(), value.column());
}
};

View File

@ -73,7 +73,7 @@ namespace AK {
template<>
struct Formatter<GUI::PersistentModelIndex> : Formatter<FormatString> {
void format(FormatBuilder& builder, const GUI::PersistentModelIndex& value)
ErrorOr<void> format(FormatBuilder& builder, GUI::PersistentModelIndex const& value)
{
return Formatter<FormatString>::format(builder, "PersistentModelIndex({},{},{})", value.row(), value.column(), value.internal_data());
}

View File

@ -40,11 +40,11 @@ private:
template<>
struct AK::Formatter<GUI::TextPosition> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, const GUI::TextPosition& value)
ErrorOr<void> format(FormatBuilder& builder, GUI::TextPosition const& value)
{
if (value.is_valid())
Formatter<FormatString>::format(builder, "({},{})", value.line(), value.column());
else
Formatter<FormatString>::format(builder, "GUI::TextPosition(Invalid)");
return Formatter<FormatString>::format(builder, "({},{})", value.line(), value.column());
return Formatter<FormatString>::format(builder, "GUI::TextPosition(Invalid)");
}
};

View File

@ -68,11 +68,10 @@ private:
template<>
struct AK::Formatter<GUI::TextRange> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, const GUI::TextRange& value)
ErrorOr<void> format(FormatBuilder& builder, GUI::TextRange const& value)
{
if (value.is_valid())
return Formatter<FormatString>::format(builder, "{}-{}", value.start(), value.end());
else
return Formatter<FormatString>::format(builder, "GUI::TextRange(Invalid)");
return Formatter<FormatString>::format(builder, "GUI::TextRange(Invalid)");
}
};

View File

@ -70,7 +70,7 @@ private:
template<>
struct AK::Formatter<Gfx::AffineTransform> : Formatter<FormatString> {
void format(FormatBuilder& builder, Gfx::AffineTransform value)
ErrorOr<void> format(FormatBuilder& builder, Gfx::AffineTransform const& value)
{
return Formatter<FormatString>::format(builder, "[{} {} {} {} {} {}]", value.a(), value.b(), value.c(), value.d(), value.e(), value.f());
}

View File

@ -75,9 +75,9 @@ namespace AK {
template<typename T>
struct Formatter<Gfx::Endpoint<T>> : Formatter<StringView> {
void format(FormatBuilder& builder, const Gfx::Endpoint<T>& value)
ErrorOr<void> format(FormatBuilder& builder, Gfx::Endpoint<T> const& value)
{
Formatter<StringView>::format(builder, String::formatted("({}, {}, {})", value.x, value.y, value.z));
return Formatter<StringView>::format(builder, String::formatted("({}, {}, {})", value.x, value.y, value.z));
}
};

View File

@ -352,7 +352,7 @@ bool IPC::decode(IPC::Decoder& decoder, Color& color)
return true;
}
void AK::Formatter<Gfx::Color>::format(FormatBuilder& builder, Gfx::Color const& value)
ErrorOr<void> AK::Formatter<Gfx::Color>::format(FormatBuilder& builder, Gfx::Color const& value)
{
Formatter<StringView>::format(builder, value.to_string());
return Formatter<StringView>::format(builder, value.to_string());
}

View File

@ -478,7 +478,7 @@ namespace AK {
template<>
struct Formatter<Gfx::Color> : public Formatter<StringView> {
void format(FormatBuilder& builder, Gfx::Color const& value);
ErrorOr<void> format(FormatBuilder&, Gfx::Color const&);
};
}

View File

@ -280,9 +280,9 @@ namespace AK {
template<typename T>
struct Formatter<Gfx::Point<T>> : Formatter<StringView> {
void format(FormatBuilder& builder, Gfx::Point<T> const& value)
ErrorOr<void> format(FormatBuilder& builder, Gfx::Point<T> const& value)
{
Formatter<StringView>::format(builder, value.to_string());
return Formatter<StringView>::format(builder, value.to_string());
}
};

View File

@ -726,9 +726,9 @@ namespace AK {
template<typename T>
struct Formatter<Gfx::Rect<T>> : Formatter<StringView> {
void format(FormatBuilder& builder, const Gfx::Rect<T>& value)
ErrorOr<void> format(FormatBuilder& builder, Gfx::Rect<T> const& value)
{
Formatter<StringView>::format(builder, value.to_string());
return Formatter<StringView>::format(builder, value.to_string());
}
};

View File

@ -178,9 +178,9 @@ namespace AK {
template<typename T>
struct Formatter<Gfx::Size<T>> : Formatter<StringView> {
void format(FormatBuilder& builder, Gfx::Size<T> const& value)
ErrorOr<void> format(FormatBuilder& builder, Gfx::Size<T> const& value)
{
Formatter<StringView>::format(builder, value.to_string());
return Formatter<StringView>::format(builder, value.to_string());
}
};

View File

@ -127,9 +127,9 @@ namespace AK {
template<typename T>
struct Formatter<Gfx::Vector2<T>> : Formatter<StringView> {
void format(FormatBuilder& builder, const Gfx::Vector2<T>& value)
ErrorOr<void> format(FormatBuilder& builder, Gfx::Vector2<T> const& value)
{
Formatter<StringView>::format(builder, value.to_string());
return Formatter<StringView>::format(builder, value.to_string());
}
};

View File

@ -144,9 +144,9 @@ namespace AK {
template<typename T>
struct Formatter<Gfx::Vector3<T>> : Formatter<StringView> {
void format(FormatBuilder& builder, const Gfx::Vector3<T>& value)
ErrorOr<void> format(FormatBuilder& builder, Gfx::Vector3<T> const& value)
{
Formatter<StringView>::format(builder, value.to_string());
return Formatter<StringView>::format(builder, value.to_string());
}
};

View File

@ -145,9 +145,9 @@ namespace AK {
template<typename T>
struct Formatter<Gfx::Vector4<T>> : Formatter<StringView> {
void format(FormatBuilder& builder, const Gfx::Vector4<T>& value)
ErrorOr<void> format(FormatBuilder& builder, Gfx::Vector4<T> const& value)
{
Formatter<StringView>::format(builder, value.to_string());
return Formatter<StringView>::format(builder, value.to_string());
}
};

View File

@ -28,7 +28,7 @@ private:
template<>
struct AK::Formatter<JS::Bytecode::Label> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, JS::Bytecode::Label const& value)
ErrorOr<void> format(FormatBuilder& builder, JS::Bytecode::Label const& value)
{
return AK::Formatter<FormatString>::format(builder, "@{}", value.block().name());
}

View File

@ -42,7 +42,7 @@ private:
template<>
struct AK::Formatter<JS::Bytecode::Register> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, JS::Bytecode::Register const& value)
ErrorOr<void> format(FormatBuilder& builder, JS::Bytecode::Register const& value)
{
if (value.index() == JS::Bytecode::Register::accumulator_index)
return AK::Formatter<FormatString>::format(builder, "acc");

View File

@ -75,11 +75,11 @@ private:
template<>
struct AK::Formatter<JS::Cell> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, const JS::Cell* cell)
ErrorOr<void> format(FormatBuilder& builder, JS::Cell const* cell)
{
if (!cell)
Formatter<FormatString>::format(builder, "Cell{nullptr}");
return Formatter<FormatString>::format(builder, "Cell{nullptr}");
else
Formatter<FormatString>::format(builder, "{}({})", cell->class_name(), cell);
return Formatter<FormatString>::format(builder, "{}({})", cell->class_name(), cell);
}
};

View File

@ -75,13 +75,13 @@ namespace AK {
template<>
struct Formatter<JS::PropertyAttributes> : Formatter<StringView> {
void format(FormatBuilder& builder, JS::PropertyAttributes const& property_attributes)
ErrorOr<void> format(FormatBuilder& builder, JS::PropertyAttributes const& property_attributes)
{
Vector<String> parts;
parts.append(String::formatted("[[Writable]]: {}", property_attributes.is_writable()));
parts.append(String::formatted("[[Enumerable]]: {}", property_attributes.is_enumerable()));
parts.append(String::formatted("[[Configurable]]: {}", property_attributes.is_configurable()));
Formatter<StringView>::format(builder, String::formatted("PropertyAttributes {{ {} }}", String::join(", ", parts)));
return Formatter<StringView>::format(builder, String::formatted("PropertyAttributes {{ {} }}", String::join(", ", parts)));
}
};

View File

@ -47,7 +47,7 @@ namespace AK {
template<>
struct Formatter<JS::PropertyDescriptor> : Formatter<StringView> {
void format(FormatBuilder& builder, JS::PropertyDescriptor const& property_descriptor)
ErrorOr<void> format(FormatBuilder& builder, JS::PropertyDescriptor const& property_descriptor)
{
Vector<String> parts;
if (property_descriptor.value.has_value())
@ -62,7 +62,7 @@ struct Formatter<JS::PropertyDescriptor> : Formatter<StringView> {
parts.append(String::formatted("[[Enumerable]]: {}", *property_descriptor.enumerable));
if (property_descriptor.configurable.has_value())
parts.append(String::formatted("[[Configurable]]: {}", *property_descriptor.configurable));
Formatter<StringView>::format(builder, String::formatted("PropertyDescriptor {{ {} }}", String::join(", ", parts)));
return Formatter<StringView>::format(builder, String::formatted("PropertyDescriptor {{ {} }}", String::join(", ", parts)));
}
};

View File

@ -225,14 +225,13 @@ struct Traits<JS::PropertyKey> : public GenericTraits<JS::PropertyKey> {
template<>
struct Formatter<JS::PropertyKey> : Formatter<StringView> {
void format(FormatBuilder& builder, JS::PropertyKey const& property_name)
ErrorOr<void> format(FormatBuilder& builder, JS::PropertyKey const& property_name)
{
if (!property_name.is_valid())
Formatter<StringView>::format(builder, "<invalid PropertyKey>");
else if (property_name.is_number())
Formatter<StringView>::format(builder, String::number(property_name.as_number()));
else
Formatter<StringView>::format(builder, property_name.to_string_or_symbol().to_display_string());
return Formatter<StringView>::format(builder, "<invalid PropertyKey>");
if (property_name.is_number())
return Formatter<StringView>::format(builder, String::number(property_name.as_number()));
return Formatter<StringView>::format(builder, property_name.to_string_or_symbol().to_display_string());
}
};

View File

@ -458,9 +458,9 @@ namespace AK {
template<>
struct Formatter<JS::Value> : Formatter<StringView> {
void format(FormatBuilder& builder, const JS::Value& value)
ErrorOr<void> format(FormatBuilder& builder, JS::Value value)
{
Formatter<StringView>::format(builder, value.is_empty() ? "<empty>" : value.to_string_without_side_effects());
return Formatter<StringView>::format(builder, value.is_empty() ? "<empty>" : value.to_string_without_side_effects());
}
};

View File

@ -166,7 +166,7 @@ namespace AK {
template<>
struct Formatter<PDF::Command> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::Command const& command)
ErrorOr<void> format(FormatBuilder& format_builder, PDF::Command const& command)
{
StringBuilder builder;
builder.appendff("{} ({})",
@ -180,7 +180,7 @@ struct Formatter<PDF::Command> : Formatter<StringView> {
builder.append(" ]");
}
Formatter<StringView>::format(format_builder, builder.to_string());
return Formatter<StringView>::format(format_builder, builder.to_string());
}
};

View File

@ -143,9 +143,9 @@ namespace AK {
template<>
struct Formatter<PDF::Rectangle> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::Rectangle const& rectangle)
ErrorOr<void> format(FormatBuilder& builder, PDF::Rectangle const& rectangle)
{
Formatter<StringView>::format(builder,
return Formatter<StringView>::format(builder,
String::formatted("Rectangle {{ ll=({}, {}), ur=({}, {}) }}",
rectangle.lower_left_x,
rectangle.lower_left_y,
@ -156,7 +156,7 @@ struct Formatter<PDF::Rectangle> : Formatter<StringView> {
template<>
struct Formatter<PDF::Page> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::Page const& page)
ErrorOr<void> format(FormatBuilder& builder, PDF::Page const& page)
{
constexpr auto fmt_string = "Page {{\n resources={}\n contents={}\n media_box={}\n crop_box={}\n user_unit={}\n rotate={}\n}}";
auto str = String::formatted(fmt_string,
@ -166,13 +166,13 @@ struct Formatter<PDF::Page> : Formatter<StringView> {
page.crop_box,
page.user_unit,
page.rotate);
Formatter<StringView>::format(builder, str);
return Formatter<StringView>::format(builder, str);
}
};
template<>
struct Formatter<PDF::Destination> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::Destination const& destination)
ErrorOr<void> format(FormatBuilder& builder, PDF::Destination const& destination)
{
String type_str;
switch (destination.type) {
@ -207,21 +207,21 @@ struct Formatter<PDF::Destination> : Formatter<StringView> {
param_builder.appendff("{} ", param);
auto str = String::formatted("{{ type={} page={} params={} }}", type_str, destination.page, param_builder.to_string());
Formatter<StringView>::format(builder, str);
return Formatter<StringView>::format(builder, str);
}
};
template<>
struct Formatter<PDF::OutlineItem> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::OutlineItem const& item)
ErrorOr<void> format(FormatBuilder& builder, PDF::OutlineItem const& item)
{
Formatter<StringView>::format(builder, item.to_string(0));
return Formatter<StringView>::format(builder, item.to_string(0));
}
};
template<>
struct Formatter<PDF::OutlineDict> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::OutlineDict const& dict)
ErrorOr<void> format(FormatBuilder& builder, PDF::OutlineDict const& dict)
{
StringBuilder child_builder;
child_builder.append('[');
@ -229,7 +229,7 @@ struct Formatter<PDF::OutlineDict> : Formatter<StringView> {
child_builder.appendff("{}\n", child.to_string(2));
child_builder.append(" ]");
Formatter<StringView>::format(builder,
return Formatter<StringView>::format(builder,
String::formatted("OutlineDict {{\n count={}\n children={}\n}}", dict.count, child_builder.to_string()));
}
};

View File

@ -63,17 +63,17 @@ namespace AK {
template<PDF::IsObject T>
struct Formatter<T> : Formatter<StringView> {
void format(FormatBuilder& builder, T const& object)
ErrorOr<void> format(FormatBuilder& builder, T const& object)
{
Formatter<StringView>::format(builder, object.to_string(0));
return Formatter<StringView>::format(builder, object.to_string(0));
}
};
template<PDF::IsObject T>
struct Formatter<NonnullRefPtr<T>> : Formatter<T> {
void format(FormatBuilder& builder, NonnullRefPtr<T> const& object)
ErrorOr<void> format(FormatBuilder& builder, NonnullRefPtr<T> const& object)
{
Formatter<T>::format(builder, *object);
return Formatter<T>::format(builder, *object);
}
};

View File

@ -1187,7 +1187,7 @@ namespace AK {
template<>
struct Formatter<PDF::Parser::LinearizationDictionary> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::Parser::LinearizationDictionary const& dict)
ErrorOr<void> format(FormatBuilder& format_builder, PDF::Parser::LinearizationDictionary const& dict)
{
StringBuilder builder;
builder.append("{\n");
@ -1202,13 +1202,13 @@ struct Formatter<PDF::Parser::LinearizationDictionary> : Formatter<StringView> {
builder.appendff(" offset_of_main_xref_table={}\n", dict.offset_of_main_xref_table);
builder.appendff(" first_page={}\n", dict.first_page);
builder.append('}');
Formatter<StringView>::format(format_builder, builder.to_string());
return Formatter<StringView>::format(format_builder, builder.to_string());
}
};
template<>
struct Formatter<PDF::Parser::PageOffsetHintTable> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::Parser::PageOffsetHintTable const& table)
ErrorOr<void> format(FormatBuilder& format_builder, PDF::Parser::PageOffsetHintTable const& table)
{
StringBuilder builder;
builder.append("{\n");
@ -1226,13 +1226,13 @@ struct Formatter<PDF::Parser::PageOffsetHintTable> : Formatter<StringView> {
builder.appendff(" bits_required_for_fraction_numerator={}\n", table.bits_required_for_fraction_numerator);
builder.appendff(" shared_object_reference_fraction_denominator={}\n", table.shared_object_reference_fraction_denominator);
builder.append('}');
Formatter<StringView>::format(format_builder, builder.to_string());
return Formatter<StringView>::format(format_builder, builder.to_string());
}
};
template<>
struct Formatter<PDF::Parser::PageOffsetHintTableEntry> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::Parser::PageOffsetHintTableEntry const& entry)
ErrorOr<void> format(FormatBuilder& format_builder, PDF::Parser::PageOffsetHintTableEntry const& entry)
{
StringBuilder builder;
builder.append("{\n");
@ -1250,7 +1250,7 @@ struct Formatter<PDF::Parser::PageOffsetHintTableEntry> : Formatter<StringView>
builder.appendff(" page_content_stream_offset_number={}\n", entry.page_content_stream_offset_number);
builder.appendff(" page_content_stream_length_number={}\n", entry.page_content_stream_length_number);
builder.append('}');
Formatter<StringView>::format(format_builder, builder.to_string());
return Formatter<StringView>::format(format_builder, builder.to_string());
}
};

View File

@ -135,43 +135,37 @@ namespace AK {
template<>
struct Formatter<PDF::LineCapStyle> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::LineCapStyle const& style)
ErrorOr<void> format(FormatBuilder& builder, PDF::LineCapStyle const& style)
{
switch (style) {
case PDF::LineCapStyle::ButtCap:
Formatter<StringView>::format(builder, "LineCapStyle::ButtCap");
break;
return Formatter<StringView>::format(builder, "LineCapStyle::ButtCap");
case PDF::LineCapStyle::RoundCap:
Formatter<StringView>::format(builder, "LineCapStyle::RoundCap");
break;
return Formatter<StringView>::format(builder, "LineCapStyle::RoundCap");
case PDF::LineCapStyle::SquareCap:
Formatter<StringView>::format(builder, "LineCapStyle::SquareCap");
break;
return Formatter<StringView>::format(builder, "LineCapStyle::SquareCap");
}
}
};
template<>
struct Formatter<PDF::LineJoinStyle> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::LineJoinStyle const& style)
ErrorOr<void> format(FormatBuilder& builder, PDF::LineJoinStyle const& style)
{
switch (style) {
case PDF::LineJoinStyle::Miter:
Formatter<StringView>::format(builder, "LineJoinStyle::Miter");
break;
return Formatter<StringView>::format(builder, "LineJoinStyle::Miter");
case PDF::LineJoinStyle::Round:
Formatter<StringView>::format(builder, "LineJoinStyle::Round");
break;
return Formatter<StringView>::format(builder, "LineJoinStyle::Round");
case PDF::LineJoinStyle::Bevel:
Formatter<StringView>::format(builder, "LineJoinStyle::Bevel");
break;
return Formatter<StringView>::format(builder, "LineJoinStyle::Bevel");
}
}
};
template<>
struct Formatter<PDF::LineDashPattern> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::LineDashPattern const& pattern)
ErrorOr<void> format(FormatBuilder& format_builder, PDF::LineDashPattern const& pattern)
{
StringBuilder builder;
builder.append("[");
@ -191,40 +185,32 @@ struct Formatter<PDF::LineDashPattern> : Formatter<StringView> {
template<>
struct Formatter<PDF::TextRenderingMode> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::TextRenderingMode const& style)
ErrorOr<void> format(FormatBuilder& builder, PDF::TextRenderingMode const& style)
{
switch (style) {
case PDF::TextRenderingMode::Fill:
Formatter<StringView>::format(builder, "TextRenderingMode::Fill");
break;
return Formatter<StringView>::format(builder, "TextRenderingMode::Fill");
case PDF::TextRenderingMode::Stroke:
Formatter<StringView>::format(builder, "TextRenderingMode::Stroke");
break;
return Formatter<StringView>::format(builder, "TextRenderingMode::Stroke");
case PDF::TextRenderingMode::FillThenStroke:
Formatter<StringView>::format(builder, "TextRenderingMode::FillThenStroke");
break;
return Formatter<StringView>::format(builder, "TextRenderingMode::FillThenStroke");
case PDF::TextRenderingMode::Invisible:
Formatter<StringView>::format(builder, "TextRenderingMode::Invisible");
break;
return Formatter<StringView>::format(builder, "TextRenderingMode::Invisible");
case PDF::TextRenderingMode::FillAndClip:
Formatter<StringView>::format(builder, "TextRenderingMode::FillAndClip");
break;
return Formatter<StringView>::format(builder, "TextRenderingMode::FillAndClip");
case PDF::TextRenderingMode::StrokeAndClip:
Formatter<StringView>::format(builder, "TextRenderingMode::StrokeAndClip");
break;
return Formatter<StringView>::format(builder, "TextRenderingMode::StrokeAndClip");
case PDF::TextRenderingMode::FillStrokeAndClip:
Formatter<StringView>::format(builder, "TextRenderingMode::FillStrokeAndClip");
break;
return Formatter<StringView>::format(builder, "TextRenderingMode::FillStrokeAndClip");
case PDF::TextRenderingMode::Clip:
Formatter<StringView>::format(builder, "TextRenderingMode::Clip");
break;
return Formatter<StringView>::format(builder, "TextRenderingMode::Clip");
}
}
};
template<>
struct Formatter<PDF::TextState> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::TextState const& state)
ErrorOr<void> format(FormatBuilder& format_builder, PDF::TextState const& state)
{
StringBuilder builder;
builder.append("TextState {\n");
@ -239,13 +225,13 @@ struct Formatter<PDF::TextState> : Formatter<StringView> {
builder.appendff(" rise={}\n", state.rise);
builder.appendff(" knockout={}\n", state.knockout);
builder.append(" }");
Formatter<StringView>::format(format_builder, builder.to_string());
return Formatter<StringView>::format(format_builder, builder.to_string());
}
};
template<>
struct Formatter<PDF::GraphicsState> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::GraphicsState const& state)
ErrorOr<void> format(FormatBuilder& format_builder, PDF::GraphicsState const& state)
{
StringBuilder builder;
builder.append("GraphicsState {\n");
@ -259,7 +245,7 @@ struct Formatter<PDF::GraphicsState> : Formatter<StringView> {
builder.appendff(" line_dash_pattern={}\n", state.line_dash_pattern);
builder.appendff(" text_state={}\n", state.text_state);
builder.append("}");
Formatter<StringView>::format(format_builder, builder.to_string());
return Formatter<StringView>::format(format_builder, builder.to_string());
}
};

View File

@ -85,9 +85,9 @@ namespace AK {
template<>
struct Formatter<PDF::Value> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::Value const& value)
ErrorOr<void> format(FormatBuilder& builder, PDF::Value const& value)
{
Formatter<StringView>::format(builder, value.to_string());
return Formatter<StringView>::format(builder, value.to_string());
}
};

View File

@ -101,9 +101,9 @@ namespace AK {
template<>
struct Formatter<PDF::XRefEntry> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::XRefEntry const& entry)
ErrorOr<void> format(FormatBuilder& builder, PDF::XRefEntry const& entry)
{
Formatter<StringView>::format(builder,
return Formatter<StringView>::format(builder,
String::formatted("XRefEntry {{ offset={} generation={} used={} }}",
entry.byte_offset,
entry.generation_number,
@ -113,14 +113,14 @@ struct Formatter<PDF::XRefEntry> : Formatter<StringView> {
template<>
struct Formatter<PDF::XRefTable> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::XRefTable const& table)
ErrorOr<void> format(FormatBuilder& format_builder, PDF::XRefTable const& table)
{
StringBuilder builder;
builder.append("XRefTable {");
for (auto& entry : table.m_entries)
builder.appendff("\n {}", entry);
builder.append("\n}");
Formatter<StringView>::format(format_builder, builder.to_string());
return Formatter<StringView>::format(format_builder, builder.to_string());
}
};

View File

@ -536,7 +536,7 @@ using regex::RegexStringView;
template<>
struct AK::Formatter<regex::RegexStringView> : Formatter<StringView> {
void format(FormatBuilder& builder, regex::RegexStringView value)
ErrorOr<void> format(FormatBuilder& builder, regex::RegexStringView value)
{
auto string = value.to_string();
return Formatter<StringView>::format(builder, string);

View File

@ -286,27 +286,27 @@ private:
template<>
struct AK::Formatter<Wasm::Validator::StackEntry> : public AK::Formatter<StringView> {
void format(FormatBuilder& builder, Wasm::Validator::StackEntry const& value)
ErrorOr<void> format(FormatBuilder& builder, Wasm::Validator::StackEntry const& value)
{
if (value.is_known)
return Formatter<StringView>::format(builder, Wasm::ValueType::kind_name(value.concrete_type.kind()));
Formatter<StringView>::format(builder, "<unknown>"sv);
return Formatter<StringView>::format(builder, "<unknown>"sv);
}
};
template<>
struct AK::Formatter<Wasm::ValueType> : public AK::Formatter<StringView> {
void format(FormatBuilder& builder, Wasm::ValueType const& value)
ErrorOr<void> format(FormatBuilder& builder, Wasm::ValueType const& value)
{
Formatter<StringView>::format(builder, Wasm::ValueType::kind_name(value.kind()));
return Formatter<StringView>::format(builder, Wasm::ValueType::kind_name(value.kind()));
}
};
template<>
struct AK::Formatter<Wasm::ValidationError> : public AK::Formatter<StringView> {
void format(FormatBuilder& builder, Wasm::ValidationError const& error)
ErrorOr<void> format(FormatBuilder& builder, Wasm::ValidationError const& error)
{
Formatter<StringView>::format(builder, error.error_string);
return Formatter<StringView>::format(builder, error.error_string);
}
};

View File

@ -101,25 +101,25 @@ namespace AK {
template<>
struct Formatter<Web::CSS::MediaQuery::MediaFeature> : Formatter<StringView> {
void format(FormatBuilder& builder, Web::CSS::MediaQuery::MediaFeature const& media_feature)
ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaQuery::MediaFeature const& media_feature)
{
Formatter<StringView>::format(builder, media_feature.to_string());
return Formatter<StringView>::format(builder, media_feature.to_string());
}
};
template<>
struct Formatter<Web::CSS::MediaQuery::MediaCondition> : Formatter<StringView> {
void format(FormatBuilder& builder, Web::CSS::MediaQuery::MediaCondition const& media_condition)
ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaQuery::MediaCondition const& media_condition)
{
Formatter<StringView>::format(builder, media_condition.to_string());
return Formatter<StringView>::format(builder, media_condition.to_string());
}
};
template<>
struct Formatter<Web::CSS::MediaQuery> : Formatter<StringView> {
void format(FormatBuilder& builder, Web::CSS::MediaQuery const& media_query)
ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaQuery const& media_query)
{
Formatter<StringView>::format(builder, media_query.to_string());
return Formatter<StringView>::format(builder, media_query.to_string());
}
};

View File

@ -151,9 +151,9 @@ namespace AK {
template<>
struct Formatter<Web::CSS::Selector> : Formatter<StringView> {
void format(FormatBuilder& builder, Web::CSS::Selector const& selector)
ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Selector const& selector)
{
Formatter<StringView>::format(builder, selector.serialize());
return Formatter<StringView>::format(builder, selector.serialize());
}
};

View File

@ -51,9 +51,9 @@ private:
namespace AK {
template<>
struct Formatter<Web::DOM::Position> : Formatter<StringView> {
void format(FormatBuilder& builder, const Web::DOM::Position& value)
ErrorOr<void> format(FormatBuilder& builder, Web::DOM::Position const& value)
{
Formatter<StringView>::format(builder, value.to_string());
return Formatter<StringView>::format(builder, value.to_string());
}
};

View File

@ -28,50 +28,42 @@ bool DNSAnswer::has_expired() const
}
void AK::Formatter<LookupServer::DNSRecordType>::format(AK::FormatBuilder& builder, LookupServer::DNSRecordType value)
ErrorOr<void> AK::Formatter<LookupServer::DNSRecordType>::format(AK::FormatBuilder& builder, LookupServer::DNSRecordType value)
{
switch (value) {
case LookupServer::DNSRecordType::A:
builder.put_string("A");
return;
return builder.put_string("A");
case LookupServer::DNSRecordType::NS:
builder.put_string("NS");
return;
return builder.put_string("NS");
case LookupServer::DNSRecordType::CNAME:
builder.put_string("CNAME");
return;
return builder.put_string("CNAME");
case LookupServer::DNSRecordType::SOA:
builder.put_string("SOA");
return;
return builder.put_string("SOA");
case LookupServer::DNSRecordType::PTR:
builder.put_string("PTR");
return;
return builder.put_string("PTR");
case LookupServer::DNSRecordType::MX:
builder.put_string("MX");
return;
return builder.put_string("MX");
case LookupServer::DNSRecordType::TXT:
builder.put_string("TXT");
return;
return builder.put_string("TXT");
case LookupServer::DNSRecordType::AAAA:
builder.put_string("AAAA");
return;
return builder.put_string("AAAA");
case LookupServer::DNSRecordType::SRV:
builder.put_string("SRV");
return;
return builder.put_string("SRV");
}
builder.put_string("DNS record type ");
builder.put_u64((u16)value);
TRY(builder.put_string("DNS record type "));
TRY(builder.put_u64((u16)value));
return {};
}
void AK::Formatter<LookupServer::DNSRecordClass>::format(AK::FormatBuilder& builder, LookupServer::DNSRecordClass value)
ErrorOr<void> AK::Formatter<LookupServer::DNSRecordClass>::format(AK::FormatBuilder& builder, LookupServer::DNSRecordClass value)
{
switch (value) {
case LookupServer::DNSRecordClass::IN:
builder.put_string("IN");
return;
return builder.put_string("IN");
}
builder.put_string("DNS record class ");
builder.put_u64((u16)value);
TRY(builder.put_string("DNS record class "));
TRY(builder.put_u64((u16)value));
return {};
}

View File

@ -65,7 +65,7 @@ struct AK::Formatter<LookupServer::DNSRecordType> : StandardFormatter {
{
}
void format(AK::FormatBuilder&, LookupServer::DNSRecordType);
ErrorOr<void> format(AK::FormatBuilder&, LookupServer::DNSRecordType);
};
template<>
@ -76,5 +76,5 @@ struct AK::Formatter<LookupServer::DNSRecordClass> : StandardFormatter {
{
}
void format(AK::FormatBuilder&, LookupServer::DNSRecordClass);
ErrorOr<void> format(AK::FormatBuilder&, LookupServer::DNSRecordClass);
};

View File

@ -41,7 +41,7 @@ OutputStream& operator<<(OutputStream& stream, const DNSName&);
template<>
struct AK::Formatter<LookupServer::DNSName> : Formatter<StringView> {
void format(FormatBuilder& builder, const LookupServer::DNSName& value)
ErrorOr<void> format(FormatBuilder& builder, LookupServer::DNSName const& value)
{
return Formatter<StringView>::format(builder, value.as_string());
}

View File

@ -19,7 +19,7 @@
#include <signal.h>
#include <unistd.h>
void AK::Formatter<Shell::AST::Command>::format(FormatBuilder& builder, const Shell::AST::Command& value)
ErrorOr<void> AK::Formatter<Shell::AST::Command>::format(FormatBuilder& builder, Shell::AST::Command const& value)
{
if (m_sign_mode != FormatBuilder::SignMode::Default)
VERIFY_NOT_REACHED();
@ -35,46 +35,46 @@ void AK::Formatter<Shell::AST::Command>::format(FormatBuilder& builder, const Sh
VERIFY_NOT_REACHED();
if (value.argv.is_empty()) {
builder.put_literal("(ShellInternal)");
TRY(builder.put_literal("(ShellInternal)"));
} else {
bool first = true;
for (auto& arg : value.argv) {
if (!first)
builder.put_literal(" ");
TRY(builder.put_literal(" "));
first = false;
builder.put_literal(arg);
TRY(builder.put_literal(arg));
}
}
for (auto& redir : value.redirections) {
builder.put_padding(' ', 1);
TRY(builder.put_padding(' ', 1));
if (redir.is_path_redirection()) {
auto path_redir = (const Shell::AST::PathRedirection*)&redir;
builder.put_i64(path_redir->fd);
TRY(builder.put_i64(path_redir->fd));
switch (path_redir->direction) {
case Shell::AST::PathRedirection::Read:
builder.put_literal("<");
TRY(builder.put_literal("<"));
break;
case Shell::AST::PathRedirection::Write:
builder.put_literal(">");
TRY(builder.put_literal(">"));
break;
case Shell::AST::PathRedirection::WriteAppend:
builder.put_literal(">>");
TRY(builder.put_literal(">>"));
break;
case Shell::AST::PathRedirection::ReadWrite:
builder.put_literal("<>");
TRY(builder.put_literal("<>"));
break;
}
builder.put_literal(path_redir->path);
TRY(builder.put_literal(path_redir->path));
} else if (redir.is_fd_redirection()) {
auto* fdredir = (const Shell::AST::FdRedirection*)&redir;
builder.put_i64(fdredir->new_fd);
builder.put_literal(">");
builder.put_i64(fdredir->old_fd);
TRY(builder.put_i64(fdredir->new_fd));
TRY(builder.put_literal(">"));
TRY(builder.put_i64(fdredir->old_fd));
} else if (redir.is_close_redirection()) {
auto close_redir = (const Shell::AST::CloseRedirection*)&redir;
builder.put_i64(close_redir->fd);
builder.put_literal(">&-");
TRY(builder.put_i64(close_redir->fd));
TRY(builder.put_literal(">&-"));
} else {
VERIFY_NOT_REACHED();
}
@ -84,23 +84,24 @@ void AK::Formatter<Shell::AST::Command>::format(FormatBuilder& builder, const Sh
for (auto& command : value.next_chain) {
switch (command.action) {
case Shell::AST::NodeWithAction::And:
builder.put_literal(" && ");
TRY(builder.put_literal(" && "));
break;
case Shell::AST::NodeWithAction::Or:
builder.put_literal(" || ");
TRY(builder.put_literal(" || "));
break;
case Shell::AST::NodeWithAction::Sequence:
builder.put_literal("; ");
TRY(builder.put_literal("; "));
break;
}
builder.put_literal("(");
builder.put_literal(command.node->class_name());
builder.put_literal("...)");
TRY(builder.put_literal("("));
TRY(builder.put_literal(command.node->class_name()));
TRY(builder.put_literal("...)"));
}
}
if (!value.should_wait)
builder.put_literal("&");
TRY(builder.put_literal("&"));
return {};
}
namespace Shell::AST {

View File

@ -1512,7 +1512,7 @@ struct Formatter<Shell::AST::Command> : StandardFormatter {
{
}
void format(FormatBuilder&, const Shell::AST::Command& value);
ErrorOr<void> format(FormatBuilder&, Shell::AST::Command const& value);
};
}

View File

@ -273,7 +273,7 @@ struct Formatter<BitflagDerivative> : StandardFormatter {
{
}
void format(FormatBuilder& format_builder, BitflagDerivative const& value)
ErrorOr<void> format(FormatBuilder& format_builder, BitflagDerivative const& value)
{
bool had_any_output = false;
int remaining = value.flagset;
@ -283,25 +283,27 @@ struct Formatter<BitflagDerivative> : StandardFormatter {
continue;
remaining &= ~option.value;
if (had_any_output)
format_builder.put_literal(" | ");
format_builder.put_literal(option.name);
TRY(format_builder.put_literal(" | "));
TRY(format_builder.put_literal(option.name));
had_any_output = true;
}
if (remaining != 0) {
// No more BitflagOptions are available. Any remaining flags are unrecognized.
if (had_any_output)
format_builder.put_literal(" | ");
TRY(format_builder.put_literal(" | "));
format_builder.builder().appendff("0x{:x} (?)", static_cast<unsigned>(remaining));
had_any_output = true;
}
if (!had_any_output) {
if constexpr (requires { BitflagDerivative::default_; })
format_builder.put_literal(BitflagDerivative::default_);
TRY(format_builder.put_literal(BitflagDerivative::default_));
else
format_builder.put_literal("0");
TRY(format_builder.put_literal("0"));
}
return {};
}
};
}
@ -319,13 +321,14 @@ struct Formatter<PointerArgument> : StandardFormatter {
{
}
void format(FormatBuilder& format_builder, PointerArgument const& value)
ErrorOr<void> format(FormatBuilder& format_builder, PointerArgument const& value)
{
auto& builder = format_builder.builder();
if (value.value == nullptr)
builder.append("null");
else
builder.appendff("{}", value.value);
return {};
}
};
}
@ -478,25 +481,27 @@ static void format_ioctl(FormattedSyscallBuilder& builder, int fd, unsigned requ
namespace AK {
template<>
struct Formatter<struct timespec> : StandardFormatter {
void format(FormatBuilder& format_builder, struct timespec value)
ErrorOr<void> format(FormatBuilder& format_builder, struct timespec value)
{
auto& builder = format_builder.builder();
builder.appendff("{{tv_sec={}, tv_nsec={}}}", value.tv_sec, value.tv_nsec);
return {};
}
};
template<>
struct Formatter<struct timeval> : StandardFormatter {
void format(FormatBuilder& format_builder, struct timeval value)
ErrorOr<void> format(FormatBuilder& format_builder, struct timeval value)
{
auto& builder = format_builder.builder();
builder.appendff("{{tv_sec={}, tv_usec={}}}", value.tv_sec, value.tv_usec);
return {};
}
};
template<>
struct Formatter<struct stat> : StandardFormatter {
void format(FormatBuilder& format_builder, struct stat value)
ErrorOr<void> format(FormatBuilder& format_builder, struct stat value)
{
auto& builder = format_builder.builder();
builder.appendff(
@ -504,6 +509,7 @@ struct Formatter<struct stat> : StandardFormatter {
"st_size={}, st_blksize={}, st_blocks={}, st_atim={}, st_mtim={}, st_ctim={}}}",
value.st_dev, value.st_ino, value.st_mode, value.st_nlink, value.st_uid, value.st_gid, value.st_rdev,
value.st_size, value.st_blksize, value.st_blocks, value.st_atim, value.st_mtim, value.st_ctim);
return {};
}
};
}
@ -561,7 +567,7 @@ static void format_select(FormattedSyscallBuilder& builder, Syscall::SC_select_p
namespace AK {
template<>
struct Formatter<struct sockaddr> : StandardFormatter {
void format(FormatBuilder& format_builder, struct sockaddr address)
ErrorOr<void> format(FormatBuilder& format_builder, struct sockaddr address)
{
auto& builder = format_builder.builder();
builder.append("{sa_family=");
@ -579,6 +585,7 @@ struct Formatter<struct sockaddr> : StandardFormatter {
address_un->sun_path);
}
builder.append('}');
return {};
}
};
}

View File

@ -28,7 +28,7 @@ static FlatPtr parse_from(ArgIter&);
template<>
struct AK::Formatter<Syscall::Function> : Formatter<StringView> {
void format(FormatBuilder& builder, Syscall::Function function)
ErrorOr<void> format(FormatBuilder& builder, Syscall::Function function)
{
return Formatter<StringView>::format(builder, to_string(function));
}