diff --git a/AK/DistinctNumeric.h b/AK/DistinctNumeric.h index 77bb7058d7f..c6d98b66bc9 100644 --- a/AK/DistinctNumeric.h +++ b/AK/DistinctNumeric.h @@ -271,7 +271,7 @@ private: template struct Formatter> : Formatter { - void format(FormatBuilder& builder, DistinctNumeric value) + ErrorOr format(FormatBuilder& builder, DistinctNumeric value) { return Formatter::format(builder, value.value()); } diff --git a/AK/Error.h b/AK/Error.h index fcc08d3008d..1346cb02d97 100644 --- a/AK/Error.h +++ b/AK/Error.h @@ -6,7 +6,6 @@ #pragma once -#include #include #include #include @@ -131,16 +130,6 @@ private: Optional m_error; }; -template<> -struct Formatter : Formatter { - void format(FormatBuilder& builder, Error const& error) - { - if (error.is_errno()) - return Formatter::format(builder, "Error(errno={})", error.code()); - return Formatter::format(builder, "Error({})", error.string_literal()); - } -}; - } using AK::Error; diff --git a/AK/FixedPoint.h b/AK/FixedPoint.h index 110f15a4de6..f50e514b351 100644 --- a/AK/FixedPoint.h +++ b/AK/FixedPoint.h @@ -322,10 +322,10 @@ struct Formatter> : StandardFormatter { { } - void format(FormatBuilder& builder, FixedPoint value) + ErrorOr format(FormatBuilder& builder, FixedPoint value) { Formatter formatter { *this }; - formatter.format(builder, (double)value); + return formatter.format(builder, (double)value); } }; diff --git a/AK/Format.cpp b/AK/Format.cpp index fb95978873d..f0aa893a644 100644 --- a/AK/Format.cpp +++ b/AK/Format.cpp @@ -75,15 +75,15 @@ static constexpr size_t convert_unsigned_to_string(u64 value, Array& bu return used; } -void vformat_impl(TypeErasedFormatParams& params, FormatBuilder& builder, FormatParser& parser) +ErrorOr 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 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 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 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(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 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 { 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 { 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(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 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(value), base, prefix, upper_case, zero_pad, align, min_width, fill, sign_mode, is_negative); + TRY(put_u64(static_cast(value), base, prefix, upper_case, zero_pad, align, min_width, fill, sign_mode, is_negative)); + return {}; } #ifndef KERNEL -void FormatBuilder::put_f64( +ErrorOr 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::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::max(), fill)); + return {}; } bool is_negative = value < 0.0; if (is_negative) value = -value; - format_builder.put_u64(static_cast(value), base, false, upper_case, false, Align::Right, 0, ' ', sign_mode, is_negative); + TRY(format_builder.put_u64(static_cast(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(value), base, false, upper_case, true, Align::Right, visible_precision); + TRY(format_builder.put_u64(static_cast(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::max(), fill); + TRY(put_string(string_builder.string_view(), align, min_width, NumericLimits::max(), fill)); + return {}; } -void FormatBuilder::put_f80( +ErrorOr 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::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::max(), fill)); + return {}; } bool is_negative = value < 0.0l; if (is_negative) value = -value; - format_builder.put_u64(static_cast(value), base, false, upper_case, false, Align::Right, 0, ' ', sign_mode, is_negative); + TRY(format_builder.put_u64(static_cast(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(value), base, false, upper_case, true, Align::Right, visible_precision); + TRY(format_builder.put_u64(static_cast(value), base, false, upper_case, true, Align::Right, visible_precision)); } } - put_string(string_builder.string_view(), align, min_width, NumericLimits::max(), fill); + TRY(put_string(string_builder.string_view(), align, min_width, NumericLimits::max(), fill)); + return {}; } #endif -void FormatBuilder::put_hexdump(ReadonlyBytes bytes, size_t width, char fill) +ErrorOr 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 { + 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 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::format(FormatBuilder& builder, StringView value) +ErrorOr Formatter::format(FormatBuilder& builder, StringView value) { if (m_sign_mode != FormatBuilder::SignMode::Default) VERIFY_NOT_REACHED(); @@ -603,20 +621,20 @@ void Formatter::format(FormatBuilder& builder, StringView value) m_precision = m_precision.value_or(NumericLimits::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::vformat(FormatBuilder& builder, StringView fmtstr, TypeErasedFormatParams& params) +ErrorOr Formatter::vformat(FormatBuilder& builder, StringView fmtstr, TypeErasedFormatParams& params) { StringBuilder string_builder; - AK::vformat(string_builder, fmtstr, params); - return Formatter::format(builder, string_builder.string_view()); + TRY(AK::vformat(string_builder, fmtstr, params)); + TRY(Formatter::format(builder, string_builder.string_view())); + return {}; } template -void Formatter>::Type>::format(FormatBuilder& builder, T value) +ErrorOr Formatter>::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>::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>::Type>::format(FormatBuilder& m_width = m_width.value_or(0); if constexpr (IsSame, 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::format(FormatBuilder& builder, char value) +ErrorOr Formatter::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::format(FormatBuilder& builder, char value) return formatter.format(builder, { &value, 1 }); } } -void Formatter::format(FormatBuilder& builder, wchar_t value) +ErrorOr Formatter::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 formatter { *this }; @@ -703,7 +720,7 @@ void Formatter::format(FormatBuilder& builder, wchar_t value) return formatter.format(builder, codepoint.to_string()); } } -void Formatter::format(FormatBuilder& builder, bool value) +ErrorOr Formatter::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 formatter { *this }; @@ -716,7 +733,7 @@ void Formatter::format(FormatBuilder& builder, bool value) } } #ifndef KERNEL -void Formatter::format(FormatBuilder& builder, long double value) +ErrorOr Formatter::format(FormatBuilder& builder, long double value) { u8 base; bool upper_case; @@ -736,10 +753,10 @@ void Formatter::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::format(FormatBuilder& builder, double value) +ErrorOr Formatter::format(FormatBuilder& builder, double value) { u8 base; bool upper_case; @@ -759,12 +776,13 @@ void Formatter::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::format(FormatBuilder& builder, float value) + +ErrorOr Formatter::format(FormatBuilder& builder, float value) { Formatter formatter { *this }; - formatter.format(builder, value); + return formatter.format(builder, value); } #endif @@ -772,7 +790,7 @@ void Formatter::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(); diff --git a/AK/Format.h b/AK/Format.h index 37fe5bcc574..e3be0e57050 100644 --- a/AK/Format.h +++ b/AK/Format.h @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include #include @@ -125,7 +127,7 @@ struct TypeErasedParameter { const void* value; Type type; - void (*formatter)(TypeErasedFormatParams&, FormatBuilder&, FormatParser&, const void* value); + ErrorOr (*formatter)(TypeErasedFormatParams&, FormatBuilder&, FormatParser&, void const* value); }; class FormatBuilder { @@ -148,18 +150,18 @@ public: { } - void put_padding(char fill, size_t amount); + ErrorOr put_padding(char fill, size_t amount); - void put_literal(StringView value); + ErrorOr put_literal(StringView value); - void put_string( + ErrorOr put_string( StringView value, Align align = Align::Left, size_t min_width = 0, size_t max_width = NumericLimits::max(), char fill = ' '); - void put_u64( + ErrorOr 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 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 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 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 put_hexdump( ReadonlyBytes, size_t width, char fill = ' '); @@ -233,12 +235,12 @@ private: }; template -void __format_value(TypeErasedFormatParams& params, FormatBuilder& builder, FormatParser& parser, const void* value) +ErrorOr __format_value(TypeErasedFormatParams& params, FormatBuilder& builder, FormatParser& parser, const void* value) { Formatter formatter; formatter.parse(params, parser); - formatter.format(builder, *static_cast(value)); + return formatter.format(builder, *static_cast(value)); } template @@ -298,7 +300,7 @@ struct Formatter>::Type> : StandardFormatter { } - void format(FormatBuilder&, T value); + ErrorOr format(FormatBuilder&, T); }; template<> @@ -309,7 +311,7 @@ struct Formatter : StandardFormatter { { } - void format(FormatBuilder&, StringView value); + ErrorOr format(FormatBuilder&, StringView); }; template @@ -320,12 +322,12 @@ requires(HasFormatter) struct Formatter> : StandardFormatter { : StandardFormatter(move(formatter)) { } - void format(FormatBuilder& builder, Vector value) + ErrorOr format(FormatBuilder& builder, Vector value) { if (m_mode == Mode::Pointer) { Formatter formatter { *this }; - formatter.format(builder, reinterpret_cast(value.data())); - return; + TRY(formatter.format(builder, reinterpret_cast(value.data()))); + return {}; } if (m_sign_mode != FormatBuilder::SignMode::Default) @@ -343,33 +345,34 @@ requires(HasFormatter) struct Formatter> : StandardFormatter { m_precision = m_precision.value_or(NumericLimits::max()); Formatter 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 {}; } 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 : Formatter { - void format(FormatBuilder& builder, ReadonlyBytes value) + ErrorOr format(FormatBuilder& builder, ReadonlyBytes value) { if (m_mode == Mode::Pointer) { Formatter formatter { *this }; - formatter.format(builder, reinterpret_cast(value.data())); - } else if (m_mode == Mode::Default || m_mode == Mode::HexDump) { - m_mode = Mode::HexDump; - Formatter::format(builder, value); - } else { - Formatter::format(builder, value); + return formatter.format(builder, reinterpret_cast(value.data())); } + if (m_mode == Mode::Default || m_mode == Mode::HexDump) { + m_mode = Mode::HexDump; + return Formatter::format(builder, value); + } + return Formatter::format(builder, value); } }; @@ -379,14 +382,13 @@ struct Formatter : Formatter { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, const char* value) + ErrorOr format(FormatBuilder& builder, const char* value) { if (m_mode == Mode::Pointer) { Formatter formatter { *this }; - formatter.format(builder, reinterpret_cast(value)); - } else { - Formatter::format(builder, value); + return formatter.format(builder, reinterpret_cast(value)); } + return Formatter::format(builder, value); } }; template<> @@ -397,14 +399,13 @@ struct Formatter : Formatter { }; template struct Formatter : Formatter { - void format(FormatBuilder& builder, const unsigned char* value) + ErrorOr format(FormatBuilder& builder, const unsigned char* value) { if (m_mode == Mode::Pointer) { Formatter formatter { *this }; - formatter.format(builder, reinterpret_cast(value)); - } else { - Formatter::format(builder, { value, Size }); + return formatter.format(builder, reinterpret_cast(value)); } + Formatter::format(builder, { value, Size }); } }; template<> @@ -416,33 +417,33 @@ struct Formatter : Formatter { template struct Formatter : StandardFormatter { - void format(FormatBuilder& builder, T* value) + ErrorOr format(FormatBuilder& builder, T* value) { if (m_mode == Mode::Default) m_mode = Mode::Pointer; Formatter formatter { *this }; - formatter.format(builder, reinterpret_cast(value)); + return formatter.format(builder, reinterpret_cast(value)); } }; template<> struct Formatter : StandardFormatter { - void format(FormatBuilder&, char value); + ErrorOr format(FormatBuilder&, char); }; template<> struct Formatter : StandardFormatter { - void format(FormatBuilder& builder, wchar_t value); + ErrorOr format(FormatBuilder& builder, wchar_t); }; template<> struct Formatter : StandardFormatter { - void format(FormatBuilder&, bool value); + ErrorOr format(FormatBuilder&, bool); }; #ifndef KERNEL template<> struct Formatter : StandardFormatter { - void format(FormatBuilder&, float value); + ErrorOr format(FormatBuilder&, float value); }; template<> struct Formatter : StandardFormatter { @@ -452,7 +453,7 @@ struct Formatter : StandardFormatter { { } - void format(FormatBuilder&, double value); + ErrorOr format(FormatBuilder&, double); }; template<> @@ -463,13 +464,13 @@ struct Formatter : StandardFormatter { { } - void format(FormatBuilder&, long double value); + ErrorOr format(FormatBuilder&, long double value); }; #endif template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, std::nullptr_t) + ErrorOr format(FormatBuilder& builder, std::nullptr_t) { if (m_mode == Mode::Default) m_mode = Mode::Pointer; @@ -478,7 +479,7 @@ struct Formatter : Formatter { } }; -void vformat(StringBuilder&, StringView fmtstr, TypeErasedFormatParams&); +ErrorOr vformat(StringBuilder&, StringView fmtstr, TypeErasedFormatParams&); #ifndef KERNEL void vout(FILE*, StringView fmtstr, TypeErasedFormatParams&, bool newline = false); @@ -582,16 +583,16 @@ private: }; template struct __FormatIfSupported : Formatter { - void format(FormatBuilder& builder, const FormatIfSupported&) + ErrorOr format(FormatBuilder& builder, FormatIfSupported const&) { - Formatter::format(builder, "?"); + return Formatter::format(builder, "?"); } }; template struct __FormatIfSupported : Formatter { - void format(FormatBuilder& builder, const FormatIfSupported& value) + ErrorOr format(FormatBuilder& builder, FormatIfSupported const& value) { - Formatter::format(builder, value.value()); + return Formatter::format(builder, value.value()); } }; template @@ -605,12 +606,22 @@ struct FormatString { template<> struct Formatter : Formatter { template - void format(FormatBuilder& builder, StringView fmtstr, const Parameters&... parameters) + ErrorOr 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 vformat(FormatBuilder& builder, StringView fmtstr, TypeErasedFormatParams& params); +}; + +template<> +struct Formatter : Formatter { + ErrorOr format(FormatBuilder& builder, Error const& error) + { + if (error.is_errno()) + return Formatter::format(builder, "Error(errno={})", error.code()); + return Formatter::format(builder, "Error({})", error.string_literal()); } - void vformat(FormatBuilder& builder, StringView fmtstr, TypeErasedFormatParams& params); }; } // namespace AK diff --git a/AK/IPv4Address.h b/AK/IPv4Address.h index 3c0f76f6b9e..3df22ea34e4 100644 --- a/AK/IPv4Address.h +++ b/AK/IPv4Address.h @@ -133,7 +133,7 @@ struct Traits : public GenericTraits { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, IPv4Address value) + ErrorOr format(FormatBuilder& builder, IPv4Address value) { return Formatter::format(builder, value.to_string()); } diff --git a/AK/JsonValue.h b/AK/JsonValue.h index d4627fb28db..629ba77689d 100644 --- a/AK/JsonValue.h +++ b/AK/JsonValue.h @@ -245,9 +245,9 @@ private: template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, const JsonValue& value) + ErrorOr format(FormatBuilder& builder, JsonValue const& value) { - Formatter::format(builder, value.to_string()); + return Formatter::format(builder, value.to_string()); } }; diff --git a/AK/LexicalPath.h b/AK/LexicalPath.h index 98e11a97c03..d758e9ad199 100644 --- a/AK/LexicalPath.h +++ b/AK/LexicalPath.h @@ -81,9 +81,9 @@ private: template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, LexicalPath const& value) + ErrorOr format(FormatBuilder& builder, LexicalPath const& value) { - Formatter::format(builder, value.string()); + return Formatter::format(builder, value.string()); } }; diff --git a/AK/NonnullOwnPtr.h b/AK/NonnullOwnPtr.h index 96df3a26f88..1645a0009db 100644 --- a/AK/NonnullOwnPtr.h +++ b/AK/NonnullOwnPtr.h @@ -190,9 +190,9 @@ inline void swap(NonnullOwnPtr& a, NonnullOwnPtr& b) template struct Formatter> : Formatter { - void format(FormatBuilder& builder, const NonnullOwnPtr& value) + ErrorOr format(FormatBuilder& builder, NonnullOwnPtr const& value) { - Formatter::format(builder, value.ptr()); + return Formatter::format(builder, value.ptr()); } }; diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h index e97e5ce5e0f..7f9a0d00c2b 100644 --- a/AK/NonnullRefPtr.h +++ b/AK/NonnullRefPtr.h @@ -237,9 +237,9 @@ inline NonnullRefPtr adopt_ref(T& object) template struct Formatter> : Formatter { - void format(FormatBuilder& builder, const NonnullRefPtr& value) + ErrorOr format(FormatBuilder& builder, NonnullRefPtr const& value) { - Formatter::format(builder, value.ptr()); + return Formatter::format(builder, value.ptr()); } }; diff --git a/AK/RefPtr.h b/AK/RefPtr.h index db350b847f9..41fe032d2b4 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -295,9 +295,9 @@ private: template struct Formatter> : Formatter { - void format(FormatBuilder& builder, const RefPtr& value) + ErrorOr format(FormatBuilder& builder, RefPtr const& value) { - Formatter::format(builder, value.ptr()); + return Formatter::format(builder, value.ptr()); } }; diff --git a/AK/SourceLocation.h b/AK/SourceLocation.h index cc8260441bb..1fabd378693 100644 --- a/AK/SourceLocation.h +++ b/AK/SourceLocation.h @@ -43,7 +43,7 @@ private: template<> struct AK::Formatter : AK::Formatter { - void format(FormatBuilder& builder, AK::SourceLocation location) + ErrorOr format(FormatBuilder& builder, AK::SourceLocation location) { return AK::Formatter::format(builder, "[{} @ {}:{}]", location.function_name(), location.filename(), location.line_number()); } diff --git a/AK/String.cpp b/AK/String.cpp index a3bb45c5b41..6dec4c14387 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -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(); } diff --git a/AK/StringBuilder.h b/AK/StringBuilder.h index d154c8be3e8..04fc816db4b 100644 --- a/AK/StringBuilder.h +++ b/AK/StringBuilder.h @@ -49,7 +49,7 @@ public: void appendff(CheckedFormatString&& 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; diff --git a/AK/StringImpl.h b/AK/StringImpl.h index 079e11825d3..7468924794f 100644 --- a/AK/StringImpl.h +++ b/AK/StringImpl.h @@ -109,9 +109,9 @@ inline size_t allocation_size_for_stringimpl(size_t length) template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, const StringImpl& value) + ErrorOr format(FormatBuilder& builder, StringImpl const& value) { - Formatter::format(builder, { value.characters(), value.length() }); + return Formatter::format(builder, { value.characters(), value.length() }); } }; diff --git a/AK/UFixedBigInt.h b/AK/UFixedBigInt.h index f5302a4b0b7..762a4eb32df 100644 --- a/AK/UFixedBigInt.h +++ b/AK/UFixedBigInt.h @@ -714,7 +714,7 @@ struct Formatter> : StandardFormatter { { } - void format(FormatBuilder& builder, UFixedBigInt value) + ErrorOr format(FormatBuilder& builder, UFixedBigInt value) { if (m_precision.has_value()) VERIFY_NOT_REACHED(); @@ -751,12 +751,13 @@ struct Formatter> : StandardFormatter { ssize_t lower_length = ceil_div(sizeof(T) * 8, (ssize_t)base); Formatter 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 {}; } }; } diff --git a/AK/URL.h b/AK/URL.h index 031be5b0402..647998df64c 100644 --- a/AK/URL.h +++ b/AK/URL.h @@ -147,9 +147,9 @@ private: template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, URL const& value) + ErrorOr format(FormatBuilder& builder, URL const& value) { - Formatter::format(builder, value.serialize()); + return Formatter::format(builder, value.serialize()); } }; diff --git a/AK/Utf16View.h b/AK/Utf16View.h index 5b6fc24e062..2f293825f9b 100644 --- a/AK/Utf16View.h +++ b/AK/Utf16View.h @@ -123,9 +123,9 @@ private: template<> struct AK::Formatter : Formatter { - void format(FormatBuilder& builder, AK::Utf16View const& value) + ErrorOr format(FormatBuilder& builder, AK::Utf16View const& value) { - return builder.builder().append(value); + return builder.builder().try_append(value); } }; diff --git a/AK/WeakPtr.h b/AK/WeakPtr.h index bb3e6bcbb28..69c36d24280 100644 --- a/AK/WeakPtr.h +++ b/AK/WeakPtr.h @@ -191,9 +191,9 @@ inline WeakPtr Weakable::make_weak_ptr() const template struct Formatter> : Formatter { - void format(FormatBuilder& builder, const WeakPtr& value) + ErrorOr format(FormatBuilder& builder, WeakPtr const& value) { - Formatter::format(builder, value.ptr()); + return Formatter::format(builder, value.ptr()); } }; diff --git a/Kernel/Arch/x86/IO.h b/Kernel/Arch/x86/IO.h index b42bdce0f79..d53264c22ee 100644 --- a/Kernel/Arch/x86/IO.h +++ b/Kernel/Arch/x86/IO.h @@ -143,7 +143,7 @@ private: template<> struct AK::Formatter : AK::Formatter { - void format(FormatBuilder& builder, IOAddress value) + ErrorOr format(FormatBuilder& builder, IOAddress value) { return Formatter::format(builder, "IO {:x}", value.get()); } diff --git a/Kernel/Bus/PCI/Definitions.h b/Kernel/Bus/PCI/Definitions.h index 1118987f4ab..f5630642572 100644 --- a/Kernel/Bus/PCI/Definitions.h +++ b/Kernel/Bus/PCI/Definitions.h @@ -298,7 +298,7 @@ class Device; template<> struct AK::Formatter : Formatter { - void format(FormatBuilder& builder, Kernel::PCI::Address value) + ErrorOr format(FormatBuilder& builder, Kernel::PCI::Address value) { return Formatter::format( builder, @@ -308,7 +308,7 @@ struct AK::Formatter : Formatter { template<> struct AK::Formatter : Formatter { - void format(FormatBuilder& builder, Kernel::PCI::HardwareID value) + ErrorOr format(FormatBuilder& builder, Kernel::PCI::HardwareID value) { return Formatter::format( builder, diff --git a/Kernel/FileSystem/InodeIdentifier.h b/Kernel/FileSystem/InodeIdentifier.h index 8cee21a3fbf..bd07c7d1cdf 100644 --- a/Kernel/FileSystem/InodeIdentifier.h +++ b/Kernel/FileSystem/InodeIdentifier.h @@ -53,7 +53,7 @@ private: template<> struct AK::Formatter : AK::Formatter { - void format(FormatBuilder& builder, Kernel::InodeIdentifier value) + ErrorOr format(FormatBuilder& builder, Kernel::InodeIdentifier value) { return AK::Formatter::format(builder, "{}:{}", value.fsid(), value.index()); } diff --git a/Kernel/KBufferBuilder.h b/Kernel/KBufferBuilder.h index e6cded298e1..335096a1d47 100644 --- a/Kernel/KBufferBuilder.h +++ b/Kernel/KBufferBuilder.h @@ -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()); } diff --git a/Kernel/KString.h b/Kernel/KString.h index d92f400df26..e5ec4d0f79f 100644 --- a/Kernel/KString.h +++ b/Kernel/KString.h @@ -46,28 +46,27 @@ namespace AK { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, Kernel::KString const& value) + ErrorOr format(FormatBuilder& builder, Kernel::KString const& value) { - Formatter::format(builder, value.view()); + return Formatter::format(builder, value.view()); } }; template<> struct Formatter> : Formatter { - void format(FormatBuilder& builder, OwnPtr const& value) + ErrorOr format(FormatBuilder& builder, OwnPtr const& value) { if (value) - Formatter::format(builder, value->view()); - else - Formatter::format(builder, "[out of memory]"sv); + return Formatter::format(builder, value->view()); + return Formatter::format(builder, "[out of memory]"sv); } }; template<> struct Formatter> : Formatter { - void format(FormatBuilder& builder, NonnullOwnPtr const& value) + ErrorOr format(FormatBuilder& builder, NonnullOwnPtr const& value) { - Formatter::format(builder, value->view()); + return Formatter::format(builder, value->view()); } }; diff --git a/Kernel/Library/ThreadSafeNonnullRefPtr.h b/Kernel/Library/ThreadSafeNonnullRefPtr.h index e12bb05a7fa..0f0a0b0eea9 100644 --- a/Kernel/Library/ThreadSafeNonnullRefPtr.h +++ b/Kernel/Library/ThreadSafeNonnullRefPtr.h @@ -322,9 +322,9 @@ inline NonnullRefPtr adopt_ref(T& object) template struct Formatter> : Formatter { - void format(FormatBuilder& builder, const NonnullRefPtr& value) + ErrorOr format(FormatBuilder& builder, const NonnullRefPtr& value) { - Formatter::format(builder, value.ptr()); + return Formatter::format(builder, value.ptr()); } }; diff --git a/Kernel/Library/ThreadSafeRefPtr.h b/Kernel/Library/ThreadSafeRefPtr.h index 0bc3f0eeaef..31f48247805 100644 --- a/Kernel/Library/ThreadSafeRefPtr.h +++ b/Kernel/Library/ThreadSafeRefPtr.h @@ -445,9 +445,9 @@ private: template struct Formatter> : Formatter { - void format(FormatBuilder& builder, const RefPtr& value) + ErrorOr format(FormatBuilder& builder, RefPtr const& value) { - Formatter::format(builder, value.ptr()); + return Formatter::format(builder, value.ptr()); } }; diff --git a/Kernel/Library/ThreadSafeWeakPtr.h b/Kernel/Library/ThreadSafeWeakPtr.h index b89dc480f29..c5675c57a08 100644 --- a/Kernel/Library/ThreadSafeWeakPtr.h +++ b/Kernel/Library/ThreadSafeWeakPtr.h @@ -217,13 +217,13 @@ inline WeakPtr Weakable::make_weak_ptr() const template struct Formatter> : Formatter { - void format(FormatBuilder& builder, const WeakPtr& value) + ErrorOr format(FormatBuilder& builder, WeakPtr const& value) { #ifdef KERNEL auto ref = value.strong_ref(); - Formatter::format(builder, ref.ptr()); + return Formatter::format(builder, ref.ptr()); #else - Formatter::format(builder, value.ptr()); + return Formatter::format(builder, value.ptr()); #endif } }; diff --git a/Kernel/Memory/VirtualRange.h b/Kernel/Memory/VirtualRange.h index 93c8f80df3f..fb959f3e52d 100644 --- a/Kernel/Memory/VirtualRange.h +++ b/Kernel/Memory/VirtualRange.h @@ -62,7 +62,7 @@ private: template<> struct AK::Formatter : Formatter { - void format(FormatBuilder& builder, Kernel::Memory::VirtualRange value) + ErrorOr format(FormatBuilder& builder, Kernel::Memory::VirtualRange value) { return Formatter::format(builder, "{} - {} (size {:p})", value.base().as_ptr(), value.base().offset(value.size() - 1).as_ptr(), value.size()); } diff --git a/Kernel/PhysicalAddress.h b/Kernel/PhysicalAddress.h index 195e4c2cc0d..e1f66d6a6aa 100644 --- a/Kernel/PhysicalAddress.h +++ b/Kernel/PhysicalAddress.h @@ -58,7 +58,7 @@ private: template<> struct AK::Formatter : AK::Formatter { - void format(FormatBuilder& builder, PhysicalAddress value) + ErrorOr format(FormatBuilder& builder, PhysicalAddress value) { if constexpr (sizeof(PhysicalPtr) == sizeof(u64)) return AK::Formatter::format(builder, "P{:016x}", value.get()); diff --git a/Kernel/Process.h b/Kernel/Process.h index e37475a3999..3fe8fe2f55a 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -980,7 +980,7 @@ inline static ErrorOr> try_copy_kstring_from_user(const K template<> struct AK::Formatter : AK::Formatter { - void format(FormatBuilder& builder, const Kernel::Process& value) + ErrorOr format(FormatBuilder& builder, Kernel::Process const& value) { return AK::Formatter::format(builder, "{}({})", value.name(), value.pid().value()); } diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp index d24f98b6e6a..45cd0fb34cb 100644 --- a/Kernel/Thread.cpp +++ b/Kernel/Thread.cpp @@ -1279,7 +1279,7 @@ void Thread::track_lock_release(LockRank rank) } -void AK::Formatter::format(FormatBuilder& builder, const Kernel::Thread& value) +ErrorOr AK::Formatter::format(FormatBuilder& builder, Kernel::Thread const& value) { return AK::Formatter::format( builder, diff --git a/Kernel/Thread.h b/Kernel/Thread.h index d2b858134d1..5378b2b9bba 100644 --- a/Kernel/Thread.h +++ b/Kernel/Thread.h @@ -1432,5 +1432,5 @@ inline IterationDecision Thread::for_each_in_state(State state, Callback callbac template<> struct AK::Formatter : AK::Formatter { - void format(FormatBuilder&, const Kernel::Thread&); + ErrorOr format(FormatBuilder&, Kernel::Thread const&); }; diff --git a/Kernel/VirtualAddress.h b/Kernel/VirtualAddress.h index 6f2120389fa..28febf60976 100644 --- a/Kernel/VirtualAddress.h +++ b/Kernel/VirtualAddress.h @@ -54,7 +54,7 @@ inline VirtualAddress operator-(const VirtualAddress& a, const VirtualAddress& b template<> struct AK::Formatter : AK::Formatter { - void format(FormatBuilder& builder, const VirtualAddress& value) + ErrorOr format(FormatBuilder& builder, const VirtualAddress& value) { return AK::Formatter::format(builder, "V{}", value.as_ptr()); } diff --git a/Tests/AK/TestFormat.cpp b/Tests/AK/TestFormat.cpp index dcae6d3c07f..89324d37aaf 100644 --- a/Tests/AK/TestFormat.cpp +++ b/Tests/AK/TestFormat.cpp @@ -201,9 +201,9 @@ struct B { }; template<> struct AK::Formatter : Formatter { - void format(FormatBuilder& builder, B) + ErrorOr format(FormatBuilder& builder, B) { - Formatter::format(builder, "B"); + return Formatter::format(builder, "B"); } }; @@ -283,7 +283,7 @@ struct C { }; template<> struct AK::Formatter : AK::Formatter { - void format(FormatBuilder& builder, C c) + ErrorOr format(FormatBuilder& builder, C c) { return AK::Formatter::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"); } diff --git a/Tests/LibM/test-math.cpp b/Tests/LibM/test-math.cpp index 167dedf65c6..35bbab3bbe2 100644 --- a/Tests/LibM/test-math.cpp +++ b/Tests/LibM/test-math.cpp @@ -128,15 +128,16 @@ union Extractor { namespace AK { template<> struct Formatter : StandardFormatter { - void format(FormatBuilder& builder, const Extractor& value) + ErrorOr 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 {}; } }; } diff --git a/Userland/DevTools/UserspaceEmulator/ValueWithShadow.h b/Userland/DevTools/UserspaceEmulator/ValueWithShadow.h index 50e2fb86fbf..db23c2597b0 100644 --- a/Userland/DevTools/UserspaceEmulator/ValueWithShadow.h +++ b/Userland/DevTools/UserspaceEmulator/ValueWithShadow.h @@ -172,7 +172,7 @@ inline void ValueAndShadowReference::operator=(const ValueWithShadow& othe template struct AK::Formatter> : AK::Formatter { - void format(FormatBuilder& builder, UserspaceEmulator::ValueWithShadow value) + ErrorOr format(FormatBuilder& builder, UserspaceEmulator::ValueWithShadow value) { return Formatter::format(builder, value.value()); } diff --git a/Userland/Games/Hearts/Player.h b/Userland/Games/Hearts/Player.h index 328c5016bc8..2fdc7945fda 100644 --- a/Userland/Games/Hearts/Player.h +++ b/Userland/Games/Hearts/Player.h @@ -63,8 +63,8 @@ public: template<> struct AK::Formatter : Formatter { - void format(FormatBuilder& builder, Hearts::Player const& player) + ErrorOr format(FormatBuilder& builder, Hearts::Player const& player) { - builder.put_string(player.name); + return builder.put_string(player.name); } }; diff --git a/Userland/Libraries/LibCards/Card.h b/Userland/Libraries/LibCards/Card.h index fbe0353d9e7..d56b5c71bad 100644 --- a/Userland/Libraries/LibCards/Card.h +++ b/Userland/Libraries/LibCards/Card.h @@ -82,7 +82,7 @@ private: template<> struct AK::Formatter : Formatter { - void format(FormatBuilder& builder, const Cards::Card& card) + ErrorOr format(FormatBuilder& builder, Cards::Card const& card) { StringView type; @@ -103,6 +103,6 @@ struct AK::Formatter : Formatter { VERIFY_NOT_REACHED(); } - Formatter::format(builder, "{:>2}{}", Cards::Card::labels[card.value()], type); + return Formatter::format(builder, "{:>2}{}", Cards::Card::labels[card.value()], type); } }; diff --git a/Userland/Libraries/LibCards/CardStack.h b/Userland/Libraries/LibCards/CardStack.h index e368d15da75..f79ce183367 100644 --- a/Userland/Libraries/LibCards/CardStack.h +++ b/Userland/Libraries/LibCards/CardStack.h @@ -98,7 +98,7 @@ private: template<> struct AK::Formatter : Formatter { - void format(FormatBuilder& builder, const Cards::CardStack& stack) + ErrorOr format(FormatBuilder& builder, Cards::CardStack const& stack) { StringView type; @@ -130,6 +130,6 @@ struct AK::Formatter : Formatter { first_card = false; } - Formatter::format(builder, "{:<10} {:>16}: {}", type, stack.bounding_box(), cards.build()); + return Formatter::format(builder, "{:<10} {:>16}: {}", type, stack.bounding_box(), cards.build()); } }; diff --git a/Userland/Libraries/LibCore/FileWatcher.h b/Userland/Libraries/LibCore/FileWatcher.h index 4d94f7f5243..56c21f3978a 100644 --- a/Userland/Libraries/LibCore/FileWatcher.h +++ b/Userland/Libraries/LibCore/FileWatcher.h @@ -85,15 +85,15 @@ namespace AK { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, const Core::FileWatcherEvent& value) + ErrorOr format(FormatBuilder& builder, Core::FileWatcherEvent const& value) { - Formatter::format(builder, "FileWatcherEvent(\"{}\", {})", value.event_path, value.type); + return Formatter::format(builder, "FileWatcherEvent(\"{}\", {})", value.event_path, value.type); } }; template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, const Core::FileWatcherEvent::Type& value) + ErrorOr format(FormatBuilder& builder, Core::FileWatcherEvent::Type const& value) { char const* type; switch (value) { @@ -116,7 +116,7 @@ struct Formatter : Formatter { VERIFY_NOT_REACHED(); } - builder.put_string(type); + return builder.put_string(type); } }; diff --git a/Userland/Libraries/LibCore/Object.h b/Userland/Libraries/LibCore/Object.h index c5f83ecd9eb..51c35c8e77d 100644 --- a/Userland/Libraries/LibCore/Object.h +++ b/Userland/Libraries/LibCore/Object.h @@ -199,7 +199,7 @@ private: template<> struct AK::Formatter : AK::Formatter { - void format(FormatBuilder& builder, const Core::Object& value) + ErrorOr format(FormatBuilder& builder, const Core::Object& value) { return AK::Formatter::format(builder, "{}({})", value.class_name(), &value); } diff --git a/Userland/Libraries/LibCore/SocketAddress.h b/Userland/Libraries/LibCore/SocketAddress.h index ad6a1f5401d..417d329badf 100644 --- a/Userland/Libraries/LibCore/SocketAddress.h +++ b/Userland/Libraries/LibCore/SocketAddress.h @@ -94,7 +94,7 @@ private: template<> struct AK::Formatter : Formatter { - void format(FormatBuilder& builder, const Core::SocketAddress& value) + ErrorOr format(FormatBuilder& builder, Core::SocketAddress const& value) { return Formatter::format(builder, value.to_string()); } diff --git a/Userland/Libraries/LibCrypto/ASN1/DER.cpp b/Userland/Libraries/LibCrypto/ASN1/DER.cpp index 4952012d54a..76da1a3e9aa 100644 --- a/Userland/Libraries/LibCrypto/ASN1/DER.cpp +++ b/Userland/Libraries/LibCrypto/ASN1/DER.cpp @@ -395,7 +395,7 @@ void pretty_print(Decoder& decoder, OutputStream& stream, int indent) } -void AK::Formatter::format(FormatBuilder& fmtbuilder, Crypto::ASN1::DecodeError error) +ErrorOr AK::Formatter::format(FormatBuilder& fmtbuilder, Crypto::ASN1::DecodeError error) { using Crypto::ASN1::DecodeError; diff --git a/Userland/Libraries/LibCrypto/ASN1/DER.h b/Userland/Libraries/LibCrypto/ASN1/DER.h index 2b57cd9c197..023b3e22586 100644 --- a/Userland/Libraries/LibCrypto/ASN1/DER.h +++ b/Userland/Libraries/LibCrypto/ASN1/DER.h @@ -206,5 +206,5 @@ void pretty_print(Decoder&, OutputStream&, int indent = 0); template<> struct AK::Formatter : Formatter { - void format(FormatBuilder&, Crypto::ASN1::DecodeError); + ErrorOr format(FormatBuilder&, Crypto::ASN1::DecodeError); }; diff --git a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp index a2c8d77ef7a..48ca0a6050b 100644 --- a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp +++ b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp @@ -350,14 +350,14 @@ bool UnsignedBigInteger::operator>=(UnsignedBigInteger const& other) const } -void AK::Formatter::format(FormatBuilder& fmtbuilder, const Crypto::UnsignedBigInteger& value) +ErrorOr AK::Formatter::format(FormatBuilder& fmtbuilder, const Crypto::UnsignedBigInteger& value) { if (value.is_invalid()) return Formatter::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::format(fmtbuilder, builder.string_view()); } diff --git a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h index 76c916d0526..5f031b67814 100644 --- a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h +++ b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h @@ -125,7 +125,7 @@ struct UnsignedDivisionResult { template<> struct AK::Formatter : Formatter { - void format(FormatBuilder&, const Crypto::UnsignedBigInteger&); + ErrorOr format(FormatBuilder&, Crypto::UnsignedBigInteger const&); }; inline Crypto::UnsignedBigInteger diff --git a/Userland/Libraries/LibDSP/ProcessorParameter.h b/Userland/Libraries/LibDSP/ProcessorParameter.h index b31ef804cb5..6945a4dd6fa 100644 --- a/Userland/Libraries/LibDSP/ProcessorParameter.h +++ b/Userland/Libraries/LibDSP/ProcessorParameter.h @@ -127,12 +127,11 @@ struct AK::Formatter : AK::StandardFormatter { : StandardFormatter(formatter) { } - void format(FormatBuilder& builder, LibDSP::ProcessorRangeParameter value) + ErrorOr format(FormatBuilder& builder, LibDSP::ProcessorRangeParameter value) { if (m_mode == Mode::Pointer) { Formatter formatter { *this }; - formatter.format(builder, reinterpret_cast(&value)); - return; + return formatter.format(builder, reinterpret_cast(&value)); } if (m_sign_mode != FormatBuilder::SignMode::Default) @@ -149,6 +148,7 @@ struct AK::Formatter : AK::StandardFormatter { m_width = m_width.value_or(0); m_precision = m_precision.value_or(NumericLimits::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 {}; } }; diff --git a/Userland/Libraries/LibGUI/ModelIndex.h b/Userland/Libraries/LibGUI/ModelIndex.h index 6e7141021d4..07743383a5d 100644 --- a/Userland/Libraries/LibGUI/ModelIndex.h +++ b/Userland/Libraries/LibGUI/ModelIndex.h @@ -66,12 +66,11 @@ namespace AK { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, const GUI::ModelIndex& value) + ErrorOr format(FormatBuilder& builder, GUI::ModelIndex const& value) { if (value.internal_data()) return Formatter::format(builder, "ModelIndex({},{},{})", value.row(), value.column(), value.internal_data()); - else - return Formatter::format(builder, "ModelIndex({},{})", value.row(), value.column()); + return Formatter::format(builder, "ModelIndex({},{})", value.row(), value.column()); } }; diff --git a/Userland/Libraries/LibGUI/PersistentModelIndex.h b/Userland/Libraries/LibGUI/PersistentModelIndex.h index 1ef3f39fb69..4efa8826421 100644 --- a/Userland/Libraries/LibGUI/PersistentModelIndex.h +++ b/Userland/Libraries/LibGUI/PersistentModelIndex.h @@ -73,7 +73,7 @@ namespace AK { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, const GUI::PersistentModelIndex& value) + ErrorOr format(FormatBuilder& builder, GUI::PersistentModelIndex const& value) { return Formatter::format(builder, "PersistentModelIndex({},{},{})", value.row(), value.column(), value.internal_data()); } diff --git a/Userland/Libraries/LibGUI/TextPosition.h b/Userland/Libraries/LibGUI/TextPosition.h index e8621ba1f1c..f650c1c6ca2 100644 --- a/Userland/Libraries/LibGUI/TextPosition.h +++ b/Userland/Libraries/LibGUI/TextPosition.h @@ -40,11 +40,11 @@ private: template<> struct AK::Formatter : AK::Formatter { - void format(FormatBuilder& builder, const GUI::TextPosition& value) + ErrorOr format(FormatBuilder& builder, GUI::TextPosition const& value) { if (value.is_valid()) - Formatter::format(builder, "({},{})", value.line(), value.column()); - else - Formatter::format(builder, "GUI::TextPosition(Invalid)"); + return Formatter::format(builder, "({},{})", value.line(), value.column()); + + return Formatter::format(builder, "GUI::TextPosition(Invalid)"); } }; diff --git a/Userland/Libraries/LibGUI/TextRange.h b/Userland/Libraries/LibGUI/TextRange.h index fb372890249..de9f13102cf 100644 --- a/Userland/Libraries/LibGUI/TextRange.h +++ b/Userland/Libraries/LibGUI/TextRange.h @@ -68,11 +68,10 @@ private: template<> struct AK::Formatter : AK::Formatter { - void format(FormatBuilder& builder, const GUI::TextRange& value) + ErrorOr format(FormatBuilder& builder, GUI::TextRange const& value) { if (value.is_valid()) return Formatter::format(builder, "{}-{}", value.start(), value.end()); - else - return Formatter::format(builder, "GUI::TextRange(Invalid)"); + return Formatter::format(builder, "GUI::TextRange(Invalid)"); } }; diff --git a/Userland/Libraries/LibGfx/AffineTransform.h b/Userland/Libraries/LibGfx/AffineTransform.h index 63277b10490..1ec3ace816d 100644 --- a/Userland/Libraries/LibGfx/AffineTransform.h +++ b/Userland/Libraries/LibGfx/AffineTransform.h @@ -70,7 +70,7 @@ private: template<> struct AK::Formatter : Formatter { - void format(FormatBuilder& builder, Gfx::AffineTransform value) + ErrorOr format(FormatBuilder& builder, Gfx::AffineTransform const& value) { return Formatter::format(builder, "[{} {} {} {} {} {}]", value.a(), value.b(), value.c(), value.d(), value.e(), value.f()); } diff --git a/Userland/Libraries/LibGfx/BMPLoader.cpp b/Userland/Libraries/LibGfx/BMPLoader.cpp index f8056a0596b..d96512fec0f 100644 --- a/Userland/Libraries/LibGfx/BMPLoader.cpp +++ b/Userland/Libraries/LibGfx/BMPLoader.cpp @@ -75,9 +75,9 @@ namespace AK { template struct Formatter> : Formatter { - void format(FormatBuilder& builder, const Gfx::Endpoint& value) + ErrorOr format(FormatBuilder& builder, Gfx::Endpoint const& value) { - Formatter::format(builder, String::formatted("({}, {}, {})", value.x, value.y, value.z)); + return Formatter::format(builder, String::formatted("({}, {}, {})", value.x, value.y, value.z)); } }; diff --git a/Userland/Libraries/LibGfx/Color.cpp b/Userland/Libraries/LibGfx/Color.cpp index 92fd42c0b6b..30bb560a72b 100644 --- a/Userland/Libraries/LibGfx/Color.cpp +++ b/Userland/Libraries/LibGfx/Color.cpp @@ -352,7 +352,7 @@ bool IPC::decode(IPC::Decoder& decoder, Color& color) return true; } -void AK::Formatter::format(FormatBuilder& builder, Gfx::Color const& value) +ErrorOr AK::Formatter::format(FormatBuilder& builder, Gfx::Color const& value) { - Formatter::format(builder, value.to_string()); + return Formatter::format(builder, value.to_string()); } diff --git a/Userland/Libraries/LibGfx/Color.h b/Userland/Libraries/LibGfx/Color.h index 1c217415686..860c12f0335 100644 --- a/Userland/Libraries/LibGfx/Color.h +++ b/Userland/Libraries/LibGfx/Color.h @@ -478,7 +478,7 @@ namespace AK { template<> struct Formatter : public Formatter { - void format(FormatBuilder& builder, Gfx::Color const& value); + ErrorOr format(FormatBuilder&, Gfx::Color const&); }; } diff --git a/Userland/Libraries/LibGfx/Point.h b/Userland/Libraries/LibGfx/Point.h index 88afb4147d6..809a504a437 100644 --- a/Userland/Libraries/LibGfx/Point.h +++ b/Userland/Libraries/LibGfx/Point.h @@ -280,9 +280,9 @@ namespace AK { template struct Formatter> : Formatter { - void format(FormatBuilder& builder, Gfx::Point const& value) + ErrorOr format(FormatBuilder& builder, Gfx::Point const& value) { - Formatter::format(builder, value.to_string()); + return Formatter::format(builder, value.to_string()); } }; diff --git a/Userland/Libraries/LibGfx/Rect.h b/Userland/Libraries/LibGfx/Rect.h index ab9370ffce4..ef61e965b84 100644 --- a/Userland/Libraries/LibGfx/Rect.h +++ b/Userland/Libraries/LibGfx/Rect.h @@ -726,9 +726,9 @@ namespace AK { template struct Formatter> : Formatter { - void format(FormatBuilder& builder, const Gfx::Rect& value) + ErrorOr format(FormatBuilder& builder, Gfx::Rect const& value) { - Formatter::format(builder, value.to_string()); + return Formatter::format(builder, value.to_string()); } }; diff --git a/Userland/Libraries/LibGfx/Size.h b/Userland/Libraries/LibGfx/Size.h index 66814a11371..52e3ac514e5 100644 --- a/Userland/Libraries/LibGfx/Size.h +++ b/Userland/Libraries/LibGfx/Size.h @@ -178,9 +178,9 @@ namespace AK { template struct Formatter> : Formatter { - void format(FormatBuilder& builder, Gfx::Size const& value) + ErrorOr format(FormatBuilder& builder, Gfx::Size const& value) { - Formatter::format(builder, value.to_string()); + return Formatter::format(builder, value.to_string()); } }; diff --git a/Userland/Libraries/LibGfx/Vector2.h b/Userland/Libraries/LibGfx/Vector2.h index 92c8043ff4f..1868e8f8aad 100644 --- a/Userland/Libraries/LibGfx/Vector2.h +++ b/Userland/Libraries/LibGfx/Vector2.h @@ -127,9 +127,9 @@ namespace AK { template struct Formatter> : Formatter { - void format(FormatBuilder& builder, const Gfx::Vector2& value) + ErrorOr format(FormatBuilder& builder, Gfx::Vector2 const& value) { - Formatter::format(builder, value.to_string()); + return Formatter::format(builder, value.to_string()); } }; diff --git a/Userland/Libraries/LibGfx/Vector3.h b/Userland/Libraries/LibGfx/Vector3.h index 0e17d2721e9..c6485eecb55 100644 --- a/Userland/Libraries/LibGfx/Vector3.h +++ b/Userland/Libraries/LibGfx/Vector3.h @@ -144,9 +144,9 @@ namespace AK { template struct Formatter> : Formatter { - void format(FormatBuilder& builder, const Gfx::Vector3& value) + ErrorOr format(FormatBuilder& builder, Gfx::Vector3 const& value) { - Formatter::format(builder, value.to_string()); + return Formatter::format(builder, value.to_string()); } }; diff --git a/Userland/Libraries/LibGfx/Vector4.h b/Userland/Libraries/LibGfx/Vector4.h index 7745af60f46..53e9c7bc148 100644 --- a/Userland/Libraries/LibGfx/Vector4.h +++ b/Userland/Libraries/LibGfx/Vector4.h @@ -145,9 +145,9 @@ namespace AK { template struct Formatter> : Formatter { - void format(FormatBuilder& builder, const Gfx::Vector4& value) + ErrorOr format(FormatBuilder& builder, Gfx::Vector4 const& value) { - Formatter::format(builder, value.to_string()); + return Formatter::format(builder, value.to_string()); } }; diff --git a/Userland/Libraries/LibJS/Bytecode/Label.h b/Userland/Libraries/LibJS/Bytecode/Label.h index b1634b1d0d6..a858ef0fc02 100644 --- a/Userland/Libraries/LibJS/Bytecode/Label.h +++ b/Userland/Libraries/LibJS/Bytecode/Label.h @@ -28,7 +28,7 @@ private: template<> struct AK::Formatter : AK::Formatter { - void format(FormatBuilder& builder, JS::Bytecode::Label const& value) + ErrorOr format(FormatBuilder& builder, JS::Bytecode::Label const& value) { return AK::Formatter::format(builder, "@{}", value.block().name()); } diff --git a/Userland/Libraries/LibJS/Bytecode/Register.h b/Userland/Libraries/LibJS/Bytecode/Register.h index 1de1826c3ad..aeae630f3dc 100644 --- a/Userland/Libraries/LibJS/Bytecode/Register.h +++ b/Userland/Libraries/LibJS/Bytecode/Register.h @@ -42,7 +42,7 @@ private: template<> struct AK::Formatter : AK::Formatter { - void format(FormatBuilder& builder, JS::Bytecode::Register const& value) + ErrorOr format(FormatBuilder& builder, JS::Bytecode::Register const& value) { if (value.index() == JS::Bytecode::Register::accumulator_index) return AK::Formatter::format(builder, "acc"); diff --git a/Userland/Libraries/LibJS/Heap/Cell.h b/Userland/Libraries/LibJS/Heap/Cell.h index 85bc0e56752..4e6701e6cae 100644 --- a/Userland/Libraries/LibJS/Heap/Cell.h +++ b/Userland/Libraries/LibJS/Heap/Cell.h @@ -75,11 +75,11 @@ private: template<> struct AK::Formatter : AK::Formatter { - void format(FormatBuilder& builder, const JS::Cell* cell) + ErrorOr format(FormatBuilder& builder, JS::Cell const* cell) { if (!cell) - Formatter::format(builder, "Cell{nullptr}"); + return Formatter::format(builder, "Cell{nullptr}"); else - Formatter::format(builder, "{}({})", cell->class_name(), cell); + return Formatter::format(builder, "{}({})", cell->class_name(), cell); } }; diff --git a/Userland/Libraries/LibJS/Runtime/PropertyAttributes.h b/Userland/Libraries/LibJS/Runtime/PropertyAttributes.h index a31b0e048f2..f00020246a5 100644 --- a/Userland/Libraries/LibJS/Runtime/PropertyAttributes.h +++ b/Userland/Libraries/LibJS/Runtime/PropertyAttributes.h @@ -75,13 +75,13 @@ namespace AK { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, JS::PropertyAttributes const& property_attributes) + ErrorOr format(FormatBuilder& builder, JS::PropertyAttributes const& property_attributes) { Vector 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::format(builder, String::formatted("PropertyAttributes {{ {} }}", String::join(", ", parts))); + return Formatter::format(builder, String::formatted("PropertyAttributes {{ {} }}", String::join(", ", parts))); } }; diff --git a/Userland/Libraries/LibJS/Runtime/PropertyDescriptor.h b/Userland/Libraries/LibJS/Runtime/PropertyDescriptor.h index 4a3b5fbbb74..2fd209147d3 100644 --- a/Userland/Libraries/LibJS/Runtime/PropertyDescriptor.h +++ b/Userland/Libraries/LibJS/Runtime/PropertyDescriptor.h @@ -47,7 +47,7 @@ namespace AK { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, JS::PropertyDescriptor const& property_descriptor) + ErrorOr format(FormatBuilder& builder, JS::PropertyDescriptor const& property_descriptor) { Vector parts; if (property_descriptor.value.has_value()) @@ -62,7 +62,7 @@ struct Formatter : Formatter { parts.append(String::formatted("[[Enumerable]]: {}", *property_descriptor.enumerable)); if (property_descriptor.configurable.has_value()) parts.append(String::formatted("[[Configurable]]: {}", *property_descriptor.configurable)); - Formatter::format(builder, String::formatted("PropertyDescriptor {{ {} }}", String::join(", ", parts))); + return Formatter::format(builder, String::formatted("PropertyDescriptor {{ {} }}", String::join(", ", parts))); } }; diff --git a/Userland/Libraries/LibJS/Runtime/PropertyKey.h b/Userland/Libraries/LibJS/Runtime/PropertyKey.h index b6eb92505ca..f74cf56bc2a 100644 --- a/Userland/Libraries/LibJS/Runtime/PropertyKey.h +++ b/Userland/Libraries/LibJS/Runtime/PropertyKey.h @@ -225,14 +225,13 @@ struct Traits : public GenericTraits { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, JS::PropertyKey const& property_name) + ErrorOr format(FormatBuilder& builder, JS::PropertyKey const& property_name) { if (!property_name.is_valid()) - Formatter::format(builder, ""); - else if (property_name.is_number()) - Formatter::format(builder, String::number(property_name.as_number())); - else - Formatter::format(builder, property_name.to_string_or_symbol().to_display_string()); + return Formatter::format(builder, ""); + if (property_name.is_number()) + return Formatter::format(builder, String::number(property_name.as_number())); + return Formatter::format(builder, property_name.to_string_or_symbol().to_display_string()); } }; diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h index 4a29327dd0e..52e7dd2c437 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.h +++ b/Userland/Libraries/LibJS/Runtime/Value.h @@ -458,9 +458,9 @@ namespace AK { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, const JS::Value& value) + ErrorOr format(FormatBuilder& builder, JS::Value value) { - Formatter::format(builder, value.is_empty() ? "" : value.to_string_without_side_effects()); + return Formatter::format(builder, value.is_empty() ? "" : value.to_string_without_side_effects()); } }; diff --git a/Userland/Libraries/LibPDF/Command.h b/Userland/Libraries/LibPDF/Command.h index 01f69aedc07..3f9572c742b 100644 --- a/Userland/Libraries/LibPDF/Command.h +++ b/Userland/Libraries/LibPDF/Command.h @@ -166,7 +166,7 @@ namespace AK { template<> struct Formatter : Formatter { - void format(FormatBuilder& format_builder, PDF::Command const& command) + ErrorOr format(FormatBuilder& format_builder, PDF::Command const& command) { StringBuilder builder; builder.appendff("{} ({})", @@ -180,7 +180,7 @@ struct Formatter : Formatter { builder.append(" ]"); } - Formatter::format(format_builder, builder.to_string()); + return Formatter::format(format_builder, builder.to_string()); } }; diff --git a/Userland/Libraries/LibPDF/Document.h b/Userland/Libraries/LibPDF/Document.h index ddd26443cdc..02303cee02a 100644 --- a/Userland/Libraries/LibPDF/Document.h +++ b/Userland/Libraries/LibPDF/Document.h @@ -143,9 +143,9 @@ namespace AK { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, PDF::Rectangle const& rectangle) + ErrorOr format(FormatBuilder& builder, PDF::Rectangle const& rectangle) { - Formatter::format(builder, + return Formatter::format(builder, String::formatted("Rectangle {{ ll=({}, {}), ur=({}, {}) }}", rectangle.lower_left_x, rectangle.lower_left_y, @@ -156,7 +156,7 @@ struct Formatter : Formatter { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, PDF::Page const& page) + ErrorOr 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 : Formatter { page.crop_box, page.user_unit, page.rotate); - Formatter::format(builder, str); + return Formatter::format(builder, str); } }; template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, PDF::Destination const& destination) + ErrorOr format(FormatBuilder& builder, PDF::Destination const& destination) { String type_str; switch (destination.type) { @@ -207,21 +207,21 @@ struct Formatter : Formatter { param_builder.appendff("{} ", param); auto str = String::formatted("{{ type={} page={} params={} }}", type_str, destination.page, param_builder.to_string()); - Formatter::format(builder, str); + return Formatter::format(builder, str); } }; template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, PDF::OutlineItem const& item) + ErrorOr format(FormatBuilder& builder, PDF::OutlineItem const& item) { - Formatter::format(builder, item.to_string(0)); + return Formatter::format(builder, item.to_string(0)); } }; template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, PDF::OutlineDict const& dict) + ErrorOr format(FormatBuilder& builder, PDF::OutlineDict const& dict) { StringBuilder child_builder; child_builder.append('['); @@ -229,7 +229,7 @@ struct Formatter : Formatter { child_builder.appendff("{}\n", child.to_string(2)); child_builder.append(" ]"); - Formatter::format(builder, + return Formatter::format(builder, String::formatted("OutlineDict {{\n count={}\n children={}\n}}", dict.count, child_builder.to_string())); } }; diff --git a/Userland/Libraries/LibPDF/Object.h b/Userland/Libraries/LibPDF/Object.h index f3d0fea884d..5774434ef08 100644 --- a/Userland/Libraries/LibPDF/Object.h +++ b/Userland/Libraries/LibPDF/Object.h @@ -63,17 +63,17 @@ namespace AK { template struct Formatter : Formatter { - void format(FormatBuilder& builder, T const& object) + ErrorOr format(FormatBuilder& builder, T const& object) { - Formatter::format(builder, object.to_string(0)); + return Formatter::format(builder, object.to_string(0)); } }; template struct Formatter> : Formatter { - void format(FormatBuilder& builder, NonnullRefPtr const& object) + ErrorOr format(FormatBuilder& builder, NonnullRefPtr const& object) { - Formatter::format(builder, *object); + return Formatter::format(builder, *object); } }; diff --git a/Userland/Libraries/LibPDF/Parser.cpp b/Userland/Libraries/LibPDF/Parser.cpp index 105b5d7f3b9..744a88521ad 100644 --- a/Userland/Libraries/LibPDF/Parser.cpp +++ b/Userland/Libraries/LibPDF/Parser.cpp @@ -1187,7 +1187,7 @@ namespace AK { template<> struct Formatter : Formatter { - void format(FormatBuilder& format_builder, PDF::Parser::LinearizationDictionary const& dict) + ErrorOr format(FormatBuilder& format_builder, PDF::Parser::LinearizationDictionary const& dict) { StringBuilder builder; builder.append("{\n"); @@ -1202,13 +1202,13 @@ struct Formatter : Formatter { 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::format(format_builder, builder.to_string()); + return Formatter::format(format_builder, builder.to_string()); } }; template<> struct Formatter : Formatter { - void format(FormatBuilder& format_builder, PDF::Parser::PageOffsetHintTable const& table) + ErrorOr format(FormatBuilder& format_builder, PDF::Parser::PageOffsetHintTable const& table) { StringBuilder builder; builder.append("{\n"); @@ -1226,13 +1226,13 @@ struct Formatter : Formatter { 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::format(format_builder, builder.to_string()); + return Formatter::format(format_builder, builder.to_string()); } }; template<> struct Formatter : Formatter { - void format(FormatBuilder& format_builder, PDF::Parser::PageOffsetHintTableEntry const& entry) + ErrorOr format(FormatBuilder& format_builder, PDF::Parser::PageOffsetHintTableEntry const& entry) { StringBuilder builder; builder.append("{\n"); @@ -1250,7 +1250,7 @@ struct Formatter : Formatter 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::format(format_builder, builder.to_string()); + return Formatter::format(format_builder, builder.to_string()); } }; diff --git a/Userland/Libraries/LibPDF/Renderer.h b/Userland/Libraries/LibPDF/Renderer.h index b2daebc19a5..eb8e0f2b631 100644 --- a/Userland/Libraries/LibPDF/Renderer.h +++ b/Userland/Libraries/LibPDF/Renderer.h @@ -135,43 +135,37 @@ namespace AK { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, PDF::LineCapStyle const& style) + ErrorOr format(FormatBuilder& builder, PDF::LineCapStyle const& style) { switch (style) { case PDF::LineCapStyle::ButtCap: - Formatter::format(builder, "LineCapStyle::ButtCap"); - break; + return Formatter::format(builder, "LineCapStyle::ButtCap"); case PDF::LineCapStyle::RoundCap: - Formatter::format(builder, "LineCapStyle::RoundCap"); - break; + return Formatter::format(builder, "LineCapStyle::RoundCap"); case PDF::LineCapStyle::SquareCap: - Formatter::format(builder, "LineCapStyle::SquareCap"); - break; + return Formatter::format(builder, "LineCapStyle::SquareCap"); } } }; template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, PDF::LineJoinStyle const& style) + ErrorOr format(FormatBuilder& builder, PDF::LineJoinStyle const& style) { switch (style) { case PDF::LineJoinStyle::Miter: - Formatter::format(builder, "LineJoinStyle::Miter"); - break; + return Formatter::format(builder, "LineJoinStyle::Miter"); case PDF::LineJoinStyle::Round: - Formatter::format(builder, "LineJoinStyle::Round"); - break; + return Formatter::format(builder, "LineJoinStyle::Round"); case PDF::LineJoinStyle::Bevel: - Formatter::format(builder, "LineJoinStyle::Bevel"); - break; + return Formatter::format(builder, "LineJoinStyle::Bevel"); } } }; template<> struct Formatter : Formatter { - void format(FormatBuilder& format_builder, PDF::LineDashPattern const& pattern) + ErrorOr format(FormatBuilder& format_builder, PDF::LineDashPattern const& pattern) { StringBuilder builder; builder.append("["); @@ -191,40 +185,32 @@ struct Formatter : Formatter { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, PDF::TextRenderingMode const& style) + ErrorOr format(FormatBuilder& builder, PDF::TextRenderingMode const& style) { switch (style) { case PDF::TextRenderingMode::Fill: - Formatter::format(builder, "TextRenderingMode::Fill"); - break; + return Formatter::format(builder, "TextRenderingMode::Fill"); case PDF::TextRenderingMode::Stroke: - Formatter::format(builder, "TextRenderingMode::Stroke"); - break; + return Formatter::format(builder, "TextRenderingMode::Stroke"); case PDF::TextRenderingMode::FillThenStroke: - Formatter::format(builder, "TextRenderingMode::FillThenStroke"); - break; + return Formatter::format(builder, "TextRenderingMode::FillThenStroke"); case PDF::TextRenderingMode::Invisible: - Formatter::format(builder, "TextRenderingMode::Invisible"); - break; + return Formatter::format(builder, "TextRenderingMode::Invisible"); case PDF::TextRenderingMode::FillAndClip: - Formatter::format(builder, "TextRenderingMode::FillAndClip"); - break; + return Formatter::format(builder, "TextRenderingMode::FillAndClip"); case PDF::TextRenderingMode::StrokeAndClip: - Formatter::format(builder, "TextRenderingMode::StrokeAndClip"); - break; + return Formatter::format(builder, "TextRenderingMode::StrokeAndClip"); case PDF::TextRenderingMode::FillStrokeAndClip: - Formatter::format(builder, "TextRenderingMode::FillStrokeAndClip"); - break; + return Formatter::format(builder, "TextRenderingMode::FillStrokeAndClip"); case PDF::TextRenderingMode::Clip: - Formatter::format(builder, "TextRenderingMode::Clip"); - break; + return Formatter::format(builder, "TextRenderingMode::Clip"); } } }; template<> struct Formatter : Formatter { - void format(FormatBuilder& format_builder, PDF::TextState const& state) + ErrorOr format(FormatBuilder& format_builder, PDF::TextState const& state) { StringBuilder builder; builder.append("TextState {\n"); @@ -239,13 +225,13 @@ struct Formatter : Formatter { builder.appendff(" rise={}\n", state.rise); builder.appendff(" knockout={}\n", state.knockout); builder.append(" }"); - Formatter::format(format_builder, builder.to_string()); + return Formatter::format(format_builder, builder.to_string()); } }; template<> struct Formatter : Formatter { - void format(FormatBuilder& format_builder, PDF::GraphicsState const& state) + ErrorOr format(FormatBuilder& format_builder, PDF::GraphicsState const& state) { StringBuilder builder; builder.append("GraphicsState {\n"); @@ -259,7 +245,7 @@ struct Formatter : Formatter { builder.appendff(" line_dash_pattern={}\n", state.line_dash_pattern); builder.appendff(" text_state={}\n", state.text_state); builder.append("}"); - Formatter::format(format_builder, builder.to_string()); + return Formatter::format(format_builder, builder.to_string()); } }; diff --git a/Userland/Libraries/LibPDF/Value.h b/Userland/Libraries/LibPDF/Value.h index 498fb20d1d3..efce1a30b0b 100644 --- a/Userland/Libraries/LibPDF/Value.h +++ b/Userland/Libraries/LibPDF/Value.h @@ -85,9 +85,9 @@ namespace AK { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, PDF::Value const& value) + ErrorOr format(FormatBuilder& builder, PDF::Value const& value) { - Formatter::format(builder, value.to_string()); + return Formatter::format(builder, value.to_string()); } }; diff --git a/Userland/Libraries/LibPDF/XRefTable.h b/Userland/Libraries/LibPDF/XRefTable.h index 5ac0cd355f0..efb87f23147 100644 --- a/Userland/Libraries/LibPDF/XRefTable.h +++ b/Userland/Libraries/LibPDF/XRefTable.h @@ -101,9 +101,9 @@ namespace AK { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, PDF::XRefEntry const& entry) + ErrorOr format(FormatBuilder& builder, PDF::XRefEntry const& entry) { - Formatter::format(builder, + return Formatter::format(builder, String::formatted("XRefEntry {{ offset={} generation={} used={} }}", entry.byte_offset, entry.generation_number, @@ -113,14 +113,14 @@ struct Formatter : Formatter { template<> struct Formatter : Formatter { - void format(FormatBuilder& format_builder, PDF::XRefTable const& table) + ErrorOr 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::format(format_builder, builder.to_string()); + return Formatter::format(format_builder, builder.to_string()); } }; diff --git a/Userland/Libraries/LibRegex/RegexMatch.h b/Userland/Libraries/LibRegex/RegexMatch.h index 2ebd0f5f8c0..201b98e0660 100644 --- a/Userland/Libraries/LibRegex/RegexMatch.h +++ b/Userland/Libraries/LibRegex/RegexMatch.h @@ -536,7 +536,7 @@ using regex::RegexStringView; template<> struct AK::Formatter : Formatter { - void format(FormatBuilder& builder, regex::RegexStringView value) + ErrorOr format(FormatBuilder& builder, regex::RegexStringView value) { auto string = value.to_string(); return Formatter::format(builder, string); diff --git a/Userland/Libraries/LibWasm/AbstractMachine/Validator.h b/Userland/Libraries/LibWasm/AbstractMachine/Validator.h index e4f8b928ef1..4e8010f98f4 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/Validator.h +++ b/Userland/Libraries/LibWasm/AbstractMachine/Validator.h @@ -286,27 +286,27 @@ private: template<> struct AK::Formatter : public AK::Formatter { - void format(FormatBuilder& builder, Wasm::Validator::StackEntry const& value) + ErrorOr format(FormatBuilder& builder, Wasm::Validator::StackEntry const& value) { if (value.is_known) return Formatter::format(builder, Wasm::ValueType::kind_name(value.concrete_type.kind())); - Formatter::format(builder, ""sv); + return Formatter::format(builder, ""sv); } }; template<> struct AK::Formatter : public AK::Formatter { - void format(FormatBuilder& builder, Wasm::ValueType const& value) + ErrorOr format(FormatBuilder& builder, Wasm::ValueType const& value) { - Formatter::format(builder, Wasm::ValueType::kind_name(value.kind())); + return Formatter::format(builder, Wasm::ValueType::kind_name(value.kind())); } }; template<> struct AK::Formatter : public AK::Formatter { - void format(FormatBuilder& builder, Wasm::ValidationError const& error) + ErrorOr format(FormatBuilder& builder, Wasm::ValidationError const& error) { - Formatter::format(builder, error.error_string); + return Formatter::format(builder, error.error_string); } }; diff --git a/Userland/Libraries/LibWeb/CSS/MediaQuery.h b/Userland/Libraries/LibWeb/CSS/MediaQuery.h index 5cb14662bf6..845918cedf4 100644 --- a/Userland/Libraries/LibWeb/CSS/MediaQuery.h +++ b/Userland/Libraries/LibWeb/CSS/MediaQuery.h @@ -101,25 +101,25 @@ namespace AK { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, Web::CSS::MediaQuery::MediaFeature const& media_feature) + ErrorOr format(FormatBuilder& builder, Web::CSS::MediaQuery::MediaFeature const& media_feature) { - Formatter::format(builder, media_feature.to_string()); + return Formatter::format(builder, media_feature.to_string()); } }; template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, Web::CSS::MediaQuery::MediaCondition const& media_condition) + ErrorOr format(FormatBuilder& builder, Web::CSS::MediaQuery::MediaCondition const& media_condition) { - Formatter::format(builder, media_condition.to_string()); + return Formatter::format(builder, media_condition.to_string()); } }; template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, Web::CSS::MediaQuery const& media_query) + ErrorOr format(FormatBuilder& builder, Web::CSS::MediaQuery const& media_query) { - Formatter::format(builder, media_query.to_string()); + return Formatter::format(builder, media_query.to_string()); } }; diff --git a/Userland/Libraries/LibWeb/CSS/Selector.h b/Userland/Libraries/LibWeb/CSS/Selector.h index fd7ba600232..2c613581031 100644 --- a/Userland/Libraries/LibWeb/CSS/Selector.h +++ b/Userland/Libraries/LibWeb/CSS/Selector.h @@ -151,9 +151,9 @@ namespace AK { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, Web::CSS::Selector const& selector) + ErrorOr format(FormatBuilder& builder, Web::CSS::Selector const& selector) { - Formatter::format(builder, selector.serialize()); + return Formatter::format(builder, selector.serialize()); } }; diff --git a/Userland/Libraries/LibWeb/DOM/Position.h b/Userland/Libraries/LibWeb/DOM/Position.h index b6d1bd3ffbd..1fc5737af2f 100644 --- a/Userland/Libraries/LibWeb/DOM/Position.h +++ b/Userland/Libraries/LibWeb/DOM/Position.h @@ -51,9 +51,9 @@ private: namespace AK { template<> struct Formatter : Formatter { - void format(FormatBuilder& builder, const Web::DOM::Position& value) + ErrorOr format(FormatBuilder& builder, Web::DOM::Position const& value) { - Formatter::format(builder, value.to_string()); + return Formatter::format(builder, value.to_string()); } }; diff --git a/Userland/Services/LookupServer/DNSAnswer.cpp b/Userland/Services/LookupServer/DNSAnswer.cpp index b4c9e8fc9a5..54791175818 100644 --- a/Userland/Services/LookupServer/DNSAnswer.cpp +++ b/Userland/Services/LookupServer/DNSAnswer.cpp @@ -28,50 +28,42 @@ bool DNSAnswer::has_expired() const } -void AK::Formatter::format(AK::FormatBuilder& builder, LookupServer::DNSRecordType value) +ErrorOr AK::Formatter::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::format(AK::FormatBuilder& builder, LookupServer::DNSRecordClass value) +ErrorOr AK::Formatter::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 {}; } diff --git a/Userland/Services/LookupServer/DNSAnswer.h b/Userland/Services/LookupServer/DNSAnswer.h index 8b8560efdd0..4cf3db4df6f 100644 --- a/Userland/Services/LookupServer/DNSAnswer.h +++ b/Userland/Services/LookupServer/DNSAnswer.h @@ -65,7 +65,7 @@ struct AK::Formatter : StandardFormatter { { } - void format(AK::FormatBuilder&, LookupServer::DNSRecordType); + ErrorOr format(AK::FormatBuilder&, LookupServer::DNSRecordType); }; template<> @@ -76,5 +76,5 @@ struct AK::Formatter : StandardFormatter { { } - void format(AK::FormatBuilder&, LookupServer::DNSRecordClass); + ErrorOr format(AK::FormatBuilder&, LookupServer::DNSRecordClass); }; diff --git a/Userland/Services/LookupServer/DNSName.h b/Userland/Services/LookupServer/DNSName.h index 17fbe7a5577..3a2cb4ba1da 100644 --- a/Userland/Services/LookupServer/DNSName.h +++ b/Userland/Services/LookupServer/DNSName.h @@ -41,7 +41,7 @@ OutputStream& operator<<(OutputStream& stream, const DNSName&); template<> struct AK::Formatter : Formatter { - void format(FormatBuilder& builder, const LookupServer::DNSName& value) + ErrorOr format(FormatBuilder& builder, LookupServer::DNSName const& value) { return Formatter::format(builder, value.as_string()); } diff --git a/Userland/Shell/AST.cpp b/Userland/Shell/AST.cpp index a32c22f69ee..09587b3573f 100644 --- a/Userland/Shell/AST.cpp +++ b/Userland/Shell/AST.cpp @@ -19,7 +19,7 @@ #include #include -void AK::Formatter::format(FormatBuilder& builder, const Shell::AST::Command& value) +ErrorOr AK::Formatter::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::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::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 { diff --git a/Userland/Shell/AST.h b/Userland/Shell/AST.h index 4f3722854fd..7661b68b565 100644 --- a/Userland/Shell/AST.h +++ b/Userland/Shell/AST.h @@ -1512,7 +1512,7 @@ struct Formatter : StandardFormatter { { } - void format(FormatBuilder&, const Shell::AST::Command& value); + ErrorOr format(FormatBuilder&, Shell::AST::Command const& value); }; } diff --git a/Userland/Utilities/strace.cpp b/Userland/Utilities/strace.cpp index b4669fd8565..8b300af9372 100644 --- a/Userland/Utilities/strace.cpp +++ b/Userland/Utilities/strace.cpp @@ -273,7 +273,7 @@ struct Formatter : StandardFormatter { { } - void format(FormatBuilder& format_builder, BitflagDerivative const& value) + ErrorOr format(FormatBuilder& format_builder, BitflagDerivative const& value) { bool had_any_output = false; int remaining = value.flagset; @@ -283,25 +283,27 @@ struct Formatter : 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(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 : StandardFormatter { { } - void format(FormatBuilder& format_builder, PointerArgument const& value) + ErrorOr 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 : StandardFormatter { - void format(FormatBuilder& format_builder, struct timespec value) + ErrorOr 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 : StandardFormatter { - void format(FormatBuilder& format_builder, struct timeval value) + ErrorOr 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 : StandardFormatter { - void format(FormatBuilder& format_builder, struct stat value) + ErrorOr format(FormatBuilder& format_builder, struct stat value) { auto& builder = format_builder.builder(); builder.appendff( @@ -504,6 +509,7 @@ struct Formatter : 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 : StandardFormatter { - void format(FormatBuilder& format_builder, struct sockaddr address) + ErrorOr format(FormatBuilder& format_builder, struct sockaddr address) { auto& builder = format_builder.builder(); builder.append("{sa_family="); @@ -579,6 +585,7 @@ struct Formatter : StandardFormatter { address_un->sun_path); } builder.append('}'); + return {}; } }; } diff --git a/Userland/Utilities/syscall.cpp b/Userland/Utilities/syscall.cpp index de119af74a0..4eaf603f7db 100644 --- a/Userland/Utilities/syscall.cpp +++ b/Userland/Utilities/syscall.cpp @@ -28,7 +28,7 @@ static FlatPtr parse_from(ArgIter&); template<> struct AK::Formatter : Formatter { - void format(FormatBuilder& builder, Syscall::Function function) + ErrorOr format(FormatBuilder& builder, Syscall::Function function) { return Formatter::format(builder, to_string(function)); }