util/string: use string_view in LineReader

Co-authored-by: Hodlinator <172445034+hodlinator@users.noreply.github.com>
This commit is contained in:
Matthew Zipkin
2026-05-19 13:35:11 -04:00
parent 881d4b6c75
commit 0cdbb191b5
4 changed files with 13 additions and 11 deletions

View File

@@ -104,7 +104,7 @@ void HTTPResponseHeaders::Read(util::LineReader& reader)
// Headers https://httpwg.org/specs/rfc9110.html#rfc.section.6.3
// A sequence of Field Lines https://httpwg.org/specs/rfc9110.html#rfc.section.5.2
while (auto maybe_line = reader.ReadLine()) {
const std::string& line = *maybe_line;
const std::string_view line = *maybe_line;
// An empty line indicates end of the headers section https://www.rfc-editor.org/rfc/rfc2616#section-4
if (line.empty()) return;
@@ -965,7 +965,7 @@ HTTPResponse HTTPClient::ReadResponse()
throw HTTPError{"Failed to read status line"};
}
const std::string& status_str = *status_line;
const std::string_view status_str = *status_line;
// Minimum status line is "HTTP/X.Y NNN" (e.g. "HTTP/1.1 200"), 12 characters.
if (status_str.size() < 12 || !status_str.starts_with("HTTP/")) {
throw HTTPError{"Invalid status line"};
@@ -976,7 +976,7 @@ HTTPResponse HTTPClient::ReadResponse()
throw HTTPError{"Invalid status line format"};
}
std::string status_code_str = status_str.substr(space1 + 1, 3);
const std::string_view status_code_str = status_str.substr(space1 + 1, 3);
auto status_code = ToIntegral<int>(status_code_str);
if (!status_code) {
throw HTTPError{"Invalid status code"};

View File

@@ -191,7 +191,7 @@ bool TorControlConnection::ProcessBuffer()
// Parse: <code><separator><data>
// <status>(-|+| )<data>
m_message.code = ToIntegral<int>(line->substr(0, 3)).value_or(0);
m_message.lines.push_back(line->substr(4));
m_message.lines.emplace_back(line->substr(4));
char separator = (*line)[3]; // '-', '+', or ' '
if (separator == ' ') {

View File

@@ -20,7 +20,7 @@ void ReplaceAll(std::string& in_out, const std::string& search, const std::strin
LineReader::LineReader(std::span<const std::byte> buffer, size_t max_line_length)
: start(buffer.begin()), end(buffer.end()), max_line_length(max_line_length), it(buffer.begin()) {}
std::optional<std::string> LineReader::ReadLine()
std::optional<std::string_view> LineReader::ReadLine()
{
if (it == end) {
return std::nullopt;
@@ -38,7 +38,7 @@ std::optional<std::string> LineReader::ReadLine()
if (c == '\n') {
const std::string_view untrimmed_line(reinterpret_cast<const char*>(std::to_address(line_start)), count);
const std::string_view line = TrimStringView(untrimmed_line); // delete leading and trailing whitespace including \r and \n
return std::string(line);
return line;
}
// If the character we just consumed gives us a line length greater
// than max_line_length, and we are not at the end of the line (or buffer) yet,
@@ -57,11 +57,11 @@ std::optional<std::string> LineReader::ReadLine()
}
// Ignores max_line_length but won't overflow
std::string LineReader::ReadLength(size_t len)
std::string_view LineReader::ReadLength(size_t len)
{
if (len == 0) return "";
if (len == 0) return {};
if (Remaining() < len) throw std::runtime_error("Not enough data in buffer");
std::string out(reinterpret_cast<const char*>(std::to_address(it)), len);
std::string_view out(reinterpret_cast<const char*>(std::to_address(it)), len);
it += len;
return out;
}

View File

@@ -18,6 +18,8 @@
#include <string_view>
#include <vector>
#include <attributes.h>
namespace util {
namespace detail {
template <unsigned num_params>
@@ -280,7 +282,7 @@ struct LineReader {
* std::nullopt if end of buffer is reached without finding a \n.
* @throws a std::runtime_error if max_line_length + 1 bytes are read without finding \n.
*/
std::optional<std::string> ReadLine();
std::optional<std::string_view> ReadLine() LIFETIMEBOUND;
/**
* Returns string from current iterator position of specified length
@@ -290,7 +292,7 @@ struct LineReader {
* @returns a string of the expected length.
* @throws a std::runtime_error if there is not enough data in the buffer.
*/
std::string ReadLength(size_t len);
std::string_view ReadLength(size_t len) LIFETIMEBOUND;
/**
* Returns remaining size of bytes in buffer