Skip to content

Commit

Permalink
Fix SkipSpaces behavior at the end of file
Browse files Browse the repository at this point in the history
  • Loading branch information
kpu committed Oct 21, 2013
1 parent 19e6c9e commit 0afbb0f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 3 additions & 1 deletion util/file_piece.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ StringPiece FilePiece::ReadLine(char delim) {
}
}
if (at_end_) {
if (position_ == position_end_) Shift();
if (position_ == position_end_) {
Shift();
}
return Consume(position_end_);
}
skip = position_end_ - position_;
Expand Down
10 changes: 9 additions & 1 deletion util/file_piece.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <iosfwd>
#include <string>

#include <assert.h>
#include <stdint.h>

namespace util {
Expand Down Expand Up @@ -66,8 +67,14 @@ class FilePiece {

// Skip spaces defined by isspace.
void SkipSpaces(const bool *delim = kSpaces) {
assert(position_ <= position_end_);
for (; ; ++position_) {
if (position_ == position_end_) Shift();
if (position_ == position_end_) {
Shift();
// And break out at end of file.
if (position_ == position_end_) return;
}
assert(position_ < position_end_);
if (!delim[static_cast<unsigned char>(*position_)]) return;
}
}
Expand All @@ -86,6 +93,7 @@ class FilePiece {
template <class T> T ReadNumber();

StringPiece Consume(const char *to) {
assert(to >= position_);
StringPiece ret(position_, to - position_);
position_ = to;
return ret;
Expand Down

0 comments on commit 0afbb0f

Please sign in to comment.