LLVM 20.0.0git
LineIterator.h
Go to the documentation of this file.
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/StringRef.h"
15#include <iterator>
16#include <optional>
17
18namespace llvm {
19
20class 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.
34 std::optional<MemoryBufferRef> Buffer;
35 char CommentMarker = '\0';
36 bool SkipBlanks = true;
37
38 unsigned LineNumber = 1;
39 StringRef CurrentLine;
40
41public:
42 using iterator_category = std::forward_iterator_tag;
44 using difference_type = std::ptrdiff_t;
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.
70 advance();
71 return *this;
72 }
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
92private:
93 /// Advance the iterator to the next line.
94 void advance();
95};
96}
97
98#endif
Value * RHS
Value * LHS
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:33
std::ptrdiff_t difference_type
Definition: LineIterator.h:44
friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS)
Definition: LineIterator.h:88
int64_t line_number() const
Return the current line number. May return any number at EOF.
Definition: LineIterator.h:66
bool is_at_eof() const
Return true if we've reached EOF or are an "end" iterator.
Definition: LineIterator.h:60
line_iterator & operator++()
Advance to the next (non-empty, non-comment) line.
Definition: LineIterator.h:69
const StringRef * operator->() const
Definition: LineIterator.h:81
bool is_at_end() const
Return true if we're an "end" iterator or have reached EOF.
Definition: LineIterator.h:63
StringRef operator*() const
Get the current line as a StringRef.
Definition: LineIterator.h:80
friend bool operator==(const line_iterator &LHS, const line_iterator &RHS)
Definition: LineIterator.h:83
line_iterator()=default
Default construct an "end" iterator.
std::forward_iterator_tag iterator_category
Definition: LineIterator.h:42
line_iterator operator++(int)
Definition: LineIterator.h:73
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18