1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 04:57:45 +00:00

AK+Userland: Introduce ByteString::create_and_overwrite

And replace two users of raw StringImpl with it.
This commit is contained in:
Dan Klishch 2024-01-22 13:26:55 -05:00 committed by Andrew Kaster
parent f7c952f842
commit 061f902f95
3 changed files with 28 additions and 12 deletions

View file

@ -93,6 +93,23 @@ public:
static ByteString must_from_utf8(StringView string) { return MUST(from_utf8(string)); }
static ByteString from_utf8_without_validation(StringView string) { return ByteString { string }; }
template<
typename F,
typename PossiblyErrorOr = decltype(declval<F>()(declval<Bytes>())),
bool is_error_or = IsSpecializationOf<PossiblyErrorOr, ErrorOr>,
typename ReturnType = Conditional<is_error_or, ErrorOr<ByteString>, ByteString>>
static ReturnType create_and_overwrite(size_t length, F&& fill_function)
{
char* buffer;
auto impl = StringImpl::create_uninitialized(length, buffer);
if constexpr (is_error_or)
TRY(fill_function(Bytes { buffer, length }));
else
fill_function(Bytes { buffer, length });
return impl;
}
[[nodiscard]] static ByteString repeated(char, size_t count);
[[nodiscard]] static ByteString repeated(StringView, size_t count);