mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
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:
parent
cd41af5ac2
commit
4cc41ea186
Notes:
sideshowbarker
2024-07-17 23:19:47 +09:00
Author: https://github.com/bgianfo Commit: https://github.com/SerenityOS/serenity/commit/4cc41ea186c Pull-request: https://github.com/SerenityOS/serenity/pull/11126
2 changed files with 18 additions and 0 deletions
|
@ -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.
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue