From 519a1cd23ddb2b87810a9e87b1a3a00c8155f371 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 10 Feb 2023 11:34:20 -0500 Subject: [PATCH] LibJS: Add a fallible ThrowableStringBuilder::appendff --- .../LibJS/Runtime/ThrowableStringBuilder.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/ThrowableStringBuilder.h b/Userland/Libraries/LibJS/Runtime/ThrowableStringBuilder.h index a5958bf968..463b590f35 100644 --- a/Userland/Libraries/LibJS/Runtime/ThrowableStringBuilder.h +++ b/Userland/Libraries/LibJS/Runtime/ThrowableStringBuilder.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include #include @@ -25,6 +26,19 @@ public: ThrowCompletionOr append_code_point(u32 value); ThrowCompletionOr to_string() const; + template + ThrowCompletionOr appendff(CheckedFormatString&& fmtstr, Parameters const&... parameters) + { + AK::VariadicFormatParams variadic_format_params { parameters... }; + + if (vformat(*this, fmtstr.view(), variadic_format_params).is_error()) { + // The size returned here is a bit of an estimate, as we don't know what the final formatted string length would be. + return m_vm.throw_completion(ErrorType::NotEnoughMemoryToAllocate, length() + fmtstr.view().length()); + } + + return {}; + } + using AK::StringBuilder::is_empty; using AK::StringBuilder::string_view; using AK::StringBuilder::trim;