mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 05:25:32 +03:00
AK: Add formatters for Span<T> and Span<T const>
This generalizes the formatter currently used for Vector to be usable for any Span.
This commit is contained in:
parent
c372012842
commit
949f5460fb
Notes:
sideshowbarker
2024-07-17 03:37:54 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/949f5460fb Pull-request: https://github.com/SerenityOS/serenity/pull/16358 Reviewed-by: https://github.com/alimpfard
27
AK/Format.h
27
AK/Format.h
@ -329,15 +329,16 @@ struct Formatter<StringView> : StandardFormatter {
|
||||
ErrorOr<void> format(FormatBuilder&, StringView);
|
||||
};
|
||||
|
||||
template<typename T, size_t inline_capacity>
|
||||
requires(HasFormatter<T>) struct Formatter<Vector<T, inline_capacity>> : StandardFormatter {
|
||||
|
||||
template<typename T>
|
||||
requires(HasFormatter<T>)
|
||||
struct Formatter<Span<T const>> : StandardFormatter {
|
||||
Formatter() = default;
|
||||
explicit Formatter(StandardFormatter formatter)
|
||||
: StandardFormatter(move(formatter))
|
||||
{
|
||||
}
|
||||
ErrorOr<void> format(FormatBuilder& builder, Vector<T> value)
|
||||
|
||||
ErrorOr<void> format(FormatBuilder& builder, Span<T const> value)
|
||||
{
|
||||
if (m_mode == Mode::Pointer) {
|
||||
Formatter<FlatPtr> formatter { *this };
|
||||
@ -375,6 +376,24 @@ requires(HasFormatter<T>) struct Formatter<Vector<T, inline_capacity>> : Standar
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
requires(HasFormatter<T>)
|
||||
struct Formatter<Span<T>> : Formatter<Span<T const>> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, Span<T> value)
|
||||
{
|
||||
return Formatter<Span<T const>>::format(builder, value);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T, size_t inline_capacity>
|
||||
requires(HasFormatter<T>)
|
||||
struct Formatter<Vector<T, inline_capacity>> : Formatter<Span<T const>> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, Vector<T, inline_capacity> const& value)
|
||||
{
|
||||
return Formatter<Span<T const>>::format(builder, value.span());
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Formatter<ReadonlyBytes> : Formatter<StringView> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, ReadonlyBytes value)
|
||||
|
@ -316,6 +316,25 @@ TEST_CASE(hex_dump)
|
||||
EXPECT_EQ(DeprecatedString::formatted("{:*>4hex-dump}", "0000"), "30303030****0000");
|
||||
}
|
||||
|
||||
TEST_CASE(span_format)
|
||||
{
|
||||
{
|
||||
Vector<int> v { 1, 2, 3, 4 };
|
||||
EXPECT_EQ(DeprecatedString::formatted("{}", v.span()), "[ 1, 2, 3, 4 ]");
|
||||
EXPECT_EQ(DeprecatedString::formatted("{}", const_cast<AddConst<decltype(v)>&>(v).span()), "[ 1, 2, 3, 4 ]");
|
||||
}
|
||||
{
|
||||
Vector<StringView> v { "1"sv, "2"sv, "3"sv, "4"sv };
|
||||
EXPECT_EQ(DeprecatedString::formatted("{}", v.span()), "[ 1, 2, 3, 4 ]");
|
||||
EXPECT_EQ(DeprecatedString::formatted("{}", const_cast<AddConst<decltype(v)>&>(v).span()), "[ 1, 2, 3, 4 ]");
|
||||
}
|
||||
{
|
||||
Vector<Vector<DeprecatedString>> v { { "1"sv, "2"sv }, { "3"sv, "4"sv } };
|
||||
EXPECT_EQ(DeprecatedString::formatted("{}", v.span()), "[ [ 1, 2 ], [ 3, 4 ] ]");
|
||||
EXPECT_EQ(DeprecatedString::formatted("{}", const_cast<AddConst<decltype(v)>&>(v).span()), "[ [ 1, 2 ], [ 3, 4 ] ]");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE(vector_format)
|
||||
{
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user