Skip to content

Commit

Permalink
Make FilePiece::ReadLine() skip carriage returns.
Browse files Browse the repository at this point in the history
By default, ReadLine() will not include a trailing carriage return in
the line it returns.  This behaviour is optional, however.
  • Loading branch information
Jeroen Vermeulen committed May 19, 2015
1 parent 0cf0790 commit 29b8e2d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
13 changes: 9 additions & 4 deletions util/file_piece.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,17 @@ FilePiece::FilePiece(std::istream &stream, const char *name, std::size_t min_buf

FilePiece::~FilePiece() {}

StringPiece FilePiece::ReadLine(char delim) {
StringPiece FilePiece::ReadLine(char delim, bool strip_cr) {
std::size_t skip = 0;
while (true) {
for (const char *i = position_ + skip; i < position_end_; ++i) {
if (*i == delim) {
StringPiece ret(position_, i - position_);
// End of line.
// Take 1 byte off the end if it's an unwanted carriage return.
const std::size_t subtract_cr = (
(strip_cr && i > position_ && *(i - 1) == '\r') ?
1 : 0);
StringPiece ret(position_, i - position_ - subtract_cr);
position_ = i + 1;
return ret;
}
Expand All @@ -86,9 +91,9 @@ StringPiece FilePiece::ReadLine(char delim) {
}
}

bool FilePiece::ReadLineOrEOF(StringPiece &to, char delim) {
bool FilePiece::ReadLineOrEOF(StringPiece &to, char delim, bool strip_cr) {
try {
to = ReadLine(delim);
to = ReadLine(delim, strip_cr);
} catch (const util::EndOfFileException &e) { return false; }
return true;
}
Expand Down
32 changes: 25 additions & 7 deletions util/file_piece.hh
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class FilePiece {
return Consume(FindDelimiterOrEOF(delim));
}

// Read word until the line or file ends.
/// Read word until the line or file ends.
bool ReadWordSameLine(StringPiece &to, const bool *delim = kSpaces) {
assert(delim[static_cast<unsigned char>('\n')]);
// Skip non-enter spaces.
Expand All @@ -75,12 +75,30 @@ class FilePiece {
return true;
}

// Unlike ReadDelimited, this includes leading spaces and consumes the delimiter.
// It is similar to getline in that way.
StringPiece ReadLine(char delim = '\n');

// Doesn't throw EndOfFileException, just returns false.
bool ReadLineOrEOF(StringPiece &to, char delim = '\n');
/** Read a line of text from the file.
*
* Unlike ReadDelimited, this includes leading spaces and consumes the
* delimiter. It is similar to getline in that way.
*
* If strip_cr is true, any trailing carriate return (as would be found on
* a file written on Windows) will be left out of the returned line.
*
* Throws EndOfFileException if the end of the file is encountered. If the
* file does not end in a newline, this could mean that the last line is
* never read.
*/
StringPiece ReadLine(char delim = '\n', bool strip_cr = true);

/** Read a line of text from the file, or return false on EOF.
*
* This is like ReadLine, except it returns false where ReadLine throws
* EndOfFileException. Like ReadLine it may not read the last line in the
* file if the file does not end in a newline.
*
* If strip_cr is true, any trailing carriate return (as would be found on
* a file written on Windows) will be left out of the returned line.
*/
bool ReadLineOrEOF(StringPiece &to, char delim = '\n', bool strip_cr = false);

float ReadFloat();
double ReadDouble();
Expand Down

0 comments on commit 29b8e2d

Please sign in to comment.