From 3de02abf3f729da92e42cfc902fa5343a89f3efc Mon Sep 17 00:00:00 2001 From: Matthew Zipkin Date: Wed, 11 Mar 2026 10:55:35 -0400 Subject: [PATCH] util/test: Add string_view constructor to LineReader and remove StringToBuffer Co-Authored by: Hodlinator <172445034+hodlinator@users.noreply.github.com> --- src/test/util_string_tests.cpp | 24 +++++++++--------------- src/util/string.h | 1 + 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/test/util_string_tests.cpp b/src/test/util_string_tests.cpp index dea45f6fe4b..3835904d117 100644 --- a/src/test/util_string_tests.cpp +++ b/src/test/util_string_tests.cpp @@ -42,12 +42,6 @@ void FailFmtWithError(const char* wrong_fmt, std::string_view error) BOOST_CHECK_EXCEPTION(CheckNumFormatSpecifiers(wrong_fmt), const char*, HasReason{error}); } -std::vector StringToBuffer(const std::string& str) -{ - auto span = std::as_bytes(std::span(str)); - return {span.begin(), span.end()}; -} - BOOST_AUTO_TEST_CASE(ConstevalFormatString_NumSpec) { PassFmt<0>(""); @@ -181,7 +175,7 @@ BOOST_AUTO_TEST_CASE(line_reader_test) { { // Check three lines terminated by \n and \r\n, trimming whitespace - const std::vector input{StringToBuffer("once upon a time\n there was a dog \r\nwho liked food\n")}; + std::string_view input = "once upon a time\n there was a dog \r\nwho liked food\n"; LineReader reader(input, /*max_line_length=*/128); BOOST_CHECK_EQUAL(reader.Consumed(), 0); BOOST_CHECK_EQUAL(reader.Remaining(), 51); @@ -206,7 +200,7 @@ BOOST_AUTO_TEST_CASE(line_reader_test) { // Do not exceed max_line_length + 1 while searching for \n // Test with 22-character line + \n + 23-character line + \n - const std::vector input{StringToBuffer("once upon a time there\nwas a dog who liked tea\n")}; + std::string_view input = "once upon a time there\nwas a dog who liked tea\n"; LineReader reader1(input, /*max_line_length=*/22); // First line is exactly the length of max_line_length @@ -224,26 +218,26 @@ BOOST_AUTO_TEST_CASE(line_reader_test) } { // Empty lines are empty - const std::vector input{StringToBuffer("\n")}; + std::string_view input = "\n"; LineReader reader(input, /*max_line_length=*/1024); BOOST_CHECK_EQUAL(reader.ReadLine(), ""); BOOST_CHECK(!reader.ReadLine()); } { // Empty buffers are null - const std::vector input{StringToBuffer("")}; + std::string_view input; LineReader reader(input, /*max_line_length=*/1024); BOOST_CHECK(!reader.ReadLine()); } { // Even one character is too long, if it's not \n - const std::vector input{StringToBuffer("ab\n")}; + std::string_view input = "ab\n"; LineReader reader(input, /*max_line_length=*/1); // First line is +1 character too long BOOST_CHECK_EXCEPTION(reader.ReadLine(), std::runtime_error, HasReason{"max_line_length exceeded by LineReader"}); } { - const std::vector input{StringToBuffer("a\nb\n")}; + std::string_view input = "a\nb\n"; LineReader reader(input, /*max_line_length=*/1); BOOST_CHECK_EQUAL(reader.ReadLine(), "a"); BOOST_CHECK_EQUAL(reader.ReadLine(), "b"); @@ -251,7 +245,7 @@ BOOST_AUTO_TEST_CASE(line_reader_test) } { // If ReadLine fails, the iterator is reset and we can ReadLength instead - const std::vector input{StringToBuffer("a\nbaboon\n")}; + std::string_view input = "a\nbaboon\n"; LineReader reader(input, /*max_line_length=*/1); BOOST_CHECK_EQUAL(reader.ReadLine(), "a"); // "baboon" is too long @@ -267,7 +261,7 @@ BOOST_AUTO_TEST_CASE(line_reader_test) } { // The end of the buffer (EOB) does not count as end of line \n - const std::vector input{StringToBuffer("once upon a time there")}; + std::string_view input = "once upon a time there"; LineReader reader(input, /*max_line_length=*/22); // First line is exactly the length of max_line_length, but that doesn't matter because \n is missing @@ -279,7 +273,7 @@ BOOST_AUTO_TEST_CASE(line_reader_test) } { // Read specific number of bytes regardless of max_line_length or \n unless buffer is too short - const std::vector input{StringToBuffer("once upon a time\n there was a dog \r\nwho liked food")}; + std::string_view input = "once upon a time\n there was a dog \r\nwho liked food"; LineReader reader(input, /*max_line_length=*/1); BOOST_CHECK_EQUAL(reader.ReadLength(0), ""); BOOST_CHECK_EQUAL(reader.ReadLength(3), "onc"); diff --git a/src/util/string.h b/src/util/string.h index 64b49bb04a0..da4ce5a33ac 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -270,6 +270,7 @@ struct LineReader { std::span::iterator it; explicit LineReader(std::span buffer, size_t max_line_length); + explicit LineReader(std::string_view str, size_t max_line_length) : LineReader{std::as_bytes(std::span{str}), max_line_length} {} /** * Returns a string from current iterator position up to (but not including) next \n