mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 08:24:58 +00:00
AK+Userland: Introduce ByteString::create_and_overwrite
And replace two users of raw StringImpl with it.
This commit is contained in:
parent
f7c952f842
commit
061f902f95
3 changed files with 28 additions and 12 deletions
|
@ -93,6 +93,23 @@ public:
|
||||||
static ByteString must_from_utf8(StringView string) { return MUST(from_utf8(string)); }
|
static ByteString must_from_utf8(StringView string) { return MUST(from_utf8(string)); }
|
||||||
static ByteString from_utf8_without_validation(StringView string) { return ByteString { 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(char, size_t count);
|
||||||
[[nodiscard]] static ByteString repeated(StringView, size_t count);
|
[[nodiscard]] static ByteString repeated(StringView, size_t count);
|
||||||
|
|
||||||
|
|
|
@ -394,10 +394,13 @@ hostent* gethostbyaddr(void const* addr, socklen_t addr_size, int type)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* buffer;
|
ssize_t nreceived;
|
||||||
auto string_impl = StringImpl::create_uninitialized(response_header.name_length, buffer);
|
|
||||||
|
|
||||||
if (auto nreceived = read(fd, buffer, response_header.name_length); nreceived < 0) {
|
gethostbyaddr_name_buffer = ByteString::create_and_overwrite(response_header.name_length, [&](Bytes bytes) {
|
||||||
|
nreceived = read(fd, bytes.data(), bytes.size());
|
||||||
|
});
|
||||||
|
|
||||||
|
if (nreceived < 0) {
|
||||||
h_errno = TRY_AGAIN;
|
h_errno = TRY_AGAIN;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
} else if (static_cast<u32>(nreceived) != response_header.name_length) {
|
} else if (static_cast<u32>(nreceived) != response_header.name_length) {
|
||||||
|
@ -405,8 +408,7 @@ hostent* gethostbyaddr(void const* addr, socklen_t addr_size, int type)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
gethostbyaddr_name_buffer = move(string_impl);
|
__gethostbyaddr_buffer.h_name = const_cast<char*>(gethostbyaddr_name_buffer.characters());
|
||||||
__gethostbyaddr_buffer.h_name = buffer;
|
|
||||||
__gethostbyaddr_alias_list_buffer[0] = nullptr;
|
__gethostbyaddr_alias_list_buffer[0] = nullptr;
|
||||||
__gethostbyaddr_buffer.h_aliases = __gethostbyaddr_alias_list_buffer;
|
__gethostbyaddr_buffer.h_aliases = __gethostbyaddr_alias_list_buffer;
|
||||||
__gethostbyaddr_buffer.h_addrtype = AF_INET;
|
__gethostbyaddr_buffer.h_addrtype = AF_INET;
|
||||||
|
|
|
@ -39,13 +39,10 @@ ErrorOr<ByteString> decode(Decoder& decoder)
|
||||||
if (length == 0)
|
if (length == 0)
|
||||||
return ByteString::empty();
|
return ByteString::empty();
|
||||||
|
|
||||||
char* text_buffer = nullptr;
|
return ByteString::create_and_overwrite(length, [&](Bytes bytes) -> ErrorOr<void> {
|
||||||
auto text_impl = StringImpl::create_uninitialized(length, text_buffer);
|
TRY(decoder.decode_into(bytes));
|
||||||
|
return {};
|
||||||
Bytes bytes { text_buffer, length };
|
});
|
||||||
TRY(decoder.decode_into(bytes));
|
|
||||||
|
|
||||||
return ByteString { *text_impl };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue