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, char CommentMarker)
16 : Buffer(Buffer.getBufferSize() ? &Buffer : 0),
17 CommentMarker(CommentMarker), LineNumber(1),
18 CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : 0, 0) {
19 // Ensure that if we are constructed on a non-empty memory buffer that it is
20 // a null terminated buffer.
21 if (Buffer.getBufferSize()) {
22 assert(Buffer.getBufferEnd()[0] == '\0');
27 void line_iterator::advance() {
28 assert(Buffer && "Cannot advance past the end!");
30 const char *Pos = CurrentLine.end();
31 assert(Pos == Buffer->getBufferStart() || *Pos == '\n' || *Pos == '\0');
33 if (CommentMarker == '\0') {
34 // If we're not stripping comments, this is simpler.
36 while (Pos[Blanks] == '\n')
41 // Skip comments and count line numbers, which is a bit more complex.
43 if (*Pos == CommentMarker)
46 } while (*Pos != '\0' && *Pos != '\n');
55 // We've hit the end of the buffer, reset ourselves to the end state.
57 CurrentLine = StringRef();
65 } while (Pos[Length] != '\0' && Pos[Length] != '\n');
67 CurrentLine = StringRef(Pos, Length);