1 //===- LineIterator.cpp - Implementation of line iteration ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/LineIterator.h"
11 #include "llvm/Support/MemoryBuffer.h"
15 line_iterator::line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks,
17 : Buffer(Buffer.getBufferSize() ? &Buffer : nullptr),
18 CommentMarker(CommentMarker), SkipBlanks(SkipBlanks), LineNumber(1),
19 CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : nullptr,
21 // Ensure that if we are constructed on a non-empty memory buffer that it is
22 // a null terminated buffer.
23 if (Buffer.getBufferSize()) {
24 assert(Buffer.getBufferEnd()[0] == '\0');
25 // Make sure we don't skip a leading newline if we're keeping blanks
26 if (SkipBlanks || *Buffer.getBufferStart() != '\n')
31 void line_iterator::advance() {
32 assert(Buffer && "Cannot advance past the end!");
34 const char *Pos = CurrentLine.end();
35 assert(Pos == Buffer->getBufferStart() || *Pos == '\n' || *Pos == '\0');
41 if (!SkipBlanks && *Pos == '\n') {
42 // Nothing to do for a blank line.
43 } else if (CommentMarker == '\0') {
44 // If we're not stripping comments, this is simpler.
46 while (Pos[Blanks] == '\n')
51 // Skip comments and count line numbers, which is a bit more complex.
53 if (*Pos == '\n' && !SkipBlanks)
55 if (*Pos == CommentMarker)
58 } while (*Pos != '\0' && *Pos != '\n');
67 // We've hit the end of the buffer, reset ourselves to the end state.
69 CurrentLine = StringRef();
75 while (Pos[Length] != '\0' && Pos[Length] != '\n') {
79 CurrentLine = StringRef(Pos, Length);