diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-04-15 14:52:33 +0100 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2022-04-16 13:27:51 -0400 |
commit | d564cf1e8970275a52a1ef43debe4b1d14b2c063 (patch) | |
tree | dca303a1d92a9cfbe86912edb59fb902b3a74576 /Userland/Libraries | |
parent | c4134e97944538f34a5881954fb55431ea9da587 (diff) | |
download | serenity-d564cf1e8970275a52a1ef43debe4b1d14b2c063.zip |
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.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibCore/ConfigFile.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/Stream.h | 12 | ||||
-rw-r--r-- | Userland/Libraries/LibWebSocket/Impl/WebSocketImpl.cpp | 4 |
3 files changed, 9 insertions, 11 deletions
diff --git a/Userland/Libraries/LibCore/ConfigFile.cpp b/Userland/Libraries/LibCore/ConfigFile.cpp index bab469e747..b8474ca37f 100644 --- a/Userland/Libraries/LibCore/ConfigFile.cpp +++ b/Userland/Libraries/LibCore/ConfigFile.cpp @@ -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')) diff --git a/Userland/Libraries/LibCore/Stream.h b/Userland/Libraries/LibCore/Stream.h index 067cf238fc..71c3432f52 100644 --- a/Userland/Libraries/LibCore/Stream.h +++ b/Userland/Libraries/LibCore/Stream.h @@ -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)); } diff --git a/Userland/Libraries/LibWebSocket/Impl/WebSocketImpl.cpp b/Userland/Libraries/LibWebSocket/Impl/WebSocketImpl.cpp index 3486b56cb7..379bd0a4b4 100644 --- a/Userland/Libraries/LibWebSocket/Impl/WebSocketImpl.cpp +++ b/Userland/Libraries/LibWebSocket/Impl/WebSocketImpl.cpp @@ -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(); } } |