1 //===- LineIterator.h - Iterator to read a text buffer's lines --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/StringRef.h"
17 /// \brief A forward iterator which reads non-blank text lines from a buffer.
19 /// This class provides a forward iterator interface for reading one line at
20 /// a time from a buffer. When default constructed the iterator will be the
23 /// The iterator also is aware of what line number it is currently processing
24 /// and can strip comment lines given the comment-starting character.
26 /// Note that this iterator requires the buffer to be nul terminated.
28 : public std::iterator<std::forward_iterator_tag, StringRef, ptrdiff_t> {
29 const MemoryBuffer *Buffer;
33 StringRef CurrentLine;
36 /// \brief Default construct an "end" iterator.
37 line_iterator() : Buffer(0) {}
39 /// \brief Construct a new iterator around some memory buffer.
40 explicit line_iterator(const MemoryBuffer &Buffer, char CommentMarker = '\0');
42 /// \brief Return true if we've reached EOF or are an "end" iterator.
43 bool is_at_eof() const { return !Buffer; }
45 /// \brief Return true if we're an "end" iterator or have reached EOF.
46 bool is_at_end() const { return is_at_eof(); }
48 /// \brief Return the current line number. May return any number at EOF.
49 int64_t line_number() const { return LineNumber; }
51 /// \brief Advance to the next (non-empty, non-comment) line.
52 line_iterator &operator++() {
57 /// \brief Get the current line as a \c StringRef.
58 StringRef operator*() const { return CurrentLine; }
59 const StringRef *operator->() const { return &CurrentLine; }
61 friend bool operator==(const line_iterator &LHS, const line_iterator &RHS) {
62 return LHS.Buffer == RHS.Buffer &&
63 LHS.CurrentLine.begin() == RHS.CurrentLine.begin();
66 friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS) {
71 /// \brief Advance the iterator to the next line.