UI/Qt: Add a utility to create a QString using our own format infra

QString::arg has an internal limit of 8 arguments. Let's add the ability
to use our own formatting logic going forward.
This commit is contained in:
Timothy Flynn 2026-05-26 13:07:49 -04:00 committed by Tim Flynn
parent 8e85f5a0c4
commit 87e84dabe4
2 changed files with 32 additions and 0 deletions

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/StringBuilder.h>
#include <LibURL/Parser.h>
#include <UI/Qt/StringUtils.h>
@ -41,6 +42,13 @@ QString qstring_from_utf16_string(Utf16View const& string)
return QString::fromUtf16(string.utf16_span().data(), static_cast<qsizetype>(string.length_in_code_units()));
}
QString vqformatted(StringView format, AK::TypeErasedFormatParams& parameters)
{
StringBuilder builder(StringBuilder::Mode::UTF16);
MUST(vformat(builder, format, parameters));
return qstring_from_utf16_string(builder.utf16_string_view());
}
QByteArray qbytearray_from_ak_string(StringView ak_string)
{
return { ak_string.characters_without_null_termination(), static_cast<qsizetype>(ak_string.length()) };

View file

@ -8,10 +8,12 @@
#include <AK/ByteString.h>
#include <AK/Error.h>
#include <AK/Format.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <AK/Utf16String.h>
#include <AK/Utf16View.h>
#include <LibURL/URL.h>
#include <QByteArray>
@ -27,7 +29,29 @@ QString qstring_from_ak_string(StringView);
Utf16String utf16_string_from_qstring(QString const&);
QString qstring_from_utf16_string(Utf16View const&);
QString vqformatted(StringView, AK::TypeErasedFormatParams&);
template<typename... Parameters>
QString qformatted(CheckedFormatString<Parameters...>&& format, Parameters const&... parameters)
{
AK::VariadicFormatParams<AK::AllowDebugOnlyFormatters::No, Parameters...> variadic_format_parameters { parameters... };
return vqformatted(format.view(), variadic_format_parameters);
}
QByteArray qbytearray_from_ak_string(StringView);
Optional<URL::URL> ak_url_from_qstring(QString const&);
URL::URL ak_url_from_qurl(QUrl const&);
namespace AK {
template<>
struct Formatter<QString> : Formatter<Utf16View> {
ErrorOr<void> format(FormatBuilder& builder, QString const& value)
{
Utf16View view { reinterpret_cast<char16_t const*>(value.utf16()), static_cast<size_t>(value.size()) };
return Formatter<Utf16View>::format(builder, view);
}
};
}