diff --git a/Applications/IRCClient/IRCClient.cpp b/Applications/IRCClient/IRCClient.cpp index b31aff409fc..49380b68fb6 100644 --- a/Applications/IRCClient/IRCClient.cpp +++ b/Applications/IRCClient/IRCClient.cpp @@ -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 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; diff --git a/Applications/IRCClient/IRCClient.h b/Applications/IRCClient/IRCClient.h index 663d8bace67..06f7e59289f 100644 --- a/Applications/IRCClient/IRCClient.h +++ b/Applications/IRCClient/IRCClient.h @@ -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&); diff --git a/Applications/Welcome/main.cpp b/Applications/Welcome/main.cpp index 9b120e94d54..3f991842628 100644 --- a/Applications/Welcome/main.cpp +++ b/Applications/Welcome/main.cpp @@ -64,10 +64,13 @@ static Optional> 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> 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; diff --git a/Libraries/LibCore/ConfigFile.cpp b/Libraries/LibCore/ConfigFile.cpp index f4e1536436d..cb7e68576c4 100644 --- a/Libraries/LibCore/ConfigFile.cpp +++ b/Libraries/LibCore/ConfigFile.cpp @@ -83,8 +83,8 @@ void ConfigFile::reparse() HashMap* 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; diff --git a/Libraries/LibCore/IODevice.cpp b/Libraries/LibCore/IODevice.cpp index 81d759afd0c..4efda65ee40 100644 --- a/Libraries/LibCore/IODevice.cpp +++ b/Libraries/LibCore/IODevice.cpp @@ -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 {}; diff --git a/Libraries/LibCore/IODevice.h b/Libraries/LibCore/IODevice.h index 2fe1e93e65f..6071a4c9126 100644 --- a/Libraries/LibCore/IODevice.h +++ b/Libraries/LibCore/IODevice.h @@ -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&); diff --git a/Libraries/LibGemini/GeminiJob.cpp b/Libraries/LibGemini/GeminiJob.cpp index 68464cc1026..fdcc9a3d3ea 100644 --- a/Libraries/LibGemini/GeminiJob.cpp +++ b/Libraries/LibGemini/GeminiJob.cpp @@ -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); } diff --git a/Libraries/LibGemini/GeminiJob.h b/Libraries/LibGemini/GeminiJob.h index 6d14371afc4..a48a8fa7439 100644 --- a/Libraries/LibGemini/GeminiJob.h +++ b/Libraries/LibGemini/GeminiJob.h @@ -57,7 +57,7 @@ protected: virtual void register_on_ready_to_read(Function) override; virtual void register_on_ready_to_write(Function) 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; diff --git a/Libraries/LibGemini/Job.cpp b/Libraries/LibGemini/Job.cpp index 37753eb920e..6ff386d5f4b 100644 --- a/Libraries/LibGemini/Job.cpp +++ b/Libraries/LibGemini/Job.cpp @@ -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); }); } diff --git a/Libraries/LibGemini/Job.h b/Libraries/LibGemini/Job.h index e308166ffd9..8101b3ef663 100644 --- a/Libraries/LibGemini/Job.h +++ b/Libraries/LibGemini/Job.h @@ -51,7 +51,7 @@ protected: virtual void register_on_ready_to_read(Function) = 0; virtual void register_on_ready_to_write(Function) = 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; diff --git a/Libraries/LibHTTP/HttpJob.cpp b/Libraries/LibHTTP/HttpJob.cpp index 1ebc5a8d45a..78ba90b7432 100644 --- a/Libraries/LibHTTP/HttpJob.cpp +++ b/Libraries/LibHTTP/HttpJob.cpp @@ -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); } diff --git a/Libraries/LibHTTP/HttpJob.h b/Libraries/LibHTTP/HttpJob.h index 4cd306c102e..98dae2b3075 100644 --- a/Libraries/LibHTTP/HttpJob.h +++ b/Libraries/LibHTTP/HttpJob.h @@ -55,7 +55,7 @@ protected: virtual void register_on_ready_to_read(Function) override; virtual void register_on_ready_to_write(Function) 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; diff --git a/Libraries/LibHTTP/HttpsJob.cpp b/Libraries/LibHTTP/HttpsJob.cpp index 87e4b086e9f..b424764d220 100644 --- a/Libraries/LibHTTP/HttpsJob.cpp +++ b/Libraries/LibHTTP/HttpsJob.cpp @@ -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); } diff --git a/Libraries/LibHTTP/HttpsJob.h b/Libraries/LibHTTP/HttpsJob.h index e78e1b87467..391df6198f4 100644 --- a/Libraries/LibHTTP/HttpsJob.h +++ b/Libraries/LibHTTP/HttpsJob.h @@ -58,7 +58,7 @@ protected: virtual void register_on_ready_to_read(Function) override; virtual void register_on_ready_to_write(Function) 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; diff --git a/Libraries/LibHTTP/Job.cpp b/Libraries/LibHTTP/Job.cpp index a3bfb32d78b..5f422e002df 100644 --- a/Libraries/LibHTTP/Job.cpp +++ b/Libraries/LibHTTP/Job.cpp @@ -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 diff --git a/Libraries/LibHTTP/Job.h b/Libraries/LibHTTP/Job.h index ae9c3e2a36d..fd410f892fc 100644 --- a/Libraries/LibHTTP/Job.h +++ b/Libraries/LibHTTP/Job.h @@ -52,7 +52,7 @@ protected: virtual void register_on_ready_to_read(Function) = 0; virtual void register_on_ready_to_write(Function) = 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; diff --git a/Libraries/LibLine/Editor.cpp b/Libraries/LibLine/Editor.cpp index c75db47dcdb..5c302bfaf75 100644 --- a/Libraries/LibLine/Editor.cpp +++ b/Libraries/LibLine/Editor.cpp @@ -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(buffer.data()), buffer.size() - 1)); + add_to_history(history_file->read_line()); } return true; } diff --git a/Libraries/LibTLS/Socket.cpp b/Libraries/LibTLS/Socket.cpp index da37e7f5f79..a8ff83197a6 100644 --- a/Libraries/LibTLS/Socket.cpp +++ b/Libraries/LibTLS/Socket.cpp @@ -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) diff --git a/Libraries/LibTLS/TLSv12.h b/Libraries/LibTLS/TLSv12.h index 0ffd44ca548..2af5383b408 100644 --- a/Libraries/LibTLS/TLSv12.h +++ b/Libraries/LibTLS/TLSv12.h @@ -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 on_tls_ready_to_read; Function on_tls_ready_to_write; diff --git a/Services/LookupServer/LookupServer.cpp b/Services/LookupServer/LookupServer.cpp index 98edf066b5b..16761ec7e43 100644 --- a/Services/LookupServer/LookupServer.cpp +++ b/Services/LookupServer/LookupServer.cpp @@ -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 { diff --git a/Userland/grep.cpp b/Userland/grep.cpp index 37ebcf96ba3..faf3342f228 100644 --- a/Userland/grep.cpp +++ b/Userland/grep.cpp @@ -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(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 { diff --git a/Userland/mount.cpp b/Userland/mount.cpp index 6a077c5d43d..6c98e496d59 100644 --- a/Userland/mount.cpp +++ b/Userland/mount.cpp @@ -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("#"))