diff options
author | Kenneth Heafield <kheafiel@cluster10.lti.ece.cmu.local> | 2011-03-09 13:40:23 -0500 |
---|---|---|
committer | Kenneth Heafield <kheafiel@cluster10.lti.ece.cmu.local> | 2011-03-09 13:40:23 -0500 |
commit | 6c923d45f2aaf960806429d36ca58a41b3a39740 (patch) | |
tree | 9d8c5bf26189e9e8e6c12c199a5925c5ca6046a9 /klm/util/file_piece.cc | |
parent | 95ea293005f74a627fdd2aae318d5746fa8c4e6c (diff) |
kenlm sync
Diffstat (limited to 'klm/util/file_piece.cc')
-rw-r--r-- | klm/util/file_piece.cc | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/klm/util/file_piece.cc b/klm/util/file_piece.cc index 81eb9bb9..67681f7e 100644 --- a/klm/util/file_piece.cc +++ b/klm/util/file_piece.cc @@ -79,22 +79,22 @@ FilePiece::~FilePiece() { } StringPiece FilePiece::ReadLine(char delim) throw (GZException, EndOfFileException) { - const char *start = position_; - do { - for (const char *i = start; i < position_end_; ++i) { + size_t skip = 0; + while (true) { + for (const char *i = position_ + skip; i < position_end_; ++i) { if (*i == delim) { StringPiece ret(position_, i - position_); position_ = i + 1; return ret; } } - size_t skip = position_end_ - position_; + if (at_end_) { + if (position_ == position_end_) Shift(); + return Consume(position_end_); + } + skip = position_end_ - position_; Shift(); - start = position_ + skip; - } while (!at_end_); - StringPiece ret(position_, position_end_ - position_); - position_ = position_end_; - return ret; + } } float FilePiece::ReadFloat() throw(GZException, EndOfFileException, ParseNumberException) { @@ -186,6 +186,21 @@ template <class T> T FilePiece::ReadNumber() throw(GZException, EndOfFileExcepti return ret; } +const char *FilePiece::FindDelimiterOrEOF(const bool *delim) throw (GZException, EndOfFileException) { + size_t skip = 0; + while (true) { + for (const char *i = position_ + skip; i < position_end_; ++i) { + if (delim[static_cast<unsigned char>(*i)]) return i; + } + if (at_end_) { + if (position_ == position_end_) Shift(); + return position_end_; + } + skip = position_end_ - position_; + Shift(); + } +} + void FilePiece::Shift() throw(GZException, EndOfFileException) { if (at_end_) { progress_.Finished(); |