rewrite CountNumNewlinesBetween to be in terms of StringRef.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82410 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/utils/FileCheck/FileCheck.cpp b/utils/FileCheck/FileCheck.cpp
index b429f5d..8f48c3a 100644
--- a/utils/FileCheck/FileCheck.cpp
+++ b/utils/FileCheck/FileCheck.cpp
@@ -241,23 +241,24 @@
                   "note");
 }
 
-static unsigned CountNumNewlinesBetween(const char *Start, const char *End) {
+/// CountNumNewlinesBetween - Count the number of newlines in the specified
+/// range.
+static unsigned CountNumNewlinesBetween(StringRef Range) {
   unsigned NumNewLines = 0;
-  for (; Start != End; ++Start) {
+  while (1) {
     // Scan for newline.
-    if (Start[0] != '\n' && Start[0] != '\r')
-      continue;
+    Range = Range.substr(Range.find_first_of("\n\r"));
+    if (Range.empty()) return NumNewLines;
     
     ++NumNewLines;
     
     // Handle \n\r and \r\n as a single newline.
-    if (Start+1 != End &&
-        (Start[0] == '\n' || Start[0] == '\r') &&
-        (Start[0] != Start[1]))
-      ++Start;
+    if (Range.size() > 1 &&
+        (Range[1] == '\n' || Range[1] == '\r') &&
+        (Range[0] != Range[1]))
+      Range = Range.substr(1);
+    Range = Range.substr(1);
   }
-  
-  return NumNewLines;
 }
 
 int main(int argc, char **argv) {
@@ -311,7 +312,9 @@
       PrintCheckFailed(SM, CheckStr, SearchFrom);
       return 1;
     }
-    
+
+    StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch);
+
     // If this check is a "CHECK-NEXT", verify that the previous match was on
     // the previous line (i.e. that there is one newline between them).
     if (CheckStr.IsCheckNext) {
@@ -319,7 +322,7 @@
       assert(LastMatch != F->getBufferStart() &&
              "CHECK-NEXT can't be the first check in a file");
 
-      unsigned NumNewLines = CountNumNewlinesBetween(LastMatch, Buffer.data());
+      unsigned NumNewLines = CountNumNewlinesBetween(SkippedRegion);
       if (NumNewLines == 0) {
         SM.PrintMessage(CheckStr.Loc,
                     CheckPrefix+"-NEXT: is on the same line as previous match",
@@ -346,7 +349,6 @@
     
     // If this match had "not strings", verify that they don't exist in the
     // skipped region.
-    StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch);
     for (unsigned i = 0, e = CheckStr.NotStrings.size(); i != e; ++i) {
       size_t Pos = SkippedRegion.find(CheckStr.NotStrings[i].second);
       if (Pos == StringRef::npos) continue;