Meta+Userland: Simplify some formatters

These are mostly minor mistakes I've encountered while working on the
removal of StringView(char const*). The usage of builder.put_string over
Format<FormatString>::format is preferrable as it will avoid the
indirection altogether when there's no formatting to be done. Similarly,
there is no need to do format(builder, "{}", number) when
builder.put_u64(number) works equally well.

Additionally a few Strings where only constant strings were used are
replaced with StringViews.
This commit is contained in:
sin-ack 2022-07-11 20:23:24 +00:00 committed by Andreas Kling
parent 7da00bfa8d
commit 7456904a39
Notes: sideshowbarker 2024-07-17 09:27:58 +09:00
9 changed files with 64 additions and 67 deletions

View file

@ -530,7 +530,7 @@ template<>
struct AK::Formatter<Unicode::HourCycle> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, Unicode::HourCycle hour_cycle)
{
return Formatter<FormatString>::format(builder, "{}", to_underlying(hour_cycle));
return builder.put_u64(to_underlying(hour_cycle));
}
};

View file

@ -62,8 +62,9 @@ struct Formatter<Core::Directory> : Formatter<StringView> {
{
auto path = directory.path();
if (path.is_error())
return Formatter<StringView>::format(builder, "<unknown>");
return Formatter<StringView>::format(builder, path.release_value().string());
TRY(builder.put_string("<unknown>"sv));
TRY(builder.put_string(path.release_value().string()));
return {};
}
};

View file

@ -401,24 +401,24 @@ ErrorOr<void> AK::Formatter<Crypto::ASN1::DecodeError>::format(FormatBuilder& fm
switch (error) {
case DecodeError::NoInput:
return Formatter<StringView>::format(fmtbuilder, "DecodeError(No input provided)");
return fmtbuilder.put_string("DecodeError(No input provided)"sv);
case DecodeError::NonConformingType:
return Formatter<StringView>::format(fmtbuilder, "DecodeError(Tried to read with a non-conforming type)");
return fmtbuilder.put_string("DecodeError(Tried to read with a non-conforming type)"sv);
case DecodeError::EndOfStream:
return Formatter<StringView>::format(fmtbuilder, "DecodeError(End of stream)");
return fmtbuilder.put_string("DecodeError(End of stream)"sv);
case DecodeError::NotEnoughData:
return Formatter<StringView>::format(fmtbuilder, "DecodeError(Not enough data)");
return fmtbuilder.put_string("DecodeError(Not enough data)"sv);
case DecodeError::EnteringNonConstructedTag:
return Formatter<StringView>::format(fmtbuilder, "DecodeError(Tried to enter a primitive tag)");
return fmtbuilder.put_string("DecodeError(Tried to enter a primitive tag)"sv);
case DecodeError::LeavingMainContext:
return Formatter<StringView>::format(fmtbuilder, "DecodeError(Tried to leave the main context)");
return fmtbuilder.put_string("DecodeError(Tried to leave the main context)"sv);
case DecodeError::InvalidInputFormat:
return Formatter<StringView>::format(fmtbuilder, "DecodeError(Input data contained invalid syntax/data)");
return fmtbuilder.put_string("DecodeError(Input data contained invalid syntax/data)"sv);
case DecodeError::Overflow:
return Formatter<StringView>::format(fmtbuilder, "DecodeError(Construction would overflow)");
return fmtbuilder.put_string("DecodeError(Construction would overflow)"sv);
case DecodeError::UnsupportedFormat:
return Formatter<StringView>::format(fmtbuilder, "DecodeError(Input data format not supported by this parser)");
return fmtbuilder.put_string("DecodeError(Input data format not supported by this parser)"sv);
default:
return Formatter<StringView>::format(fmtbuilder, "DecodeError(Unknown)");
return fmtbuilder.put_string("DecodeError(Unknown)"sv);
}
}

View file

@ -375,7 +375,7 @@ bool UnsignedBigInteger::operator>=(UnsignedBigInteger const& other) const
ErrorOr<void> AK::Formatter<Crypto::UnsignedBigInteger>::format(FormatBuilder& fmtbuilder, Crypto::UnsignedBigInteger const& value)
{
if (value.is_invalid())
return Formatter<StringView>::format(fmtbuilder, "invalid");
return fmtbuilder.put_string("invalid"sv);
StringBuilder builder;
for (int i = value.length() - 1; i >= 0; --i)

View file

@ -86,8 +86,7 @@ struct Traits<GUI::PersistentModelIndex> : public GenericTraits<GUI::PersistentM
{
if (index.has_valid_handle())
return Traits<GUI::ModelIndex>::hash(index.m_handle->m_index);
else
return 0;
return 0;
}
};

View file

@ -70,8 +70,7 @@ struct AK::Formatter<JS::Cell> : AK::Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, JS::Cell const* cell)
{
if (!cell)
return Formatter<FormatString>::format(builder, "Cell{nullptr}");
else
return Formatter<FormatString>::format(builder, "{}({})", cell->class_name(), cell);
return builder.put_string("Cell{nullptr}"sv);
return Formatter<FormatString>::format(builder, "{}({})"sv, cell->class_name(), cell);
}
};

View file

@ -228,10 +228,10 @@ struct Formatter<JS::PropertyKey> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, JS::PropertyKey const& property_key)
{
if (!property_key.is_valid())
return Formatter<StringView>::format(builder, "<invalid PropertyKey>");
return builder.put_string("<invalid PropertyKey>"sv);
if (property_key.is_number())
return Formatter<StringView>::format(builder, String::number(property_key.as_number()));
return Formatter<StringView>::format(builder, property_key.to_string_or_symbol().to_display_string());
return builder.put_u64(property_key.as_number());
return builder.put_string(property_key.to_string_or_symbol().to_display_string());
}
};

View file

@ -165,63 +165,62 @@ private:
namespace AK {
template<>
struct Formatter<PDF::Rectangle> : Formatter<StringView> {
struct Formatter<PDF::Rectangle> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, PDF::Rectangle const& rectangle)
{
return Formatter<StringView>::format(builder,
String::formatted("Rectangle {{ ll=({}, {}), ur=({}, {}) }}",
rectangle.lower_left_x,
rectangle.lower_left_y,
rectangle.upper_right_x,
rectangle.upper_right_y));
return Formatter<FormatString>::format(builder,
"Rectangle {{ ll=({}, {}), ur=({}, {}) }}"sv,
rectangle.lower_left_x,
rectangle.lower_left_y,
rectangle.upper_right_x,
rectangle.upper_right_y);
}
};
template<>
struct Formatter<PDF::Page> : Formatter<StringView> {
struct Formatter<PDF::Page> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, PDF::Page const& page)
{
constexpr auto fmt_string = "Page {{\n resources={}\n contents={}\n media_box={}\n crop_box={}\n user_unit={}\n rotate={}\n}}";
auto str = String::formatted(fmt_string,
return Formatter<FormatString>::format(builder,
"Page {{\n resources={}\n contents={}\n media_box={}\n crop_box={}\n user_unit={}\n rotate={}\n}}"sv,
page.resources->to_string(1),
page.contents->to_string(1),
page.media_box,
page.crop_box,
page.user_unit,
page.rotate);
return Formatter<StringView>::format(builder, str);
}
};
template<>
struct Formatter<PDF::Destination> : Formatter<StringView> {
struct Formatter<PDF::Destination> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, PDF::Destination const& destination)
{
String type_str;
StringView type_str;
switch (destination.type) {
case PDF::Destination::Type::XYZ:
type_str = "XYZ";
type_str = "XYZ"sv;
break;
case PDF::Destination::Type::Fit:
type_str = "Fit";
type_str = "Fit"sv;
break;
case PDF::Destination::Type::FitH:
type_str = "FitH";
type_str = "FitH"sv;
break;
case PDF::Destination::Type::FitV:
type_str = "FitV";
type_str = "FitV"sv;
break;
case PDF::Destination::Type::FitR:
type_str = "FitR";
type_str = "FitR"sv;
break;
case PDF::Destination::Type::FitB:
type_str = "FitB";
type_str = "FitB"sv;
break;
case PDF::Destination::Type::FitBH:
type_str = "FitBH";
type_str = "FitBH"sv;
break;
case PDF::Destination::Type::FitBV:
type_str = "FitBV";
type_str = "FitBV"sv;
break;
}
@ -229,21 +228,20 @@ struct Formatter<PDF::Destination> : Formatter<StringView> {
for (auto& param : destination.parameters)
param_builder.appendff("{} ", param);
auto str = String::formatted("{{ type={} page={} params={} }}", type_str, destination.page, param_builder.to_string());
return Formatter<StringView>::format(builder, str);
return Formatter<FormatString>::format(builder, "{{ type={} page={} params={} }}"sv, type_str, destination.page, param_builder.to_string());
}
};
template<>
struct Formatter<PDF::OutlineItem> : Formatter<StringView> {
struct Formatter<PDF::OutlineItem> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, PDF::OutlineItem const& item)
{
return Formatter<StringView>::format(builder, item.to_string(0));
return builder.put_string(item.to_string(0));
}
};
template<>
struct Formatter<PDF::OutlineDict> : Formatter<StringView> {
struct Formatter<PDF::OutlineDict> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, PDF::OutlineDict const& dict)
{
StringBuilder child_builder;
@ -252,8 +250,8 @@ struct Formatter<PDF::OutlineDict> : Formatter<StringView> {
child_builder.appendff("{}\n", child.to_string(2));
child_builder.append(" ]");
return Formatter<StringView>::format(builder,
String::formatted("OutlineDict {{\n count={}\n children={}\n}}", dict.count, child_builder.to_string()));
return Formatter<FormatString>::format(builder,
"OutlineDict {{\n count={}\n children={}\n}}"sv, dict.count, child_builder.to_string());
}
};

View file

@ -143,11 +143,11 @@ struct Formatter<PDF::LineCapStyle> : Formatter<StringView> {
{
switch (style) {
case PDF::LineCapStyle::ButtCap:
return Formatter<StringView>::format(builder, "LineCapStyle::ButtCap");
return builder.put_string("LineCapStyle::ButtCap"sv);
case PDF::LineCapStyle::RoundCap:
return Formatter<StringView>::format(builder, "LineCapStyle::RoundCap");
return builder.put_string("LineCapStyle::RoundCap"sv);
case PDF::LineCapStyle::SquareCap:
return Formatter<StringView>::format(builder, "LineCapStyle::SquareCap");
return builder.put_string("LineCapStyle::SquareCap"sv);
}
}
};
@ -158,11 +158,11 @@ struct Formatter<PDF::LineJoinStyle> : Formatter<StringView> {
{
switch (style) {
case PDF::LineJoinStyle::Miter:
return Formatter<StringView>::format(builder, "LineJoinStyle::Miter");
return builder.put_string("LineJoinStyle::Miter"sv);
case PDF::LineJoinStyle::Round:
return Formatter<StringView>::format(builder, "LineJoinStyle::Round");
return builder.put_string("LineJoinStyle::Round"sv);
case PDF::LineJoinStyle::Bevel:
return Formatter<StringView>::format(builder, "LineJoinStyle::Bevel");
return builder.put_string("LineJoinStyle::Bevel"sv);
}
}
};
@ -183,7 +183,7 @@ struct Formatter<PDF::LineDashPattern> : Formatter<StringView> {
}
builder.appendff("] {}", pattern.phase);
return Formatter<StringView>::format(format_builder, builder.to_string());
return format_builder.put_string(builder.to_string());
}
};
@ -193,21 +193,21 @@ struct Formatter<PDF::TextRenderingMode> : Formatter<StringView> {
{
switch (style) {
case PDF::TextRenderingMode::Fill:
return Formatter<StringView>::format(builder, "TextRenderingMode::Fill");
return builder.put_string("TextRenderingMode::Fill"sv);
case PDF::TextRenderingMode::Stroke:
return Formatter<StringView>::format(builder, "TextRenderingMode::Stroke");
return builder.put_string("TextRenderingMode::Stroke"sv);
case PDF::TextRenderingMode::FillThenStroke:
return Formatter<StringView>::format(builder, "TextRenderingMode::FillThenStroke");
return builder.put_string("TextRenderingMode::FillThenStroke"sv);
case PDF::TextRenderingMode::Invisible:
return Formatter<StringView>::format(builder, "TextRenderingMode::Invisible");
return builder.put_string("TextRenderingMode::Invisible"sv);
case PDF::TextRenderingMode::FillAndClip:
return Formatter<StringView>::format(builder, "TextRenderingMode::FillAndClip");
return builder.put_string("TextRenderingMode::FillAndClip"sv);
case PDF::TextRenderingMode::StrokeAndClip:
return Formatter<StringView>::format(builder, "TextRenderingMode::StrokeAndClip");
return builder.put_string("TextRenderingMode::StrokeAndClip"sv);
case PDF::TextRenderingMode::FillStrokeAndClip:
return Formatter<StringView>::format(builder, "TextRenderingMode::FillStrokeAndClip");
return builder.put_string("TextRenderingMode::FillStrokeAndClip"sv);
case PDF::TextRenderingMode::Clip:
return Formatter<StringView>::format(builder, "TextRenderingMode::Clip");
return builder.put_string("TextRenderingMode::Clip"sv);
}
}
};
@ -229,7 +229,7 @@ struct Formatter<PDF::TextState> : Formatter<StringView> {
builder.appendff(" rise={}\n", state.rise);
builder.appendff(" knockout={}\n", state.knockout);
builder.append(" }");
return Formatter<StringView>::format(format_builder, builder.to_string());
return format_builder.put_string(builder.to_string());
}
};
@ -249,7 +249,7 @@ struct Formatter<PDF::GraphicsState> : Formatter<StringView> {
builder.appendff(" line_dash_pattern={}\n", state.line_dash_pattern);
builder.appendff(" text_state={}\n", state.text_state);
builder.append("}");
return Formatter<StringView>::format(format_builder, builder.to_string());
return format_builder.put_string(builder.to_string());
}
};