Kernel: Add an error propagating KString::format(..) API :^)

In the continuous effort of better handling OOM in the kernel,
we want to move away from all AK::String usage. One of the final
pieces left to accomplish this goal is replacing all of the usages
of `String::formatted` with something that can actually propagate
failure.

The StringBuilder API was enhanced in the recent past to propagate
failure and thus a slightly modified version of what exists in
`AK::format` will work well for implementing failable format with
`KString`.
This commit is contained in:
Brian Gianforcaro 2021-11-20 00:40:57 -08:00 committed by Andreas Kling
parent cd41af5ac2
commit 4cc41ea186
Notes: sideshowbarker 2024-07-17 23:19:47 +09:00
2 changed files with 18 additions and 0 deletions

View file

@ -4,6 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Format.h>
#include <AK/StringBuilder.h>
#include <Kernel/KString.h>
extern bool g_in_early_boot;
@ -21,6 +23,13 @@ ErrorOr<NonnullOwnPtr<KString>> KString::try_create(StringView string)
return new_string;
}
ErrorOr<NonnullOwnPtr<KString>> KString::vformatted(StringView fmtstr, AK::TypeErasedFormatParams& params)
{
StringBuilder builder;
TRY(AK::vformat(builder, fmtstr, params));
return try_create(builder.string_view());
}
NonnullOwnPtr<KString> KString::must_create(StringView string)
{
// We can only enforce success during early boot.

View file

@ -21,6 +21,15 @@ public:
[[nodiscard]] static ErrorOr<NonnullOwnPtr<KString>> try_create(StringView);
[[nodiscard]] static NonnullOwnPtr<KString> must_create(StringView);
[[nodiscard]] static ErrorOr<NonnullOwnPtr<KString>> vformatted(StringView fmtstr, AK::TypeErasedFormatParams&);
template<typename... Parameters>
[[nodiscard]] static ErrorOr<NonnullOwnPtr<KString>> formatted(CheckedFormatString<Parameters...>&& fmtstr, const Parameters&... parameters)
{
AK::VariadicFormatParams variadic_format_parameters { parameters... };
return vformatted(fmtstr.view(), variadic_format_parameters);
}
void operator delete(void*);
ErrorOr<NonnullOwnPtr<KString>> try_clone() const;