[llvm] r221153 - Add CRLF support to LineIterator.

Rafael Espindola rafael.espindola at gmail.com
Mon Nov 3 06:09:47 PST 2014


Author: rafael
Date: Mon Nov  3 08:09:47 2014
New Revision: 221153

URL: http://llvm.org/viewvc/llvm-project?rev=221153&view=rev
Log:
Add CRLF support to LineIterator.

The MRI scripts have to work with CRLF, and in general it is probably
a good idea to support this in a core utility like LineIterator.

Added:
    llvm/trunk/test/Object/Inputs/mri-crlf.mri
    llvm/trunk/test/Object/mri-crlf.test
Modified:
    llvm/trunk/lib/Support/LineIterator.cpp

Modified: llvm/trunk/lib/Support/LineIterator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/LineIterator.cpp?rev=221153&r1=221152&r2=221153&view=diff
==============================================================================
--- llvm/trunk/lib/Support/LineIterator.cpp (original)
+++ llvm/trunk/lib/Support/LineIterator.cpp Mon Nov  3 08:09:47 2014
@@ -12,6 +12,26 @@
 
 using namespace llvm;
 
+static bool isAtLineEnd(const char *P) {
+  if (*P == '\n')
+    return true;
+  if (*P == '\r' && *(P + 1) == '\n')
+    return true;
+  return false;
+}
+
+static bool skipIfAtLineEnd(const char *&P) {
+  if (*P == '\n') {
+    ++P;
+    return true;
+  }
+  if (*P == '\r' && *(P + 1) == '\n') {
+    P += 2;
+    return true;
+  }
+  return false;
+}
+
 line_iterator::line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks,
                              char CommentMarker)
     : Buffer(Buffer.getBufferSize() ? &Buffer : nullptr),
@@ -23,7 +43,7 @@ line_iterator::line_iterator(const Memor
   if (Buffer.getBufferSize()) {
     assert(Buffer.getBufferEnd()[0] == '\0');
     // Make sure we don't skip a leading newline if we're keeping blanks
-    if (SkipBlanks || *Buffer.getBufferStart() != '\n')
+    if (SkipBlanks || !isAtLineEnd(Buffer.getBufferStart()))
       advance();
   }
 }
@@ -32,33 +52,27 @@ void line_iterator::advance() {
   assert(Buffer && "Cannot advance past the end!");
 
   const char *Pos = CurrentLine.end();
-  assert(Pos == Buffer->getBufferStart() || *Pos == '\n' || *Pos == '\0');
+  assert(Pos == Buffer->getBufferStart() || isAtLineEnd(Pos) || *Pos == '\0');
 
-  if (*Pos == '\n') {
-    ++Pos;
+  if (skipIfAtLineEnd(Pos))
     ++LineNumber;
-  }
-  if (!SkipBlanks && *Pos == '\n') {
+  if (!SkipBlanks && isAtLineEnd(Pos)) {
     // Nothing to do for a blank line.
   } else if (CommentMarker == '\0') {
     // If we're not stripping comments, this is simpler.
-    size_t Blanks = 0;
-    while (Pos[Blanks] == '\n')
-      ++Blanks;
-    Pos += Blanks;
-    LineNumber += Blanks;
+    while (skipIfAtLineEnd(Pos))
+      ++LineNumber;
   } else {
     // Skip comments and count line numbers, which is a bit more complex.
     for (;;) {
-      if (*Pos == '\n' && !SkipBlanks)
+      if (isAtLineEnd(Pos) && !SkipBlanks)
         break;
       if (*Pos == CommentMarker)
         do {
           ++Pos;
-        } while (*Pos != '\0' && *Pos != '\n');
-      if (*Pos != '\n')
+        } while (*Pos != '\0' && !isAtLineEnd(Pos));
+      if (!skipIfAtLineEnd(Pos))
         break;
-      ++Pos;
       ++LineNumber;
     }
   }
@@ -72,7 +86,7 @@ void line_iterator::advance() {
 
   // Measure the line.
   size_t Length = 0;
-  while (Pos[Length] != '\0' && Pos[Length] != '\n') {
+  while (Pos[Length] != '\0' && !isAtLineEnd(&Pos[Length])) {
     ++Length;
   }
 

Added: llvm/trunk/test/Object/Inputs/mri-crlf.mri
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/Inputs/mri-crlf.mri?rev=221153&view=auto
==============================================================================
--- llvm/trunk/test/Object/Inputs/mri-crlf.mri (added)
+++ llvm/trunk/test/Object/Inputs/mri-crlf.mri Mon Nov  3 08:09:47 2014
@@ -0,0 +1,2 @@
+; this file intentionally has crlf line endings
+end

Added: llvm/trunk/test/Object/mri-crlf.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/mri-crlf.test?rev=221153&view=auto
==============================================================================
--- llvm/trunk/test/Object/mri-crlf.test (added)
+++ llvm/trunk/test/Object/mri-crlf.test Mon Nov  3 08:09:47 2014
@@ -0,0 +1 @@
+; RUN: llvm-ar -M  < %S/Inputs/mri-crlf.mri





More information about the llvm-commits mailing list