Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/libutil/include/nix/util/file-descriptor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ void writeFull(Descriptor fd, std::string_view s, bool allowInterrupts = true);
*
* @param fd The file descriptor to read from
* @param eofOk If true, return an unterminated line if EOF is reached. (e.g. the empty string)
* @param terminator The chartacter that ends the line
*
* @return A line of text ending in `\n`, or a string without `\n` if `eofOk` is true and EOF is reached.
*/
std::string readLine(Descriptor fd, bool eofOk = false);
std::string readLine(Descriptor fd, bool eofOk = false, char terminator = '\n');

/**
* Write a line to a file descriptor.
Expand Down
2 changes: 1 addition & 1 deletion src/libutil/include/nix/util/serialise.hh
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ struct BufferedSource : virtual Source

size_t read(char * data, size_t len) override;

std::string readLine(bool eofOk = false);
std::string readLine(bool eofOk = false, char terminator = '\n');

/**
* Return true if the buffer is not empty.
Expand Down
4 changes: 2 additions & 2 deletions src/libutil/serialise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ size_t BufferedSource::read(char * data, size_t len)
return n;
}

std::string BufferedSource::readLine(bool eofOk)
std::string BufferedSource::readLine(bool eofOk, char terminator)
{
if (!buffer)
buffer = std::make_unique_for_overwrite<char[]>(bufSize);
Expand All @@ -148,7 +148,7 @@ std::string BufferedSource::readLine(bool eofOk)
if (bufPosOut < bufPosIn) {
auto * start = buffer.get() + bufPosOut;
auto * end = buffer.get() + bufPosIn;
if (auto * newline = static_cast<char *>(memchr(start, '\n', end - start))) {
if (auto * newline = static_cast<char *>(memchr(start, terminator, end - start))) {
line.append(start, newline - start);
bufPosOut = (newline - buffer.get()) + 1;
if (bufPosOut == bufPosIn)
Expand Down
4 changes: 2 additions & 2 deletions src/libutil/unix/file-descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void writeFull(int fd, std::string_view s, bool allowInterrupts)
}
}

std::string readLine(int fd, bool eofOk)
std::string readLine(int fd, bool eofOk, char terminator)
{
std::string s;
while (1) {
Expand All @@ -123,7 +123,7 @@ std::string readLine(int fd, bool eofOk)
else
throw EndOfFile("unexpected EOF reading a line");
} else {
if (ch == '\n')
if (ch == terminator)
return s;
s += ch;
}
Expand Down
Loading