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 #ifndef LLVM_SUPPORT_LINEITERATOR_H__
11 #define LLVM_SUPPORT_LINEITERATOR_H__
13 #include "llvm/ADT/StringRef.h"
20 /// \brief A forward iterator which reads non-blank text lines from a buffer.
22 /// This class provides a forward iterator interface for reading one line at
23 /// a time from a buffer. When default constructed the iterator will be the
26 /// The iterator also is aware of what line number it is currently processing
27 /// and can strip comment lines given the comment-starting character.
29 /// Note that this iterator requires the buffer to be nul terminated.
31 : public std::iterator<std::forward_iterator_tag, StringRef, ptrdiff_t> {
32 const MemoryBuffer *Buffer;
36 StringRef CurrentLine;
39 /// \brief Default construct an "end" iterator.
40 line_iterator() : Buffer(0) {}
42 /// \brief Construct a new iterator around some memory buffer.
43 explicit line_iterator(const MemoryBuffer &Buffer, char CommentMarker = '\0');
45 /// \brief Return true if we've reached EOF or are an "end" iterator.
46 bool is_at_eof() const { return !Buffer; }
48 /// \brief Return true if we're an "end" iterator or have reached EOF.
49 bool is_at_end() const { return is_at_eof(); }
51 /// \brief Return the current line number. May return any number at EOF.
52 int64_t line_number() const { return LineNumber; }
54 /// \brief Advance to the next (non-empty, non-comment) line.
55 line_iterator &operator++() {
59 line_iterator operator++(int) {
60 line_iterator tmp(*this);
65 /// \brief Get the current line as a \c StringRef.
66 StringRef operator*() const { return CurrentLine; }
67 const StringRef *operator->() const { return &CurrentLine; }
69 friend bool operator==(const line_iterator &LHS, const line_iterator &RHS) {
70 return LHS.Buffer == RHS.Buffer &&
71 LHS.CurrentLine.begin() == RHS.CurrentLine.begin();
74 friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS) {
79 /// \brief Advance the iterator to the next line.
84 #endif // LLVM_SUPPORT_LINEITERATOR_H__