1 //===- LineIterator.h - Iterator to read a text buffer's lines --*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_SUPPORT_LINEITERATOR_H
10 #define LLVM_SUPPORT_LINEITERATOR_H
11 
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/DataTypes.h"
15 #include "llvm/Support/MemoryBufferRef.h"
16 #include <iterator>
17 
18 namespace llvm {
19 
20 class MemoryBuffer;
21 
22 /// A forward iterator which reads text lines from a buffer.
23 ///
24 /// This class provides a forward iterator interface for reading one line at
25 /// a time from a buffer. When default constructed the iterator will be the
26 /// "end" iterator.
27 ///
28 /// The iterator is aware of what line number it is currently processing. It
29 /// strips blank lines by default, and comment lines given a comment-starting
30 /// character.
31 ///
32 /// Note that this iterator requires the buffer to be nul terminated.
33 class line_iterator
34     : public std::iterator<std::forward_iterator_tag, StringRef> {
35   Optional<MemoryBufferRef> Buffer;
36   char CommentMarker = '\0';
37   bool SkipBlanks = true;
38 
39   unsigned LineNumber = 1;
40   StringRef CurrentLine;
41 
42 public:
43   /// Default construct an "end" iterator.
44   line_iterator() = default;
45 
46   /// Construct a new iterator around an unowned memory buffer.
47   explicit line_iterator(const MemoryBufferRef &Buffer, bool SkipBlanks = true,
48                          char CommentMarker = '\0');
49 
50   /// Construct a new iterator around some memory buffer.
51   explicit line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks = true,
52                          char CommentMarker = '\0');
53 
54   /// Return true if we've reached EOF or are an "end" iterator.
55   bool is_at_eof() const { return !Buffer; }
56 
57   /// Return true if we're an "end" iterator or have reached EOF.
58   bool is_at_end() const { return is_at_eof(); }
59 
60   /// Return the current line number. May return any number at EOF.
61   int64_t line_number() const { return LineNumber; }
62 
63   /// Advance to the next (non-empty, non-comment) line.
64   line_iterator &operator++() {
65     advance();
66     return *this;
67   }
68   line_iterator operator++(int) {
69     line_iterator tmp(*this);
70     advance();
71     return tmp;
72   }
73 
74   /// Get the current line as a \c StringRef.
75   StringRef operator*() const { return CurrentLine; }
76   const StringRef *operator->() const { return &CurrentLine; }
77 
78   friend bool operator==(const line_iterator &LHS, const line_iterator &RHS) {
79     return LHS.Buffer == RHS.Buffer &&
80            LHS.CurrentLine.begin() == RHS.CurrentLine.begin();
81   }
82 
83   friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS) {
84     return !(LHS == RHS);
85   }
86 
87 private:
88   /// Advance the iterator to the next line.
89   void advance();
90 };
91 }
92 
93 #endif
94