mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:07:35 +00:00
LibJS: Add and begin using a completion-compatible string formatter
The `deprecated_format` helper is a thin wrapper to map results from AK::vformat to a throw completion. This will let us try to throw on OOM conditions rather than just blowing up. Note it's called `deprecated_format` as we will likely end up adding a method named just `format` to return `ThrowCompletionOr<String>`, when we begin the migration from DeprecatedString->String for LibJS.
This commit is contained in:
parent
6e1a239a62
commit
356e58332c
2 changed files with 31 additions and 2 deletions
|
@ -23,6 +23,7 @@
|
||||||
#include <LibJS/Runtime/StringIterator.h>
|
#include <LibJS/Runtime/StringIterator.h>
|
||||||
#include <LibJS/Runtime/StringObject.h>
|
#include <LibJS/Runtime/StringObject.h>
|
||||||
#include <LibJS/Runtime/StringPrototype.h>
|
#include <LibJS/Runtime/StringPrototype.h>
|
||||||
|
#include <LibJS/Runtime/ThrowableFormat.h>
|
||||||
#include <LibJS/Runtime/ThrowableStringBuilder.h>
|
#include <LibJS/Runtime/ThrowableStringBuilder.h>
|
||||||
#include <LibJS/Runtime/Utf16String.h>
|
#include <LibJS/Runtime/Utf16String.h>
|
||||||
#include <LibJS/Runtime/Value.h>
|
#include <LibJS/Runtime/Value.h>
|
||||||
|
@ -536,8 +537,8 @@ static ThrowCompletionOr<Value> pad_string(VM& vm, Utf16String string, PadPlacem
|
||||||
auto filler = filler_builder.build();
|
auto filler = filler_builder.build();
|
||||||
|
|
||||||
auto formatted = placement == PadPlacement::Start
|
auto formatted = placement == PadPlacement::Start
|
||||||
? DeprecatedString::formatted("{}{}", filler, string.view())
|
? TRY(deprecated_format(vm, "{}{}", filler, string.view()))
|
||||||
: DeprecatedString::formatted("{}{}", string.view(), filler);
|
: TRY(deprecated_format(vm, "{}{}", string.view(), filler));
|
||||||
return PrimitiveString::create(vm, move(formatted));
|
return PrimitiveString::create(vm, move(formatted));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
28
Userland/Libraries/LibJS/Runtime/ThrowableFormat.h
Normal file
28
Userland/Libraries/LibJS/Runtime/ThrowableFormat.h
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/CheckedFormatString.h>
|
||||||
|
#include <AK/DeprecatedString.h>
|
||||||
|
#include <AK/Format.h>
|
||||||
|
#include <LibJS/Runtime/Completion.h>
|
||||||
|
#include <LibJS/Runtime/ErrorTypes.h>
|
||||||
|
#include <LibJS/Runtime/VM.h>
|
||||||
|
|
||||||
|
namespace JS {
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
ThrowCompletionOr<DeprecatedString> deprecated_format(VM& vm, CheckedFormatString<Args...>&& fmtstr, Args const&... args)
|
||||||
|
{
|
||||||
|
StringBuilder builder;
|
||||||
|
AK::VariadicFormatParams parameters { args... };
|
||||||
|
|
||||||
|
TRY_OR_THROW_OOM(vm, vformat(builder, fmtstr.view(), parameters));
|
||||||
|
return builder.to_deprecated_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue