LineIterator.h 2.6 KB

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