diff options
author | sin-ack <sin-ack@users.noreply.github.com> | 2022-07-11 20:23:24 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-07-12 23:11:35 +0200 |
commit | 7456904a398c62cb87349fb4b47dd3e507fd634e (patch) | |
tree | b0fb2d75dd0aba46dd293e3b19cd9a178927f9c1 | |
parent | 7da00bfa8d03741e74a0a50f0c17a15b92ca94e2 (diff) | |
download | serenity-7456904a398c62cb87349fb4b47dd3e507fd634e.zip |
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.
-rw-r--r-- | Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/Directory.h | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibCrypto/ASN1/DER.cpp | 20 | ||||
-rw-r--r-- | Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/PersistentModelIndex.h | 3 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Heap/Cell.h | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/PropertyKey.h | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibPDF/Document.h | 54 | ||||
-rw-r--r-- | Userland/Libraries/LibPDF/Renderer.h | 34 |
9 files changed, 64 insertions, 67 deletions
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp index 5a1476462e..c2e478706c 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp @@ -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)); } }; diff --git a/Userland/Libraries/LibCore/Directory.h b/Userland/Libraries/LibCore/Directory.h index 5eebe5f32c..1405477bce 100644 --- a/Userland/Libraries/LibCore/Directory.h +++ b/Userland/Libraries/LibCore/Directory.h @@ -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 {}; } }; diff --git a/Userland/Libraries/LibCrypto/ASN1/DER.cpp b/Userland/Libraries/LibCrypto/ASN1/DER.cpp index 339759340c..49e0b90ba8 100644 --- a/Userland/Libraries/LibCrypto/ASN1/DER.cpp +++ b/Userland/Libraries/LibCrypto/ASN1/DER.cpp @@ -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); } } diff --git a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp index b47b0f05ec..a0e30d96ed 100644 --- a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp +++ b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp @@ -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) diff --git a/Userland/Libraries/LibGUI/PersistentModelIndex.h b/Userland/Libraries/LibGUI/PersistentModelIndex.h index c3a56e77ba..5840010e49 100644 --- a/Userland/Libraries/LibGUI/PersistentModelIndex.h +++ b/Userland/Libraries/LibGUI/PersistentModelIndex.h @@ -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; } }; diff --git a/Userland/Libraries/LibJS/Heap/Cell.h b/Userland/Libraries/LibJS/Heap/Cell.h index be2d63dfff..b03f0dd960 100644 --- a/Userland/Libraries/LibJS/Heap/Cell.h +++ b/Userland/Libraries/LibJS/Heap/Cell.h @@ -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); } }; diff --git a/Userland/Libraries/LibJS/Runtime/PropertyKey.h b/Userland/Libraries/LibJS/Runtime/PropertyKey.h index fda6a53c08..a32c01dfc6 100644 --- a/Userland/Libraries/LibJS/Runtime/PropertyKey.h +++ b/Userland/Libraries/LibJS/Runtime/PropertyKey.h @@ -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()); } }; diff --git a/Userland/Libraries/LibPDF/Document.h b/Userland/Libraries/LibPDF/Document.h index d7836bb237..fd3401f905 100644 --- a/Userland/Libraries/LibPDF/Document.h +++ b/Userland/Libraries/LibPDF/Document.h @@ -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()); } }; diff --git a/Userland/Libraries/LibPDF/Renderer.h b/Userland/Libraries/LibPDF/Renderer.h index d74246e043..00e3cc978c 100644 --- a/Userland/Libraries/LibPDF/Renderer.h +++ b/Userland/Libraries/LibPDF/Renderer.h @@ -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()); } }; |