1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 06:27:45 +00:00

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
9 changed files with 64 additions and 67 deletions

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