LibCore: Make IODevice::read_line() return a String

Almost everyone using this API actually wanted String instead of a
ByteBuffer anyway, and there were a bunch of slightly different ways
clients would convert to String.

Let's just cut out all the confusion and make it return String. :^)
This commit is contained in:
Andreas Kling 2020-12-13 11:44:53 +01:00
parent 4da327d650
commit b9b7b2b28a
Notes: sideshowbarker 2024-07-19 01:59:31 +09:00
22 changed files with 50 additions and 66 deletions

View file

@ -131,7 +131,7 @@ bool IRCClient::connect()
void IRCClient::receive_from_server()
{
while (m_socket->can_read_line()) {
auto line = m_socket->read_line(PAGE_SIZE);
auto line = m_socket->read_line();
if (line.is_null()) {
if (!m_socket->is_connected()) {
outln("IRCClient: Connection closed!");
@ -139,11 +139,11 @@ void IRCClient::receive_from_server()
}
ASSERT_NOT_REACHED();
}
process_line(move(line));
process_line(line);
}
}
void IRCClient::process_line(ByteBuffer&& line)
void IRCClient::process_line(const String& line)
{
Message msg;
Vector<char, 32> prefix;
@ -159,7 +159,7 @@ void IRCClient::process_line(ByteBuffer&& line)
} state
= Start;
for (size_t i = 0; i < line.size(); ++i) {
for (size_t i = 0; i < line.length(); ++i) {
char ch = line[i];
if (ch == '\r')
continue;

View file

@ -170,7 +170,7 @@ private:
void send_kick(const String& channel_name, const String& nick, const String&);
void send_list();
void send_whois(const String&);
void process_line(ByteBuffer&&);
void process_line(const String&);
void handle_join(const Message&);
void handle_part(const Message&);
void handle_quit(const Message&);

View file

@ -64,10 +64,13 @@ static Optional<Vector<ContentPage>> parse_welcome_file(const String& path)
bool started = false;
ContentPage current;
while (file->can_read_line()) {
auto buffer = file->read_line(4096);
auto line = String((const char*)buffer.data(), buffer.size());
if (line.length() > 1)
line = line.substring(0, line.length() - 1); // remove newline
auto line = file->read_line();
if (line.is_empty()) {
if (!current_output_line.to_string().is_empty())
current.content.append(current_output_line.to_string());
current_output_line.clear();
continue;
}
switch (line[0]) {
case '*':
dbgln("menu_item line:\t{}", line);
@ -87,13 +90,6 @@ static Optional<Vector<ContentPage>> parse_welcome_file(const String& path)
dbgln("title line:\t{}", line);
current.title = line.substring(2, line.length() - 2);
break;
case '\n':
dbgln("newline");
if (!current_output_line.to_string().is_empty())
current.content.append(current_output_line.to_string());
current_output_line.clear();
break;
case '#':
dbgln("comment line:\t{}", line);
break;

View file

@ -83,8 +83,8 @@ void ConfigFile::reparse()
HashMap<String, String>* current_group = nullptr;
while (file->can_read_line()) {
auto line = file->read_line(BUFSIZ);
auto* cp = (const char*)line.data();
auto line = file->read_line();
auto* cp = line.characters();
while (*cp && (*cp == ' ' || *cp == '\t' || *cp == '\n'))
++cp;

View file

@ -174,7 +174,7 @@ ByteBuffer IODevice::read_all()
return ByteBuffer::copy(data.data(), data.size());
}
ByteBuffer IODevice::read_line(size_t max_size)
String IODevice::read_line(size_t max_size)
{
if (m_fd < 0)
return {};
@ -187,9 +187,9 @@ ByteBuffer IODevice::read_line(size_t max_size)
dbgprintf("IODevice::read_line: At EOF but there's more than max_size(%zu) buffered\n", max_size);
return {};
}
auto buffer = ByteBuffer::copy(m_buffered_data.data(), m_buffered_data.size());
auto line = String((const char*)m_buffered_data.data(), m_buffered_data.size(), Chomp);
m_buffered_data.clear();
return buffer;
return line;
}
auto line = ByteBuffer::create_uninitialized(max_size + 1);
size_t line_index = 0;
@ -201,7 +201,7 @@ ByteBuffer IODevice::read_line(size_t max_size)
new_buffered_data.append(m_buffered_data.data() + line_index, m_buffered_data.size() - line_index);
m_buffered_data = move(new_buffered_data);
line.trim(line_index);
return line;
return String::copy(line, Chomp);
}
}
return {};

View file

@ -59,8 +59,8 @@ public:
int read(u8* buffer, int length);
ByteBuffer read(size_t max_size);
ByteBuffer read_line(size_t max_size);
ByteBuffer read_all();
String read_line(size_t max_size = 16384);
bool write(const u8*, int size);
bool write(const StringView&);

View file

@ -124,7 +124,7 @@ bool GeminiJob::can_read_line() const
return m_socket->can_read_line();
}
ByteBuffer GeminiJob::read_line(size_t size)
String GeminiJob::read_line(size_t size)
{
return m_socket->read_line(size);
}

View file

@ -57,7 +57,7 @@ protected:
virtual void register_on_ready_to_read(Function<void()>) override;
virtual void register_on_ready_to_write(Function<void()>) override;
virtual bool can_read_line() const override;
virtual ByteBuffer read_line(size_t) override;
virtual String read_line(size_t) override;
virtual bool can_read() const override;
virtual ByteBuffer receive(size_t) override;
virtual bool eof() const override;

View file

@ -71,9 +71,9 @@ void Job::on_socket_connected()
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::TransmissionFailed); });
}
auto parts = String::copy(line, Chomp).split_limit(' ', 2);
auto parts = line.split_limit(' ', 2);
if (parts.size() != 2) {
fprintf(stderr, "Job: Expected 2-part status line, got '%s'\n", line.data());
warnln("Job: Expected 2-part status line, got '{}'", line);
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
}

View file

@ -51,7 +51,7 @@ protected:
virtual void register_on_ready_to_read(Function<void()>) = 0;
virtual void register_on_ready_to_write(Function<void()>) = 0;
virtual bool can_read_line() const = 0;
virtual ByteBuffer read_line(size_t) = 0;
virtual String read_line(size_t) = 0;
virtual bool can_read() const = 0;
virtual ByteBuffer receive(size_t) = 0;
virtual bool eof() const = 0;

View file

@ -78,7 +78,7 @@ bool HttpJob::can_read_line() const
return m_socket->can_read_line();
}
ByteBuffer HttpJob::read_line(size_t size)
String HttpJob::read_line(size_t size)
{
return m_socket->read_line(size);
}

View file

@ -55,7 +55,7 @@ protected:
virtual void register_on_ready_to_read(Function<void()>) override;
virtual void register_on_ready_to_write(Function<void()>) override;
virtual bool can_read_line() const override;
virtual ByteBuffer read_line(size_t) override;
virtual String read_line(size_t) override;
virtual bool can_read() const override;
virtual ByteBuffer receive(size_t) override;
virtual bool eof() const override;

View file

@ -126,7 +126,7 @@ bool HttpsJob::can_read_line() const
return m_socket->can_read_line();
}
ByteBuffer HttpsJob::read_line(size_t size)
String HttpsJob::read_line(size_t size)
{
return m_socket->read_line(size);
}

View file

@ -58,7 +58,7 @@ protected:
virtual void register_on_ready_to_read(Function<void()>) override;
virtual void register_on_ready_to_write(Function<void()>) override;
virtual bool can_read_line() const override;
virtual ByteBuffer read_line(size_t) override;
virtual String read_line(size_t) override;
virtual bool can_read() const override;
virtual ByteBuffer receive(size_t) override;
virtual bool eof() const override;

View file

@ -103,9 +103,9 @@ void Job::on_socket_connected()
fprintf(stderr, "Job: Expected HTTP status\n");
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::TransmissionFailed); });
}
auto parts = String::copy(line, Chomp).split(' ');
auto parts = line.split_view(' ');
if (parts.size() < 3) {
fprintf(stderr, "Job: Expected 3-part HTTP status, got '%s'\n", line.data());
warnln("Job: Expected 3-part HTTP status, got '{}'", line);
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
}
auto code = parts[1].to_uint();
@ -131,8 +131,7 @@ void Job::on_socket_connected()
fprintf(stderr, "Job: Expected HTTP header\n");
return did_fail(Core::NetworkJob::Error::ProtocolFailed);
}
auto chomped_line = String::copy(line, Chomp);
if (chomped_line.is_empty()) {
if (line.is_empty()) {
if (m_state == State::Trailers) {
return finish_up();
} else {
@ -140,7 +139,7 @@ void Job::on_socket_connected()
}
return;
}
auto parts = chomped_line.split(':');
auto parts = line.split_view(':');
if (parts.is_empty()) {
if (m_state == State::Trailers) {
// Some servers like to send two ending chunks
@ -152,17 +151,17 @@ void Job::on_socket_connected()
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
}
auto name = parts[0];
if (chomped_line.length() < name.length() + 2) {
if (line.length() < name.length() + 2) {
if (m_state == State::Trailers) {
// Some servers like to send two ending chunks
// use this fact as an excuse to ignore anything after the last chunk
// that is not a valid trailing header.
return finish_up();
}
fprintf(stderr, "Job: Malformed HTTP header: '%s' (%zu)\n", chomped_line.characters(), chomped_line.length());
warnln("Job: Malformed HTTP header: '{}' ({})", line, line.length());
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
}
auto value = chomped_line.substring(name.length() + 2, chomped_line.length() - name.length() - 2);
auto value = line.substring(name.length() + 2, line.length() - name.length() - 2);
m_headers.set(name, value);
#ifdef JOB_DEBUG
dbg() << "Job: [" << name << "] = '" << value << "'";
@ -180,7 +179,7 @@ void Job::on_socket_connected()
if (remaining == -1) {
// read size
auto size_data = read_line(PAGE_SIZE);
auto size_lines = StringView { size_data.data(), size_data.size() }.lines();
auto size_lines = size_data.view().lines();
#ifdef JOB_DEBUG
dbg() << "Job: Received a chunk with size _" << size_data << "_";
#endif

View file

@ -52,7 +52,7 @@ protected:
virtual void register_on_ready_to_read(Function<void()>) = 0;
virtual void register_on_ready_to_write(Function<void()>) = 0;
virtual bool can_read_line() const = 0;
virtual ByteBuffer read_line(size_t) = 0;
virtual String read_line(size_t) = 0;
virtual bool can_read() const = 0;
virtual ByteBuffer receive(size_t) = 0;
virtual bool eof() const = 0;

View file

@ -226,9 +226,7 @@ bool Editor::load_history(const String& path)
if (!history_file->open(Core::IODevice::ReadOnly))
return false;
while (history_file->can_read_line()) {
auto buffer = history_file->read_line(1024);
// -1 to skip the newline character
add_to_history(String(reinterpret_cast<const char*>(buffer.data()), buffer.size() - 1));
add_to_history(history_file->read_line());
}
return true;
}

View file

@ -53,7 +53,7 @@ ByteBuffer TLSv12::read(size_t max_size)
return {};
}
ByteBuffer TLSv12::read_line(size_t max_size)
String TLSv12::read_line(size_t max_size)
{
if (!can_read_line())
return {};
@ -70,7 +70,7 @@ ByteBuffer TLSv12::read_line(size_t max_size)
auto buffer = ByteBuffer::copy(start, offset);
m_context.application_buffer = m_context.application_buffer.slice(offset + 1, m_context.application_buffer.size() - offset - 1);
return buffer;
return String::copy(buffer);
}
bool TLSv12::write(const ByteBuffer& buffer)

View file

@ -318,7 +318,7 @@ public:
bool can_read_line() const { return m_context.application_buffer.size() && memchr(m_context.application_buffer.data(), '\n', m_context.application_buffer.size()); }
bool can_read() const { return m_context.application_buffer.size() > 0; }
ByteBuffer read_line(size_t max_size);
String read_line(size_t max_size);
Function<void(TLSv12&)> on_tls_ready_to_read;
Function<void(TLSv12&)> on_tls_ready_to_write;

View file

@ -72,8 +72,7 @@ void LookupServer::load_etc_hosts()
auto line = file->read_line(1024);
if (line.is_empty())
break;
auto str_line = String((const char*)line.data(), line.size() - 1, Chomp);
auto fields = str_line.split('\t');
auto fields = line.split('\t');
auto sections = fields[0].split('.');
IPv4Address addr {

View file

@ -146,7 +146,7 @@ int main(int argc, char** argv)
match.view.to_string());
last_printed_char_pos = match.global_offset + match.view.length();
}
out("{}", StringView(&str[last_printed_char_pos], str.length() - last_printed_char_pos));
outln("{}", StringView(&str[last_printed_char_pos], str.length() - last_printed_char_pos));
}
return true;
@ -163,11 +163,10 @@ int main(int argc, char** argv)
}
while (file->can_read_line()) {
auto line = file->read_line(1024);
auto is_binary = memchr(line.data(), 0, line.size()) != nullptr;
auto line = file->read_line();
auto is_binary = memchr(line.characters(), 0, line.length()) != nullptr;
StringView str { reinterpret_cast<const char*>(line.data()), line.size() };
if (matches(str, filename, print_filename, is_binary) && is_binary && binary_mode == BinaryFileMode::Binary)
if (matches(line, filename, print_filename, is_binary) && is_binary && binary_mode == BinaryFileMode::Binary)
return true;
}
return true;
@ -189,14 +188,13 @@ int main(int argc, char** argv)
if (!files.size() && !recursive) {
auto stdin_file = Core::File::stdin();
for (;;) {
auto line = stdin_file->read_line(4096);
StringView str { line.data(), line.size() };
bool is_binary = str.bytes().contains_slow(0);
auto line = stdin_file->read_line();
bool is_binary = line.bytes().contains_slow(0);
if (is_binary && binary_mode == BinaryFileMode::Skip)
return 1;
if (matches(str, "stdin", false, is_binary) && is_binary && binary_mode == BinaryFileMode::Binary)
if (matches(line, "stdin", false, is_binary) && is_binary && binary_mode == BinaryFileMode::Binary)
return 0;
}
} else {

View file

@ -95,13 +95,7 @@ static bool mount_all()
bool all_ok = true;
while (fstab->can_read_line()) {
ByteBuffer buffer = fstab->read_line(1024);
StringView line_view { buffer.data(), buffer.size() };
// Trim the trailing newline, if any.
if (line_view.length() > 0 && line_view[line_view.length() - 1] == '\n')
line_view = line_view.substring_view(0, line_view.length() - 1);
String line = line_view;
auto line = fstab->read_line();
// Skip comments and blank lines.
if (line.is_empty() || line.starts_with("#"))