AK: Remove infallible version of StringBuilder::to_byte_buffer

Also drop the try_ prefix from the fallible function, as it is no longer
needed to distinguish the two.
This commit is contained in:
Linus Groh 2023-03-09 15:00:14 +00:00
parent ad5e8f2742
commit e76394d96c
Notes: sideshowbarker 2024-07-17 04:09:56 +09:00
13 changed files with 21 additions and 28 deletions

View file

@ -105,15 +105,9 @@ void StringBuilder::append_repeated(char ch, size_t n)
MUST(try_append_repeated(ch, n));
}
ByteBuffer StringBuilder::to_byte_buffer() const
ErrorOr<ByteBuffer> StringBuilder::to_byte_buffer() const
{
// FIXME: Handle OOM failure.
return ByteBuffer::copy(data(), length()).release_value_but_fixme_should_propagate_errors();
}
ErrorOr<ByteBuffer> StringBuilder::try_to_byte_buffer() const
{
return TRY(ByteBuffer::copy(data(), length()));
return ByteBuffer::copy(data(), length());
}
#ifndef KERNEL

View file

@ -68,8 +68,7 @@ public:
ErrorOr<String> to_string() const;
ErrorOr<FlyString> to_fly_string() const;
[[nodiscard]] ByteBuffer to_byte_buffer() const;
[[nodiscard]] ErrorOr<ByteBuffer> try_to_byte_buffer() const;
[[nodiscard]] ErrorOr<ByteBuffer> to_byte_buffer() const;
[[nodiscard]] StringView string_view() const;
void clear();

View file

@ -50,7 +50,7 @@ ErrorOr<void> DomainListModel::save()
TRY(builder.try_appendff("{}\n", domain));
auto file = TRY(Core::File::open(filter_list_file_path(), Core::File::OpenMode::Write));
TRY(file->write(TRY(builder.try_to_byte_buffer()).bytes()));
TRY(file->write(TRY(builder.to_byte_buffer()).bytes()));
return {};
}

View file

@ -277,7 +277,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
}
auto file = file_or_error.release_value().release_stream();
auto byte_buffer_or_error = full_backtrace.try_to_byte_buffer();
auto byte_buffer_or_error = full_backtrace.to_byte_buffer();
if (byte_buffer_or_error.is_error()) {
GUI::MessageBox::show(window, DeprecatedString::formatted("Couldn't create backtrace: {}.", byte_buffer_or_error.release_error()), "Saving backtrace failed"sv, GUI::MessageBox::Type::Error);
return;

View file

@ -38,7 +38,7 @@ ErrorOr<void> MimeData::set_urls(Vector<URL> const& urls)
TRY(builder.try_append(url.to_deprecated_string()));
TRY(builder.try_append('\n'));
}
set_data("text/uri-list", TRY(builder.try_to_byte_buffer()));
set_data("text/uri-list", TRY(builder.to_byte_buffer()));
return {};
}

View file

@ -952,7 +952,7 @@ ErrorOr<ByteBuffer> GLContext::build_extension_string()
TRY(string_builder.try_join(' ', extensions));
// Create null-terminated string
auto extensions_bytes = TRY(string_builder.try_to_byte_buffer());
auto extensions_bytes = TRY(string_builder.to_byte_buffer());
TRY(extensions_bytes.try_append(0));
return extensions_bytes;
}

View file

@ -125,7 +125,7 @@ RefPtr<Core::MimeData> Model::mime_data(ModelSelection const& selection) const
}
});
mime_data->set_data(drag_data_type(), data_builder.try_to_byte_buffer().release_value_but_fixme_should_propagate_errors());
mime_data->set_data(drag_data_type(), data_builder.to_byte_buffer().release_value_but_fixme_should_propagate_errors());
mime_data->set_text(text_builder.to_deprecated_string());
if (bitmap)
mime_data->set_data("image/x-raw-bitmap", bitmap->serialize_to_byte_buffer().release_value_but_fixme_should_propagate_errors());

View file

@ -15,7 +15,7 @@ ErrorOr<ByteBuffer> GeminiRequest::to_raw_request() const
StringBuilder builder;
TRY(builder.try_append(m_url.to_deprecated_string()));
TRY(builder.try_append("\r\n"sv));
return builder.try_to_byte_buffer();
return builder.to_byte_buffer();
}
Optional<GeminiRequest> GeminiRequest::from_raw_request(ByteBuffer const& raw_request)

View file

@ -72,7 +72,7 @@ ErrorOr<ByteBuffer> HttpRequest::to_raw_request() const
TRY(builder.try_append((char const*)m_body.data(), m_body.size()));
}
TRY(builder.try_append("\r\n"sv));
return builder.try_to_byte_buffer();
return builder.to_byte_buffer();
}
Optional<HttpRequest> HttpRequest::from_raw_request(ReadonlyBytes raw_request)

View file

@ -80,7 +80,7 @@ ErrorOr<ByteBuffer> decode_quoted_printable(StringView input)
}
}
return output.try_to_byte_buffer();
return output.to_byte_buffer();
}
}

View file

@ -220,7 +220,7 @@ ErrorOr<void, Client::WrappedError> Client::on_ready_to_read()
break;
}
m_request = HTTP::HttpRequest::from_raw_request(TRY(builder.try_to_byte_buffer()));
m_request = HTTP::HttpRequest::from_raw_request(TRY(builder.to_byte_buffer()));
if (!m_request.has_value())
return {};
@ -278,7 +278,7 @@ ErrorOr<void, Client::WrappedError> Client::send_success_response(JsonValue resu
builder.appendff("Content-Length: {}\r\n", content.length());
builder.append("\r\n"sv);
auto builder_contents = TRY(builder.try_to_byte_buffer());
auto builder_contents = TRY(builder.to_byte_buffer());
TRY(m_socket->write(builder_contents));
while (!content.is_empty()) {
@ -319,8 +319,8 @@ ErrorOr<void, Client::WrappedError> Client::send_error_response(Error const& err
header_builder.appendff("Content-Length: {}\r\n", content_builder.length());
header_builder.append("\r\n"sv);
TRY(m_socket->write(TRY(header_builder.try_to_byte_buffer())));
TRY(m_socket->write(TRY(content_builder.try_to_byte_buffer())));
TRY(m_socket->write(TRY(header_builder.to_byte_buffer())));
TRY(m_socket->write(TRY(content_builder.to_byte_buffer())));
log_response(error.http_status);
return {};

View file

@ -182,7 +182,7 @@ ErrorOr<void> Client::send_data(StringView data)
}
}
auto builder_contents = TRY(builder.try_to_byte_buffer());
auto builder_contents = TRY(builder.to_byte_buffer());
TRY(m_socket->write(builder_contents));
return {};
}

View file

@ -81,7 +81,7 @@ void Client::start()
builder.append("\r\n"sv);
}
auto request = builder.try_to_byte_buffer().release_value_but_fixme_should_propagate_errors();
auto request = builder.to_byte_buffer().release_value_but_fixme_should_propagate_errors();
dbgln_if(WEBSERVER_DEBUG, "Got raw request: '{}'", DeprecatedString::copy(request));
auto maybe_did_handle = handle_request(request);
@ -191,7 +191,7 @@ ErrorOr<void> Client::send_response(Stream& response, HTTP::HttpRequest const& r
builder.appendff("Content-Length: {}\r\n", content_info.length);
builder.append("\r\n"sv);
auto builder_contents = TRY(builder.try_to_byte_buffer());
auto builder_contents = TRY(builder.to_byte_buffer());
TRY(m_socket->write(builder_contents));
log_response(200, request);
@ -233,7 +233,7 @@ ErrorOr<void> Client::send_redirect(StringView redirect_path, HTTP::HttpRequest
builder.append("\r\n"sv);
builder.append("\r\n"sv);
auto builder_contents = TRY(builder.try_to_byte_buffer());
auto builder_contents = TRY(builder.to_byte_buffer());
TRY(m_socket->write(builder_contents));
log_response(301, request);
@ -363,8 +363,8 @@ ErrorOr<void> Client::send_error_response(unsigned code, HTTP::HttpRequest const
header_builder.append("Content-Type: text/html; charset=UTF-8\r\n"sv);
header_builder.appendff("Content-Length: {}\r\n", content_builder.length());
header_builder.append("\r\n"sv);
TRY(m_socket->write(TRY(header_builder.try_to_byte_buffer())));
TRY(m_socket->write(TRY(content_builder.try_to_byte_buffer())));
TRY(m_socket->write(TRY(header_builder.to_byte_buffer())));
TRY(m_socket->write(TRY(content_builder.to_byte_buffer())));
log_response(code, request);
return {};