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"
14 #include "llvm/Support/DataTypes.h"
21 /// \brief A forward iterator which reads non-blank text lines from a buffer.
23 /// This class provides a forward iterator interface for reading one line at
24 /// a time from a buffer. When default constructed the iterator will be the
27 /// The iterator also is aware of what line number it is currently processing
28 /// and can strip comment lines given the comment-starting character.
30 /// Note that this iterator requires the buffer to be nul terminated.
32 : public std::iterator<std::forward_iterator_tag, StringRef> {
33 const MemoryBuffer *Buffer;
37 StringRef CurrentLine;
40 /// \brief Default construct an "end" iterator.
41 line_iterator() : Buffer(nullptr) {}
43 /// \brief Construct a new iterator around some memory buffer.
44 explicit line_iterator(const MemoryBuffer &Buffer, char CommentMarker = '\0');
46 /// \brief Return true if we've reached EOF or are an "end" iterator.
47 bool is_at_eof() const { return !Buffer; }
49 /// \brief Return true if we're an "end" iterator or have reached EOF.
50 bool is_at_end() const { return is_at_eof(); }
52 /// \brief Return the current line number. May return any number at EOF.
53 int64_t line_number() const { return LineNumber; }
55 /// \brief Advance to the next (non-empty, non-comment) line.
56 line_iterator &operator++() {
60 line_iterator operator++(int) {
61 line_iterator tmp(*this);
66 /// \brief Get the current line as a \c StringRef.
67 StringRef operator*() const { return CurrentLine; }
68 const StringRef *operator->() const { return &CurrentLine; }
70 friend bool operator==(const line_iterator &LHS, const line_iterator &RHS) {
71 return LHS.Buffer == RHS.Buffer &&
72 LHS.CurrentLine.begin() == RHS.CurrentLine.begin();
75 friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS) {
80 /// \brief Advance the iterator to the next line.