LineIterator.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- LineIterator.h - Iterator to read a text buffer's lines --*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_LINEITERATOR_H
  14. #define LLVM_SUPPORT_LINEITERATOR_H
  15. #include "llvm/ADT/Optional.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/Support/DataTypes.h"
  18. #include "llvm/Support/MemoryBufferRef.h"
  19. #include <iterator>
  20. namespace llvm {
  21. class MemoryBuffer;
  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. unsigned LineNumber = 1;
  38. StringRef CurrentLine;
  39. public:
  40. using iterator_category = std::forward_iterator_tag;
  41. using value_type = StringRef;
  42. using difference_type = std::ptrdiff_t;
  43. using pointer = value_type *;
  44. using reference = value_type &;
  45. /// Default construct an "end" iterator.
  46. line_iterator() = default;
  47. /// Construct a new iterator around an unowned memory buffer.
  48. explicit line_iterator(const MemoryBufferRef &Buffer, bool SkipBlanks = true,
  49. char CommentMarker = '\0');
  50. /// Construct a new iterator around some memory buffer.
  51. explicit line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks = true,
  52. char CommentMarker = '\0');
  53. /// Return true if we've reached EOF or are an "end" iterator.
  54. bool is_at_eof() const { return !Buffer; }
  55. /// Return true if we're an "end" iterator or have reached EOF.
  56. bool is_at_end() const { return is_at_eof(); }
  57. /// Return the current line number. May return any number at EOF.
  58. int64_t line_number() const { return LineNumber; }
  59. /// Advance to the next (non-empty, non-comment) line.
  60. line_iterator &operator++() {
  61. advance();
  62. return *this;
  63. }
  64. line_iterator operator++(int) {
  65. line_iterator tmp(*this);
  66. advance();
  67. return tmp;
  68. }
  69. /// Get the current line as a \c StringRef.
  70. StringRef operator*() const { return CurrentLine; }
  71. const StringRef *operator->() const { return &CurrentLine; }
  72. friend bool operator==(const line_iterator &LHS, const line_iterator &RHS) {
  73. return LHS.Buffer == RHS.Buffer &&
  74. LHS.CurrentLine.begin() == RHS.CurrentLine.begin();
  75. }
  76. friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS) {
  77. return !(LHS == RHS);
  78. }
  79. private:
  80. /// Advance the iterator to the next line.
  81. void advance();
  82. };
  83. }
  84. #endif
  85. #ifdef __GNUC__
  86. #pragma GCC diagnostic pop
  87. #endif