Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:32

0001 //===- LineIterator.h - Iterator to read a text buffer's lines --*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_SUPPORT_LINEITERATOR_H
0010 #define LLVM_SUPPORT_LINEITERATOR_H
0011 
0012 #include "llvm/ADT/StringRef.h"
0013 #include "llvm/Support/DataTypes.h"
0014 #include "llvm/Support/MemoryBufferRef.h"
0015 #include <iterator>
0016 #include <optional>
0017 
0018 namespace llvm {
0019 
0020 class MemoryBuffer;
0021 
0022 /// A forward iterator which reads text lines from a buffer.
0023 ///
0024 /// This class provides a forward iterator interface for reading one line at
0025 /// a time from a buffer. When default constructed the iterator will be the
0026 /// "end" iterator.
0027 ///
0028 /// The iterator is aware of what line number it is currently processing. It
0029 /// strips blank lines by default, and comment lines given a comment-starting
0030 /// character.
0031 ///
0032 /// Note that this iterator requires the buffer to be nul terminated.
0033 class line_iterator {
0034   std::optional<MemoryBufferRef> Buffer;
0035   char CommentMarker = '\0';
0036   bool SkipBlanks = true;
0037 
0038   unsigned LineNumber = 1;
0039   StringRef CurrentLine;
0040 
0041 public:
0042   using iterator_category = std::forward_iterator_tag;
0043   using value_type = StringRef;
0044   using difference_type = std::ptrdiff_t;
0045   using pointer = value_type *;
0046   using reference = value_type &;
0047 
0048   /// Default construct an "end" iterator.
0049   line_iterator() = default;
0050 
0051   /// Construct a new iterator around an unowned memory buffer.
0052   explicit line_iterator(const MemoryBufferRef &Buffer, bool SkipBlanks = true,
0053                          char CommentMarker = '\0');
0054 
0055   /// Construct a new iterator around some memory buffer.
0056   explicit line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks = true,
0057                          char CommentMarker = '\0');
0058 
0059   /// Return true if we've reached EOF or are an "end" iterator.
0060   bool is_at_eof() const { return !Buffer; }
0061 
0062   /// Return true if we're an "end" iterator or have reached EOF.
0063   bool is_at_end() const { return is_at_eof(); }
0064 
0065   /// Return the current line number. May return any number at EOF.
0066   int64_t line_number() const { return LineNumber; }
0067 
0068   /// Advance to the next (non-empty, non-comment) line.
0069   line_iterator &operator++() {
0070     advance();
0071     return *this;
0072   }
0073   line_iterator operator++(int) {
0074     line_iterator tmp(*this);
0075     advance();
0076     return tmp;
0077   }
0078 
0079   /// Get the current line as a \c StringRef.
0080   StringRef operator*() const { return CurrentLine; }
0081   const StringRef *operator->() const { return &CurrentLine; }
0082 
0083   friend bool operator==(const line_iterator &LHS, const line_iterator &RHS) {
0084     return LHS.Buffer == RHS.Buffer &&
0085            LHS.CurrentLine.begin() == RHS.CurrentLine.begin();
0086   }
0087 
0088   friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS) {
0089     return !(LHS == RHS);
0090   }
0091 
0092 private:
0093   /// Advance the iterator to the next line.
0094   void advance();
0095 };
0096 }
0097 
0098 #endif