-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add buffered line reads to BufferedSource #14845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
xokdvium
merged 1 commit into
NixOS:master
from
Zaczero:zaczero/BufferedSource--readLine
Dec 27, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "nix/util/file-descriptor.hh" | ||
| #include "nix/util/serialise.hh" | ||
|
|
||
| #include <cstring> | ||
|
|
||
| namespace nix { | ||
|
|
||
| // BufferedSource with configurable small buffer for precise boundary testing. | ||
| struct TestBufferedStringSource : BufferedSource | ||
| { | ||
| std::string_view data; | ||
| size_t pos = 0; | ||
|
|
||
| TestBufferedStringSource(std::string_view d, size_t bufSize) | ||
| : BufferedSource(bufSize) | ||
| , data(d) | ||
| { | ||
| } | ||
|
|
||
| protected: | ||
| size_t readUnbuffered(char * buf, size_t len) override | ||
| { | ||
| if (pos >= data.size()) | ||
| throw EndOfFile("end of test data"); | ||
| size_t n = std::min(len, data.size() - pos); | ||
| std::memcpy(buf, data.data() + pos, n); | ||
| pos += n; | ||
| return n; | ||
| } | ||
| }; | ||
|
|
||
| TEST(ReadLine, ReadsLinesFromPipe) | ||
| { | ||
| Pipe pipe; | ||
| pipe.create(); | ||
|
|
||
| writeFull(pipe.writeSide.get(), "hello\nworld\n", /*allowInterrupts=*/false); | ||
| pipe.writeSide.close(); | ||
|
|
||
| EXPECT_EQ(readLine(pipe.readSide.get()), "hello"); | ||
| EXPECT_EQ(readLine(pipe.readSide.get()), "world"); | ||
| EXPECT_EQ(readLine(pipe.readSide.get(), /*eofOk=*/true), ""); | ||
| } | ||
|
|
||
| TEST(ReadLine, ReturnsPartialLineOnEofWhenAllowed) | ||
| { | ||
| Pipe pipe; | ||
| pipe.create(); | ||
|
|
||
| writeFull(pipe.writeSide.get(), "partial", /*allowInterrupts=*/false); | ||
| pipe.writeSide.close(); | ||
|
|
||
| EXPECT_EQ(readLine(pipe.readSide.get(), /*eofOk=*/true), "partial"); | ||
| EXPECT_EQ(readLine(pipe.readSide.get(), /*eofOk=*/true), ""); | ||
| } | ||
|
|
||
| TEST(ReadLine, ThrowsOnEofWhenNotAllowed) | ||
| { | ||
| Pipe pipe; | ||
| pipe.create(); | ||
| pipe.writeSide.close(); | ||
|
|
||
| EXPECT_THROW(readLine(pipe.readSide.get()), EndOfFile); | ||
| } | ||
|
|
||
| TEST(ReadLine, EmptyLine) | ||
| { | ||
| Pipe pipe; | ||
| pipe.create(); | ||
|
|
||
| writeFull(pipe.writeSide.get(), "\n", /*allowInterrupts=*/false); | ||
| pipe.writeSide.close(); | ||
|
|
||
| EXPECT_EQ(readLine(pipe.readSide.get()), ""); | ||
| } | ||
|
|
||
| TEST(ReadLine, ConsecutiveEmptyLines) | ||
| { | ||
| Pipe pipe; | ||
| pipe.create(); | ||
|
|
||
| writeFull(pipe.writeSide.get(), "\n\n\n", /*allowInterrupts=*/false); | ||
| pipe.writeSide.close(); | ||
|
|
||
| EXPECT_EQ(readLine(pipe.readSide.get()), ""); | ||
| EXPECT_EQ(readLine(pipe.readSide.get()), ""); | ||
| EXPECT_EQ(readLine(pipe.readSide.get()), ""); | ||
| EXPECT_EQ(readLine(pipe.readSide.get(), /*eofOk=*/true), ""); | ||
| } | ||
|
|
||
| TEST(ReadLine, LineWithNullBytes) | ||
| { | ||
| Pipe pipe; | ||
| pipe.create(); | ||
|
|
||
| std::string data( | ||
| "a\x00" | ||
| "b\n", | ||
| 4); | ||
| writeFull(pipe.writeSide.get(), data, /*allowInterrupts=*/false); | ||
| pipe.writeSide.close(); | ||
|
|
||
| auto line = readLine(pipe.readSide.get()); | ||
| EXPECT_EQ(line.size(), 3); | ||
| EXPECT_EQ( | ||
| line, | ||
| std::string( | ||
| "a\x00" | ||
| "b", | ||
| 3)); | ||
| } | ||
|
|
||
| TEST(BufferedSourceReadLine, ReadsLinesFromPipe) | ||
| { | ||
| Pipe pipe; | ||
| pipe.create(); | ||
|
|
||
| writeFull(pipe.writeSide.get(), "hello\nworld\n", /*allowInterrupts=*/false); | ||
| pipe.writeSide.close(); | ||
|
|
||
| FdSource source(pipe.readSide.get()); | ||
|
|
||
| EXPECT_EQ(source.readLine(), "hello"); | ||
| EXPECT_EQ(source.readLine(), "world"); | ||
| EXPECT_EQ(source.readLine(/*eofOk=*/true), ""); | ||
| } | ||
|
|
||
| TEST(BufferedSourceReadLine, ReturnsPartialLineOnEofWhenAllowed) | ||
| { | ||
| Pipe pipe; | ||
| pipe.create(); | ||
|
|
||
| writeFull(pipe.writeSide.get(), "partial", /*allowInterrupts=*/false); | ||
| pipe.writeSide.close(); | ||
|
|
||
| FdSource source(pipe.readSide.get()); | ||
|
|
||
| EXPECT_EQ(source.readLine(/*eofOk=*/true), "partial"); | ||
| EXPECT_EQ(source.readLine(/*eofOk=*/true), ""); | ||
| } | ||
|
|
||
| TEST(BufferedSourceReadLine, ThrowsOnEofWhenNotAllowed) | ||
| { | ||
| Pipe pipe; | ||
| pipe.create(); | ||
| pipe.writeSide.close(); | ||
|
|
||
| FdSource source(pipe.readSide.get()); | ||
|
|
||
| EXPECT_THROW(source.readLine(), EndOfFile); | ||
| } | ||
|
|
||
| TEST(BufferedSourceReadLine, EmptyLine) | ||
| { | ||
| Pipe pipe; | ||
| pipe.create(); | ||
|
|
||
| writeFull(pipe.writeSide.get(), "\n", /*allowInterrupts=*/false); | ||
| pipe.writeSide.close(); | ||
|
|
||
| FdSource source(pipe.readSide.get()); | ||
|
|
||
| EXPECT_EQ(source.readLine(), ""); | ||
| } | ||
|
|
||
| TEST(BufferedSourceReadLine, ConsecutiveEmptyLines) | ||
| { | ||
| Pipe pipe; | ||
| pipe.create(); | ||
|
|
||
| writeFull(pipe.writeSide.get(), "\n\n\n", /*allowInterrupts=*/false); | ||
| pipe.writeSide.close(); | ||
|
|
||
| FdSource source(pipe.readSide.get()); | ||
|
|
||
| EXPECT_EQ(source.readLine(), ""); | ||
| EXPECT_EQ(source.readLine(), ""); | ||
| EXPECT_EQ(source.readLine(), ""); | ||
| EXPECT_EQ(source.readLine(/*eofOk=*/true), ""); | ||
| } | ||
|
|
||
| TEST(BufferedSourceReadLine, LineWithNullBytes) | ||
| { | ||
| Pipe pipe; | ||
| pipe.create(); | ||
|
|
||
| std::string data( | ||
| "a\x00" | ||
| "b\n", | ||
| 4); | ||
| writeFull(pipe.writeSide.get(), data, /*allowInterrupts=*/false); | ||
| pipe.writeSide.close(); | ||
|
|
||
| FdSource source(pipe.readSide.get()); | ||
|
|
||
| auto line = source.readLine(); | ||
| EXPECT_EQ(line.size(), 3); | ||
| EXPECT_EQ( | ||
| line, | ||
| std::string( | ||
| "a\x00" | ||
| "b", | ||
| 3)); | ||
| } | ||
|
|
||
| TEST(BufferedSourceReadLine, NewlineAtBufferBoundary) | ||
| { | ||
| // "abc\n" with buf=4: newline is last byte, triggers buffer reset. | ||
| TestBufferedStringSource source("abc\nxyz\n", 4); | ||
|
|
||
| EXPECT_EQ(source.readLine(), "abc"); | ||
| EXPECT_FALSE(source.hasData()); | ||
| EXPECT_EQ(source.readLine(), "xyz"); | ||
| } | ||
|
|
||
| TEST(BufferedSourceReadLine, DataRemainsAfterReadLine) | ||
| { | ||
| // "ab\ncd\n" with buf=8: all fits in buffer, data remains after first read. | ||
| TestBufferedStringSource source("ab\ncd\n", 8); | ||
|
|
||
| EXPECT_EQ(source.readLine(), "ab"); | ||
| EXPECT_TRUE(source.hasData()); | ||
| EXPECT_EQ(source.readLine(), "cd"); | ||
| } | ||
|
|
||
| TEST(BufferedSourceReadLine, LineSpansMultipleRefills) | ||
| { | ||
| // 10-char line with buf=4 requires 3 buffer refills. | ||
| TestBufferedStringSource source("0123456789\n", 4); | ||
|
|
||
| EXPECT_EQ(source.readLine(), "0123456789"); | ||
| } | ||
|
|
||
| TEST(BufferedSourceReadLine, BufferExhaustedThenEof) | ||
| { | ||
| // 8 chars with buf=4: two refills, then EOF with partial line. | ||
| TestBufferedStringSource source("abcdefgh", 4); | ||
|
|
||
| EXPECT_EQ(source.readLine(/*eofOk=*/true), "abcdefgh"); | ||
| EXPECT_EQ(source.readLine(/*eofOk=*/true), ""); | ||
| } | ||
|
|
||
| } // namespace nix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,7 +125,7 @@ void Source::skip(size_t len) | |
| size_t BufferedSource::read(char * data, size_t len) | ||
| { | ||
| if (!buffer) | ||
| buffer = decltype(buffer)(new char[bufSize]); | ||
| buffer = std::make_unique_for_overwrite<char[]>(bufSize); | ||
|
|
||
| if (!bufPosIn) | ||
| bufPosIn = readUnbuffered(buffer.get(), bufSize); | ||
|
|
@@ -139,6 +139,51 @@ size_t BufferedSource::read(char * data, size_t len) | |
| return n; | ||
| } | ||
|
|
||
| std::string BufferedSource::readLine(bool eofOk) | ||
| { | ||
| if (!buffer) | ||
| buffer = std::make_unique_for_overwrite<char[]>(bufSize); | ||
|
|
||
| std::string line; | ||
| while (true) { | ||
| if (bufPosOut < bufPosIn) { | ||
| auto * start = buffer.get() + bufPosOut; | ||
| auto * end = buffer.get() + bufPosIn; | ||
| if (auto * newline = static_cast<char *>(memchr(start, '\n', end - start))) { | ||
| line.append(start, newline - start); | ||
| bufPosOut = (newline - buffer.get()) + 1; | ||
| if (bufPosOut == bufPosIn) | ||
| bufPosOut = bufPosIn = 0; | ||
| return line; | ||
| } | ||
|
|
||
| line.append(start, end - start); | ||
| bufPosOut = bufPosIn = 0; | ||
| } | ||
|
|
||
| auto handleEof = [&]() -> std::string { | ||
| bufPosOut = bufPosIn = 0; | ||
| if (eofOk) | ||
| return line; | ||
| throw EndOfFile("unexpected EOF reading a line"); | ||
|
Comment on lines
+165
to
+168
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and below could maybe get deduplicated with a lambda, but that's short enough to maybe not warrant that. |
||
| }; | ||
|
|
||
| size_t n = 0; | ||
| try { | ||
| n = readUnbuffered(buffer.get(), bufSize); | ||
| } catch (EndOfFile & e) { | ||
| return handleEof(); | ||
| } | ||
|
|
||
| if (n == 0) { | ||
| return handleEof(); | ||
| } | ||
|
|
||
| bufPosIn = n; | ||
| bufPosOut = 0; | ||
| } | ||
| } | ||
|
|
||
| bool BufferedSource::hasData() | ||
| { | ||
| return bufPosOut < bufPosIn; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hope we could wrap this in a helper method that returns a
std::span. This manual handling of bounds is quite tricky.