diff options
author | Kenneth Heafield <github@kheafield.com> | 2014-06-02 21:28:02 +0400 |
---|---|---|
committer | Kenneth Heafield <github@kheafield.com> | 2014-06-02 21:29:40 +0400 |
commit | dd03f9fb69da82a37f1846d25dcc8d8781956e96 (patch) | |
tree | 12df06240d6149f110982d1f76c9addf40acbce1 /util/file_piece.hh | |
parent | 31a583b0bc322626356a42965f02534249070525 (diff) |
KenLM 5a7efd8fe1db88ee0a9f7e9479b24ac3ca348221 with Hieu's patch to exception.hh
Diffstat (limited to 'util/file_piece.hh')
-rw-r--r-- | util/file_piece.hh | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/util/file_piece.hh b/util/file_piece.hh index ed3dc5adf..5495ddccc 100644 --- a/util/file_piece.hh +++ b/util/file_piece.hh @@ -1,5 +1,5 @@ -#ifndef UTIL_FILE_PIECE__ -#define UTIL_FILE_PIECE__ +#ifndef UTIL_FILE_PIECE_H +#define UTIL_FILE_PIECE_H #include "util/ersatz_progress.hh" #include "util/exception.hh" @@ -56,10 +56,33 @@ class FilePiece { return Consume(FindDelimiterOrEOF(delim)); } + // 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. + for (; ; ++position_) { + if (position_ == position_end_) { + try { + Shift(); + } catch (const util::EndOfFileException &e) { return false; } + // And break out at end of file. + if (position_ == position_end_) return false; + } + if (!delim[static_cast<unsigned char>(*position_)]) break; + if (*position_ == '\n') return false; + } + // We can't be at the end of file because there's at least one character open. + to = Consume(FindDelimiterOrEOF(delim)); + 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'); + float ReadFloat(); double ReadDouble(); long int ReadLong(); @@ -132,4 +155,4 @@ class FilePiece { } // namespace util -#endif // UTIL_FILE_PIECE__ +#endif // UTIL_FILE_PIECE_H |