LibCore+Everywhere: Make Core::Stream read_line() return StringView

Similar reasoning to making Core::Stream::read() return Bytes, except
that every user of read_line() creates a StringView from the result, so
let's just return one right away.
This commit is contained in:
Sam Atkins 2022-04-15 14:52:33 +01:00 committed by Tim Flynn
parent c4134e9794
commit d564cf1e89
Notes: sideshowbarker 2024-07-17 11:46:28 +09:00
9 changed files with 40 additions and 54 deletions

View file

@ -316,8 +316,7 @@ static ErrorOr<void> parse_time_zones(StringView time_zone_path, TimeZoneData& t
Vector<TimeZoneOffset>* last_parsed_zone = nullptr;
while (TRY(file->can_read_line())) {
auto nread = TRY(file->read_line(buffer));
StringView line { buffer.data(), nread };
auto line = TRY(file->read_line(buffer));
if (line.is_empty() || line.trim_whitespace(TrimMode::Left).starts_with('#'))
continue;
@ -374,8 +373,7 @@ static ErrorOr<void> parse_time_zone_coordinates(Core::Stream::BufferedFile& fil
Array<u8, 1024> buffer {};
while (TRY(file.can_read_line())) {
auto nread = TRY(file.read_line(buffer));
StringView line { buffer.data(), nread };
auto line = TRY(file.read_line(buffer));
if (line.is_empty() || line.trim_whitespace(TrimMode::Left).starts_with('#'))
continue;

View file

@ -197,8 +197,7 @@ static ErrorOr<void> parse_special_casing(Core::Stream::BufferedFile& file, Unic
Array<u8, 1024> buffer;
while (TRY(file.can_read_line())) {
auto nread = TRY(file.read_line(buffer));
StringView line { buffer.data(), nread };
auto line = TRY(file.read_line(buffer));
if (line.is_empty() || line.starts_with('#'))
continue;
@ -264,8 +263,7 @@ static ErrorOr<void> parse_prop_list(Core::Stream::BufferedFile& file, PropList&
Array<u8, 1024> buffer;
while (TRY(file.can_read_line())) {
auto nread = TRY(file.read_line(buffer));
StringView line { buffer.data(), nread };
auto line = TRY(file.read_line(buffer));
if (line.is_empty() || line.starts_with('#'))
continue;
@ -311,8 +309,7 @@ static ErrorOr<void> parse_alias_list(Core::Stream::BufferedFile& file, PropList
};
while (TRY(file.can_read_line())) {
auto nread = TRY(file.read_line(buffer));
StringView line { buffer.data(), nread };
auto line = TRY(file.read_line(buffer));
if (line.is_empty() || line.starts_with('#')) {
if (line.ends_with("Properties"sv))
@ -345,8 +342,7 @@ static ErrorOr<void> parse_name_aliases(Core::Stream::BufferedFile& file, Unicod
Array<u8, 1024> buffer;
while (TRY(file.can_read_line())) {
auto nread = TRY(file.read_line(buffer));
StringView line { buffer.data(), nread };
auto line = TRY(file.read_line(buffer));
if (line.is_empty() || line.starts_with('#'))
continue;
@ -387,8 +383,7 @@ static ErrorOr<void> parse_value_alias_list(Core::Stream::BufferedFile& file, St
};
while (TRY(file.can_read_line())) {
auto nread = TRY(file.read_line(buffer));
StringView line { buffer.data(), nread };
auto line = TRY(file.read_line(buffer));
if (line.is_empty() || line.starts_with('#'))
continue;
@ -421,8 +416,7 @@ static ErrorOr<void> parse_normalization_props(Core::Stream::BufferedFile& file,
Array<u8, 1024> buffer;
while (TRY(file.can_read_line())) {
auto nread = TRY(file.read_line(buffer));
StringView line { buffer.data(), nread };
auto line = TRY(file.read_line(buffer));
if (line.is_empty() || line.starts_with('#'))
continue;
@ -518,8 +512,7 @@ static ErrorOr<void> parse_block_display_names(Core::Stream::BufferedFile& file,
{
Array<u8, 1024> buffer;
while (TRY(file.can_read_line())) {
auto nread = TRY(file.read_line(buffer));
StringView line { buffer.data(), nread };
auto line = TRY(file.read_line(buffer));
if (line.is_empty() || line.starts_with('#'))
continue;
@ -547,8 +540,7 @@ static ErrorOr<void> parse_unicode_data(Core::Stream::BufferedFile& file, Unicod
Array<u8, 1024> buffer;
while (TRY(file.can_read_line())) {
auto nread = TRY(file.read_line(buffer));
StringView line { buffer.data(), nread };
auto line = TRY(file.read_line(buffer));
if (line.is_empty())
continue;

View file

@ -426,15 +426,15 @@ TEST_CASE(buffered_long_file_read)
auto buffer = ByteBuffer::create_uninitialized(4096).release_value();
EXPECT(!file->seek(255, Core::Stream::SeekMode::SetPosition).is_error());
EXPECT(file->can_read_line().release_value());
auto maybe_nread = file->read_line(buffer);
EXPECT(!maybe_nread.is_error());
EXPECT_EQ(maybe_nread.value(), 4095ul); // 4095 bytes on the third line
auto maybe_line = file->read_line(buffer);
EXPECT(!maybe_line.is_error());
EXPECT_EQ(maybe_line.value().length(), 4095ul); // 4095 bytes on the third line
// Testing that buffering with seeking works properly
EXPECT(!file->seek(365, Core::Stream::SeekMode::SetPosition).is_error());
auto maybe_after_seek_nread = file->read_line(buffer);
EXPECT(!maybe_after_seek_nread.is_error());
EXPECT_EQ(maybe_after_seek_nread.value(), 3985ul); // 4095 - 110
auto maybe_after_seek_line = file->read_line(buffer);
EXPECT(!maybe_after_seek_line.is_error());
EXPECT_EQ(maybe_after_seek_line.value().length(), 3985ul); // 4095 - 110
}
TEST_CASE(buffered_small_file_read)
@ -456,10 +456,10 @@ TEST_CASE(buffered_small_file_read)
auto buffer = ByteBuffer::create_uninitialized(4096).release_value();
for (auto const& line : expected_lines) {
VERIFY(file->can_read_line().release_value());
auto maybe_nread = file->read_line(buffer);
EXPECT(!maybe_nread.is_error());
EXPECT_EQ(maybe_nread.value(), line.length());
EXPECT_EQ(StringView(buffer.span().trim(maybe_nread.value())), line);
auto maybe_read_line = file->read_line(buffer);
EXPECT(!maybe_read_line.is_error());
EXPECT_EQ(maybe_read_line.value().length(), line.length());
EXPECT_EQ(StringView(buffer.span().trim(maybe_read_line.value().length())), line);
}
EXPECT(!file->can_read_line().is_error());
EXPECT(!file->can_read_line().value());
@ -496,13 +496,13 @@ TEST_CASE(buffered_tcp_socket_read)
auto receive_buffer = ByteBuffer::create_uninitialized(64).release_value();
auto maybe_first_nread = client_socket->read_line(receive_buffer);
EXPECT(!maybe_first_nread.is_error());
StringView first_received_line { receive_buffer.data(), maybe_first_nread.value() };
auto maybe_first_received_line = client_socket->read_line(receive_buffer);
EXPECT(!maybe_first_received_line.is_error());
auto first_received_line = maybe_first_received_line.value();
EXPECT_EQ(first_received_line, first_line);
auto maybe_second_nread = client_socket->read_line(receive_buffer);
EXPECT(!maybe_second_nread.is_error());
StringView second_received_line { receive_buffer.data(), maybe_second_nread.value() };
auto maybe_second_received_line = client_socket->read_line(receive_buffer);
EXPECT(!maybe_second_received_line.is_error());
auto second_received_line = maybe_second_received_line.value();
EXPECT_EQ(second_received_line, second_line);
}

View file

@ -43,8 +43,7 @@ static ErrorOr<void> load_content_filters()
auto ad_filter_list = TRY(Core::Stream::BufferedFile::create(move(file)));
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
while (TRY(ad_filter_list->can_read_line())) {
auto length = TRY(ad_filter_list->read_line(buffer));
StringView line { buffer.data(), length };
auto line = TRY(ad_filter_list->read_line(buffer));
if (!line.is_empty())
Browser::g_content_filters.append(line);
}

View file

@ -30,8 +30,7 @@ ErrorOr<void> DomainListModel::load()
auto content_filter_list = TRY(Core::Stream::BufferedFile::create(move(file)));
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
while (TRY(content_filter_list->can_read_line())) {
auto length = TRY(content_filter_list->read_line(buffer));
StringView line { buffer.data(), length };
auto line = TRY(content_filter_list->read_line(buffer));
dbgln("Content filter for {}", line);
if (!line.is_empty())
m_domain_list.append(line);

View file

@ -72,13 +72,13 @@ FileOperationProgressWidget::FileOperationProgressWidget(FileOperation operation
m_notifier = Core::Notifier::construct(helper_pipe_fd, Core::Notifier::Read);
m_notifier->on_ready_to_read = [this] {
auto line_buffer = ByteBuffer::create_zeroed(1 * KiB).release_value_but_fixme_should_propagate_errors();
auto line_length_or_error = m_helper_pipe->read_line(line_buffer.bytes());
if (line_length_or_error.is_error() || line_length_or_error.value() == 0) {
auto line_or_error = m_helper_pipe->read_line(line_buffer.bytes());
if (line_or_error.is_error() || line_or_error.value().is_empty()) {
did_error("Read from pipe returned null."sv);
return;
}
StringView line { line_buffer.bytes().data(), line_length_or_error.value() };
auto line = line_or_error.release_value();
auto parts = line.split_view(' ');
VERIFY(!parts.is_empty());

View file

@ -88,9 +88,7 @@ ErrorOr<void> ConfigFile::reparse()
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
while (TRY(m_file->can_read_line())) {
auto length = TRY(m_file->read_line(buffer));
StringView line { buffer.data(), length };
auto line = TRY(m_file->read_line(buffer));
size_t i = 0;
while (i < line.length() && (line[i] == ' ' || line[i] == '\t' || line[i] == '\n'))

View file

@ -557,10 +557,10 @@ public:
// Reads into the buffer until \n is encountered.
// The size of the Bytes object is the maximum amount of bytes that will be
// read. Returns the amount of bytes read.
ErrorOr<size_t> read_line(Bytes buffer)
// read. Returns the bytes read as a StringView.
ErrorOr<StringView> read_line(Bytes buffer)
{
return TRY(read_until(buffer, "\n"sv)).size();
return StringView { TRY(read_until(buffer, "\n"sv)) };
}
ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate)
@ -769,7 +769,7 @@ public:
return m_helper.stream().truncate(length);
}
ErrorOr<size_t> read_line(Bytes buffer) { return m_helper.read_line(move(buffer)); }
ErrorOr<StringView> read_line(Bytes buffer) { return m_helper.read_line(move(buffer)); }
ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate) { return m_helper.read_until(move(buffer), move(candidate)); }
template<size_t N>
ErrorOr<Bytes> read_until_any_of(Bytes buffer, Array<StringView, N> candidates) { return m_helper.read_until_any_of(move(buffer), move(candidates)); }
@ -790,7 +790,7 @@ private:
class BufferedSocketBase : public Socket {
public:
virtual ErrorOr<size_t> read_line(Bytes buffer) = 0;
virtual ErrorOr<StringView> read_line(Bytes buffer) = 0;
virtual ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate) = 0;
virtual ErrorOr<bool> can_read_line() = 0;
virtual size_t buffer_size() const = 0;
@ -838,7 +838,7 @@ public:
virtual ErrorOr<void> set_close_on_exec(bool enabled) override { return m_helper.stream().set_close_on_exec(enabled); }
virtual void set_notifications_enabled(bool enabled) override { m_helper.stream().set_notifications_enabled(enabled); }
virtual ErrorOr<size_t> read_line(Bytes buffer) override { return m_helper.read_line(move(buffer)); }
virtual ErrorOr<StringView> read_line(Bytes buffer) override { return m_helper.read_line(move(buffer)); }
virtual ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate) override { return m_helper.read_until(move(buffer), move(candidate)); }
template<size_t N>
ErrorOr<Bytes> read_until_any_of(Bytes buffer, Array<StringView, N> candidates) { return m_helper.read_until_any_of(move(buffer), move(candidates)); }

View file

@ -63,8 +63,8 @@ ErrorOr<ByteBuffer> WebSocketImpl::read(int max_size)
ErrorOr<String> WebSocketImpl::read_line(size_t size)
{
auto buffer = TRY(ByteBuffer::create_uninitialized(size));
auto nread = TRY(m_socket->read_line(buffer));
return String::copy(buffer.span().slice(0, nread));
auto line = TRY(m_socket->read_line(buffer));
return line.to_string();
}
}