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

View file

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

View file

@ -426,15 +426,15 @@ TEST_CASE(buffered_long_file_read)
auto buffer = ByteBuffer::create_uninitialized(4096).release_value(); auto buffer = ByteBuffer::create_uninitialized(4096).release_value();
EXPECT(!file->seek(255, Core::Stream::SeekMode::SetPosition).is_error()); EXPECT(!file->seek(255, Core::Stream::SeekMode::SetPosition).is_error());
EXPECT(file->can_read_line().release_value()); EXPECT(file->can_read_line().release_value());
auto maybe_nread = file->read_line(buffer); auto maybe_line = file->read_line(buffer);
EXPECT(!maybe_nread.is_error()); EXPECT(!maybe_line.is_error());
EXPECT_EQ(maybe_nread.value(), 4095ul); // 4095 bytes on the third line EXPECT_EQ(maybe_line.value().length(), 4095ul); // 4095 bytes on the third line
// Testing that buffering with seeking works properly // Testing that buffering with seeking works properly
EXPECT(!file->seek(365, Core::Stream::SeekMode::SetPosition).is_error()); EXPECT(!file->seek(365, Core::Stream::SeekMode::SetPosition).is_error());
auto maybe_after_seek_nread = file->read_line(buffer); auto maybe_after_seek_line = file->read_line(buffer);
EXPECT(!maybe_after_seek_nread.is_error()); EXPECT(!maybe_after_seek_line.is_error());
EXPECT_EQ(maybe_after_seek_nread.value(), 3985ul); // 4095 - 110 EXPECT_EQ(maybe_after_seek_line.value().length(), 3985ul); // 4095 - 110
} }
TEST_CASE(buffered_small_file_read) TEST_CASE(buffered_small_file_read)
@ -456,10 +456,10 @@ TEST_CASE(buffered_small_file_read)
auto buffer = ByteBuffer::create_uninitialized(4096).release_value(); auto buffer = ByteBuffer::create_uninitialized(4096).release_value();
for (auto const& line : expected_lines) { for (auto const& line : expected_lines) {
VERIFY(file->can_read_line().release_value()); VERIFY(file->can_read_line().release_value());
auto maybe_nread = file->read_line(buffer); auto maybe_read_line = file->read_line(buffer);
EXPECT(!maybe_nread.is_error()); EXPECT(!maybe_read_line.is_error());
EXPECT_EQ(maybe_nread.value(), line.length()); EXPECT_EQ(maybe_read_line.value().length(), line.length());
EXPECT_EQ(StringView(buffer.span().trim(maybe_nread.value())), line); EXPECT_EQ(StringView(buffer.span().trim(maybe_read_line.value().length())), line);
} }
EXPECT(!file->can_read_line().is_error()); EXPECT(!file->can_read_line().is_error());
EXPECT(!file->can_read_line().value()); 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 receive_buffer = ByteBuffer::create_uninitialized(64).release_value();
auto maybe_first_nread = client_socket->read_line(receive_buffer); auto maybe_first_received_line = client_socket->read_line(receive_buffer);
EXPECT(!maybe_first_nread.is_error()); EXPECT(!maybe_first_received_line.is_error());
StringView first_received_line { receive_buffer.data(), maybe_first_nread.value() }; auto first_received_line = maybe_first_received_line.value();
EXPECT_EQ(first_received_line, first_line); EXPECT_EQ(first_received_line, first_line);
auto maybe_second_nread = client_socket->read_line(receive_buffer); auto maybe_second_received_line = client_socket->read_line(receive_buffer);
EXPECT(!maybe_second_nread.is_error()); EXPECT(!maybe_second_received_line.is_error());
StringView second_received_line { receive_buffer.data(), maybe_second_nread.value() }; auto second_received_line = maybe_second_received_line.value();
EXPECT_EQ(second_received_line, second_line); 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 ad_filter_list = TRY(Core::Stream::BufferedFile::create(move(file)));
auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
while (TRY(ad_filter_list->can_read_line())) { while (TRY(ad_filter_list->can_read_line())) {
auto length = TRY(ad_filter_list->read_line(buffer)); auto line = TRY(ad_filter_list->read_line(buffer));
StringView line { buffer.data(), length };
if (!line.is_empty()) if (!line.is_empty())
Browser::g_content_filters.append(line); 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 content_filter_list = TRY(Core::Stream::BufferedFile::create(move(file)));
auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
while (TRY(content_filter_list->can_read_line())) { while (TRY(content_filter_list->can_read_line())) {
auto length = TRY(content_filter_list->read_line(buffer)); auto line = TRY(content_filter_list->read_line(buffer));
StringView line { buffer.data(), length };
dbgln("Content filter for {}", line); dbgln("Content filter for {}", line);
if (!line.is_empty()) if (!line.is_empty())
m_domain_list.append(line); 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 = Core::Notifier::construct(helper_pipe_fd, Core::Notifier::Read);
m_notifier->on_ready_to_read = [this] { m_notifier->on_ready_to_read = [this] {
auto line_buffer = ByteBuffer::create_zeroed(1 * KiB).release_value_but_fixme_should_propagate_errors(); 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()); auto line_or_error = m_helper_pipe->read_line(line_buffer.bytes());
if (line_length_or_error.is_error() || line_length_or_error.value() == 0) { if (line_or_error.is_error() || line_or_error.value().is_empty()) {
did_error("Read from pipe returned null."sv); did_error("Read from pipe returned null."sv);
return; return;
} }
StringView line { line_buffer.bytes().data(), line_length_or_error.value() }; auto line = line_or_error.release_value();
auto parts = line.split_view(' '); auto parts = line.split_view(' ');
VERIFY(!parts.is_empty()); VERIFY(!parts.is_empty());

View file

@ -88,9 +88,7 @@ ErrorOr<void> ConfigFile::reparse()
auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
while (TRY(m_file->can_read_line())) { while (TRY(m_file->can_read_line())) {
auto length = TRY(m_file->read_line(buffer)); auto line = TRY(m_file->read_line(buffer));
StringView line { buffer.data(), length };
size_t i = 0; size_t i = 0;
while (i < line.length() && (line[i] == ' ' || line[i] == '\t' || line[i] == '\n')) 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. // Reads into the buffer until \n is encountered.
// The size of the Bytes object is the maximum amount of bytes that will be // The size of the Bytes object is the maximum amount of bytes that will be
// read. Returns the amount of bytes read. // read. Returns the bytes read as a StringView.
ErrorOr<size_t> read_line(Bytes buffer) 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) ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate)
@ -769,7 +769,7 @@ public:
return m_helper.stream().truncate(length); 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)); } ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate) { return m_helper.read_until(move(buffer), move(candidate)); }
template<size_t N> 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)); } 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 { class BufferedSocketBase : public Socket {
public: 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<Bytes> read_until(Bytes buffer, StringView candidate) = 0;
virtual ErrorOr<bool> can_read_line() = 0; virtual ErrorOr<bool> can_read_line() = 0;
virtual size_t buffer_size() const = 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 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 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)); } virtual ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate) override { return m_helper.read_until(move(buffer), move(candidate)); }
template<size_t N> 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)); } 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) ErrorOr<String> WebSocketImpl::read_line(size_t size)
{ {
auto buffer = TRY(ByteBuffer::create_uninitialized(size)); auto buffer = TRY(ByteBuffer::create_uninitialized(size));
auto nread = TRY(m_socket->read_line(buffer)); auto line = TRY(m_socket->read_line(buffer));
return String::copy(buffer.span().slice(0, nread)); return line.to_string();
} }
} }