xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Support/LineIterator.h (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
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   Optional<MemoryBufferRef> Buffer;
35   char CommentMarker = '\0';
36   bool SkipBlanks = true;
37 
38   unsigned LineNumber = 1;
39   StringRef CurrentLine;
40 
41 public:
42   using iterator_category = std::forward_iterator_tag;
43   using value_type = StringRef;
44   using difference_type = std::ptrdiff_t;
45   using pointer = value_type *;
46   using reference = value_type &;
47 
48   /// Default construct an "end" iterator.
49   line_iterator() = default;
50 
51   /// Construct a new iterator around an unowned memory buffer.
52   explicit line_iterator(const MemoryBufferRef &Buffer, bool SkipBlanks = true,
53                          char CommentMarker = '\0');
54 
55   /// Construct a new iterator around some memory buffer.
56   explicit line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks = true,
57                          char CommentMarker = '\0');
58 
59   /// Return true if we've reached EOF or are an "end" iterator.
60   bool is_at_eof() const { return !Buffer; }
61 
62   /// Return true if we're an "end" iterator or have reached EOF.
63   bool is_at_end() const { return is_at_eof(); }
64 
65   /// Return the current line number. May return any number at EOF.
66   int64_t line_number() const { return LineNumber; }
67 
68   /// Advance to the next (non-empty, non-comment) line.
69   line_iterator &operator++() {
70     advance();
71     return *this;
72   }
73   line_iterator operator++(int) {
74     line_iterator tmp(*this);
75     advance();
76     return tmp;
77   }
78 
79   /// Get the current line as a \c StringRef.
80   StringRef operator*() const { return CurrentLine; }
81   const StringRef *operator->() const { return &CurrentLine; }
82 
83   friend bool operator==(const line_iterator &LHS, const line_iterator &RHS) {
84     return LHS.Buffer == RHS.Buffer &&
85            LHS.CurrentLine.begin() == RHS.CurrentLine.begin();
86   }
87 
88   friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS) {
89     return !(LHS == RHS);
90   }
91 
92 private:
93   /// Advance the iterator to the next line.
94   void advance();
95 };
96 }
97 
98 #endif
99